1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! Logging macros /// The standard logging macro /// /// This macro will generically log over a provided level (of type u32) with a /// format!-based argument list. See documentation in `std::fmt` for details on /// how to use the syntax. /// /// # Example /// /// ``` /// #[macro_use] extern crate log; /// /// fn main() { /// log!(log::WARN, "this is a warning {}", "message"); /// log!(log::DEBUG, "this is a debug message"); /// log!(6, "this is a custom logging level: {level}", level=6u); /// } /// ``` /// /// Assumes the binary is `main`: /// /// ```{.bash} /// $ RUST_LOG=warn ./main /// WARN:main: this is a warning message /// ``` /// /// ```{.bash} /// $ RUST_LOG=debug ./main /// DEBUG:main: this is a debug message /// WARN:main: this is a warning message /// ``` /// /// ```{.bash} /// $ RUST_LOG=6 ./main /// DEBUG:main: this is a debug message /// WARN:main: this is a warning message /// 6:main: this is a custom logging level: 6 /// ``` #[macro_export] macro_rules! log { ($lvl:expr, $($arg:tt)+) => ({ static LOC: ::log::LogLocation = ::log::LogLocation { line: line!(), file: file!(), module_path: module_path!(), }; let lvl = $lvl; if log_enabled!(lvl) { ::log::log(lvl, &LOC, format_args!($($arg)+)); } }) } /// A convenience macro for logging at the error log level. /// /// # Example /// /// ``` /// #[macro_use] extern crate log; /// /// fn main() { /// let error = 3u; /// error!("the build has failed with error code: {}", error); /// } /// ``` /// /// Assumes the binary is `main`: /// /// ```{.bash} /// $ RUST_LOG=error ./main /// ERROR:main: the build has failed with error code: 3 /// ``` /// #[macro_export] macro_rules! error { ($($arg:tt)*) => (log!(::log::ERROR, $($arg)*)) } /// A convenience macro for logging at the warning log level. /// /// # Example /// /// ``` /// #[macro_use] extern crate log; /// /// fn main() { /// let code = 3u; /// warn!("you may like to know that a process exited with: {}", code); /// } /// ``` /// /// Assumes the binary is `main`: /// /// ```{.bash} /// $ RUST_LOG=warn ./main /// WARN:main: you may like to know that a process exited with: 3 /// ``` #[macro_export] macro_rules! warn { ($($arg:tt)*) => (log!(::log::WARN, $($arg)*)) } /// A convenience macro for logging at the info log level. /// /// # Example /// /// ``` /// #[macro_use] extern crate log; /// /// fn main() { /// let ret = 3i; /// info!("this function is about to return: {}", ret); /// } /// ``` /// /// Assumes the binary is `main`: /// /// ```{.bash} /// $ RUST_LOG=info ./main /// INFO:main: this function is about to return: 3 /// ``` #[macro_export] macro_rules! info { ($($arg:tt)*) => (log!(::log::INFO, $($arg)*)) } /// A convenience macro for logging at the debug log level. This macro can also /// be omitted at compile time by passing `--cfg ndebug` to the compiler. If /// this option is not passed, then debug statements will be compiled. /// /// # Example /// /// ``` /// #[macro_use] extern crate log; /// /// fn main() { /// debug!("x = {x}, y = {y}", x=10i, y=20i); /// } /// ``` /// /// Assumes the binary is `main`: /// /// ```{.bash} /// $ RUST_LOG=debug ./main /// DEBUG:main: x = 10, y = 20 /// ``` #[macro_export] macro_rules! debug { ($($arg:tt)*) => (if cfg!(not(ndebug)) { log!(::log::DEBUG, $($arg)*) }) } /// A macro to test whether a log level is enabled for the current module. /// /// # Example /// /// ``` /// #[macro_use] extern crate log; /// /// struct Point { x: int, y: int } /// fn some_expensive_computation() -> Point { Point { x: 1, y: 2 } } /// /// fn main() { /// if log_enabled!(log::DEBUG) { /// let x = some_expensive_computation(); /// debug!("x.x = {}, x.y = {}", x.x, x.y); /// } /// } /// ``` /// /// Assumes the binary is `main`: /// /// ```{.bash} /// $ RUST_LOG=error ./main /// ``` /// /// ```{.bash} /// $ RUST_LOG=debug ./main /// DEBUG:main: x.x = 1, x.y = 2 /// ``` #[macro_export] macro_rules! log_enabled { ($lvl:expr) => ({ let lvl = $lvl; (lvl != ::log::DEBUG || cfg!(not(ndebug))) && lvl <= ::log::log_level() && ::log::mod_enabled(lvl, module_path!()) }) }