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 /// # #![feature(rustc_private)]
23 /// #[macro_use] extern crate log;
26 /// log!(log::WARN, "this is a warning {}", "message");
27 /// log!(log::DEBUG, "this is a debug message");
28 /// log!(6, "this is a custom logging level: {level}", level=6);
32 /// Assumes the binary is `main`:
35 /// $ RUST_LOG=warn ./main
36 /// WARN:main: this is a warning message
40 /// $ RUST_LOG=debug ./main
41 /// DEBUG:main: this is a debug message
42 /// WARN:main: this is a warning message
46 /// $ RUST_LOG=6 ./main
47 /// DEBUG:main: this is a debug message
48 /// WARN:main: this is a warning message
49 /// 6:main: this is a custom logging level: 6
53 ($lvl
:expr
, $
($arg
:tt
)+) => ({
54 static LOC
: ::log
::LogLocation
= ::log
::LogLocation
{
57 module_path
: module_path
!(),
60 if log_enabled
!(lvl
) {
61 ::log
::log(lvl
, &LOC
, format_args
!($
($arg
)+))
66 /// A convenience macro for logging at the error log level.
71 /// # #![feature(rustc_private)]
72 /// #[macro_use] extern crate log;
76 /// error!("the build has failed with error code: {}", error);
80 /// Assumes the binary is `main`:
83 /// $ RUST_LOG=error ./main
84 /// ERROR:main: the build has failed with error code: 3
89 ($
($arg
:tt
)*) => (log
!(::log
::ERROR
, $
($arg
)*))
92 /// A convenience macro for logging at the warning log level.
97 /// # #![feature(rustc_private)]
98 /// #[macro_use] extern crate log;
102 /// warn!("you may like to know that a process exited with: {}", code);
106 /// Assumes the binary is `main`:
109 /// $ RUST_LOG=warn ./main
110 /// WARN:main: you may like to know that a process exited with: 3
114 ($
($arg
:tt
)*) => (log
!(::log
::WARN
, $
($arg
)*))
117 /// A convenience macro for logging at the info log level.
122 /// # #![feature(rustc_private)]
123 /// #[macro_use] extern crate log;
127 /// info!("this function is about to return: {}", ret);
131 /// Assumes the binary is `main`:
134 /// $ RUST_LOG=info ./main
135 /// INFO:main: this function is about to return: 3
139 ($
($arg
:tt
)*) => (log
!(::log
::INFO
, $
($arg
)*))
142 /// A convenience macro for logging at the debug log level. This macro will
143 /// be omitted at compile time in an optimized build unless `-C debug-assertions`
144 /// is passed to the compiler.
149 /// # #![feature(rustc_private)]
150 /// #[macro_use] extern crate log;
153 /// debug!("x = {x}, y = {y}", x=10, y=20);
157 /// Assumes the binary is `main`:
160 /// $ RUST_LOG=debug ./main
161 /// DEBUG:main: x = 10, y = 20
165 ($
($arg
:tt
)*) => (if cfg
!(debug_assertions
) { log!(::log::DEBUG, $($arg)*) }
)
168 /// A macro to test whether a log level is enabled for the current module.
173 /// # #![feature(rustc_private)]
174 /// #[macro_use] extern crate log;
176 /// struct Point { x: i32, y: i32 }
177 /// fn some_expensive_computation() -> Point { Point { x: 1, y: 2 } }
180 /// if log_enabled!(log::DEBUG) {
181 /// let x = some_expensive_computation();
182 /// debug!("x.x = {}, x.y = {}", x.x, x.y);
187 /// Assumes the binary is `main`:
190 /// $ RUST_LOG=error ./main
194 /// $ RUST_LOG=debug ./main
195 /// DEBUG:main: x.x = 1, x.y = 2
198 macro_rules
! log_enabled
{
201 (lvl
!= ::log
::DEBUG
|| cfg
!(debug_assertions
)) &&
202 lvl
<= ::log
::log_level() &&
203 ::log
::mod_enabled(lvl
, module_path
!())