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.
15 /// The standard logging macro
17 /// This macro will generically log over a provided level (of type u32) with a
18 /// format!-based argument list. See documentation in `std::fmt` for details on
19 /// how to use the syntax.
24 /// #![feature(phase)]
25 /// #[phase(plugin, link)] extern crate log;
28 /// log!(log::WARN, "this is a warning {}", "message");
29 /// log!(log::DEBUG, "this is a debug message");
30 /// log!(6, "this is a custom logging level: {level}", level=6u);
34 /// Assumes the binary is `main`:
37 /// $ RUST_LOG=warn ./main
38 /// WARN:main: this is a warning message
42 /// $ RUST_LOG=debug ./main
43 /// DEBUG:main: this is a debug message
44 /// WARN:main: this is a warning message
48 /// $ RUST_LOG=6 ./main
49 /// DEBUG:main: this is a debug message
50 /// WARN:main: this is a warning message
51 /// 6:main: this is a custom logging level: 6
55 ($lvl:expr, $($arg:tt)+) => ({
56 static LOC: ::log::LogLocation = ::log::LogLocation {
59 module_path: module_path!(),
62 if log_enabled!(lvl) {
63 ::log::log(lvl, &LOC, format_args!($($arg)+))
68 /// A convenience macro for logging at the error log level.
73 /// #![feature(phase)]
74 /// #[phase(plugin, link)] extern crate log;
78 /// error!("the build has failed with error code: {}", error);
82 /// Assumes the binary is `main`:
85 /// $ RUST_LOG=error ./main
86 /// ERROR:main: the build has failed with error code: 3
91 ($($arg:tt)*) => (log!(::log::ERROR, $($arg)*))
94 /// A convenience macro for logging at the warning log level.
99 /// #![feature(phase)]
100 /// #[phase(plugin, link)] extern crate log;
104 /// warn!("you may like to know that a process exited with: {}", code);
108 /// Assumes the binary is `main`:
111 /// $ RUST_LOG=warn ./main
112 /// WARN:main: you may like to know that a process exited with: 3
116 ($($arg:tt)*) => (log!(::log::WARN, $($arg)*))
119 /// A convenience macro for logging at the info log level.
124 /// #![feature(phase)]
125 /// #[phase(plugin, link)] extern crate log;
129 /// info!("this function is about to return: {}", ret);
133 /// Assumes the binary is `main`:
136 /// $ RUST_LOG=info ./main
137 /// INFO:main: this function is about to return: 3
141 ($($arg:tt)*) => (log!(::log::INFO, $($arg)*))
144 /// A convenience macro for logging at the debug log level. This macro can also
145 /// be omitted at compile time by passing `--cfg ndebug` to the compiler. If
146 /// this option is not passed, then debug statements will be compiled.
151 /// #![feature(phase)]
152 /// #[phase(plugin, link)] extern crate log;
155 /// debug!("x = {x}, y = {y}", x=10, y=20);
159 /// Assumes the binary is `main`:
162 /// $ RUST_LOG=debug ./main
163 /// DEBUG:main: x = 10, y = 20
167 ($($arg:tt)*) => (if cfg!(not(ndebug)) { log!(::log::DEBUG, $($arg)*) })
170 /// A macro to test whether a log level is enabled for the current module.
175 /// #![feature(phase)]
176 /// #[phase(plugin, link)] extern crate log;
178 /// struct Point { x: int, y: int }
179 /// fn some_expensive_computation() -> Point { Point { x: 1, y: 2 } }
182 /// if log_enabled!(log::DEBUG) {
183 /// let x = some_expensive_computation();
184 /// debug!("x.x = {}, x.y = {}", x.x, x.y);
189 /// Assumes the binary is `main`:
192 /// $ RUST_LOG=error ./main
196 /// $ RUST_LOG=debug ./main
197 /// DEBUG:main: x.x = 1, x.y = 2
200 macro_rules! log_enabled {
203 (lvl != ::log::DEBUG || cfg!(not(ndebug))) &&
204 lvl <= ::log::log_level() &&
205 ::log::mod_enabled(lvl, module_path!())