1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 /// The standard logging macro
15 /// This macro will generically log over a provided level (of type u32) with a
16 /// format!-based argument list. See documentation in `std::fmt` for details on
17 /// how to use the syntax.
22 /// #[macro_use] extern crate log;
25 /// log!(log::WARN, "this is a warning {}", "message");
26 /// log!(log::DEBUG, "this is a debug message");
27 /// log!(6, "this is a custom logging level: {level}", level=6);
31 /// Assumes the binary is `main`:
34 /// $ RUST_LOG=warn ./main
35 /// WARN:main: this is a warning message
39 /// $ RUST_LOG=debug ./main
40 /// DEBUG:main: this is a debug message
41 /// WARN:main: this is a warning message
45 /// $ RUST_LOG=6 ./main
46 /// DEBUG:main: this is a debug message
47 /// WARN:main: this is a warning message
48 /// 6:main: this is a custom logging level: 6
52 ($lvl
:expr
, $
($arg
:tt
)+) => ({
53 static LOC
: ::log
::LogLocation
= ::log
::LogLocation
{
56 module_path
: module_path
!(),
59 if log_enabled
!(lvl
) {
60 ::log
::log(lvl
, &LOC
, format_args
!($
($arg
)+))
65 /// A convenience macro for logging at the error log level.
70 /// #[macro_use] extern crate log;
74 /// error!("the build has failed with error code: {}", error);
78 /// Assumes the binary is `main`:
81 /// $ RUST_LOG=error ./main
82 /// ERROR:main: the build has failed with error code: 3
87 ($
($arg
:tt
)*) => (log
!(::log
::ERROR
, $
($arg
)*))
90 /// A convenience macro for logging at the warning log level.
95 /// #[macro_use] extern crate log;
99 /// warn!("you may like to know that a process exited with: {}", code);
103 /// Assumes the binary is `main`:
106 /// $ RUST_LOG=warn ./main
107 /// WARN:main: you may like to know that a process exited with: 3
111 ($
($arg
:tt
)*) => (log
!(::log
::WARN
, $
($arg
)*))
114 /// A convenience macro for logging at the info log level.
119 /// #[macro_use] extern crate log;
123 /// info!("this function is about to return: {}", ret);
127 /// Assumes the binary is `main`:
130 /// $ RUST_LOG=info ./main
131 /// INFO:main: this function is about to return: 3
135 ($
($arg
:tt
)*) => (log
!(::log
::INFO
, $
($arg
)*))
138 /// A convenience macro for logging at the debug log level. This macro can also
139 /// be omitted at compile time by passing `-C debug-assertions` to the compiler. If
140 /// this option is not passed, then debug statements will be compiled.
145 /// #[macro_use] extern crate log;
148 /// debug!("x = {x}, y = {y}", x=10, y=20);
152 /// Assumes the binary is `main`:
155 /// $ RUST_LOG=debug ./main
156 /// DEBUG:main: x = 10, y = 20
160 ($
($arg
:tt
)*) => (if cfg
!(debug_assertions
) { log!(::log::DEBUG, $($arg)*) }
)
163 /// A macro to test whether a log level is enabled for the current module.
168 /// #[macro_use] extern crate log;
170 /// struct Point { x: int, y: int }
171 /// fn some_expensive_computation() -> Point { Point { x: 1, y: 2 } }
174 /// if log_enabled!(log::DEBUG) {
175 /// let x = some_expensive_computation();
176 /// debug!("x.x = {}, x.y = {}", x.x, x.y);
181 /// Assumes the binary is `main`:
184 /// $ RUST_LOG=error ./main
188 /// $ RUST_LOG=debug ./main
189 /// DEBUG:main: x.x = 1, x.y = 2
192 macro_rules
! log_enabled
{
195 (lvl
!= ::log
::DEBUG
|| cfg
!(debug_assertions
)) &&
196 lvl
<= ::log
::log_level() &&
197 ::log
::mod_enabled(lvl
, module_path
!())