| 1 | // Copyright 2014-2015 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. |
| 4 | // |
| 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. |
| 10 | |
| 11 | /// The standard logging macro. |
| 12 | /// |
| 13 | /// This macro will generically log with the specified `Level` and `format!` |
| 14 | /// based argument list. |
| 15 | /// |
| 16 | /// # Examples |
| 17 | /// |
| 18 | /// ``` |
| 19 | /// use log::{log, Level}; |
| 20 | /// |
| 21 | /// # fn main() { |
| 22 | /// let data = (42, "Forty-two" ); |
| 23 | /// let private_data = "private" ; |
| 24 | /// |
| 25 | /// log!(Level::Error, "Received errors: {}, {}" , data.0, data.1); |
| 26 | /// log!(target: "app_events" , Level::Warn, "App warning: {}, {}, {}" , |
| 27 | /// data.0, data.1, private_data); |
| 28 | /// # } |
| 29 | /// ``` |
| 30 | #[macro_export ] |
| 31 | macro_rules! log { |
| 32 | // log!(target: "my_target", Level::Info, key1:? = 42, key2 = true; "a {} event", "log"); |
| 33 | (target: $target:expr, $lvl:expr, $($key:tt $(:$capture:tt)? $(= $value:expr)?),+; $($arg:tt)+) => ({ |
| 34 | let lvl = $lvl; |
| 35 | if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() { |
| 36 | $crate::__private_api::log::<&_>( |
| 37 | $crate::__private_api::format_args!($($arg)+), |
| 38 | lvl, |
| 39 | &($target, $crate::__private_api::module_path!(), $crate::__private_api::loc()), |
| 40 | &[$(($crate::__log_key!($key), $crate::__log_value!($key $(:$capture)* = $($value)*))),+] |
| 41 | ); |
| 42 | } |
| 43 | }); |
| 44 | |
| 45 | // log!(target: "my_target", Level::Info, "a {} event", "log"); |
| 46 | (target: $target:expr, $lvl:expr, $($arg:tt)+) => ({ |
| 47 | let lvl = $lvl; |
| 48 | if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() { |
| 49 | $crate::__private_api::log( |
| 50 | $crate::__private_api::format_args!($($arg)+), |
| 51 | lvl, |
| 52 | &($target, $crate::__private_api::module_path!(), $crate::__private_api::loc()), |
| 53 | (), |
| 54 | ); |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | // log!(Level::Info, "a log event") |
| 59 | ($lvl:expr, $($arg:tt)+) => ($crate::log!(target: $crate::__private_api::module_path!(), $lvl, $($arg)+)); |
| 60 | } |
| 61 | |
| 62 | /// Logs a message at the error level. |
| 63 | /// |
| 64 | /// # Examples |
| 65 | /// |
| 66 | /// ``` |
| 67 | /// use log::error; |
| 68 | /// |
| 69 | /// # fn main() { |
| 70 | /// let (err_info, port) = ("No connection" , 22); |
| 71 | /// |
| 72 | /// error!("Error: {err_info} on port {port}" ); |
| 73 | /// error!(target: "app_events" , "App Error: {err_info}, Port: {port}" ); |
| 74 | /// # } |
| 75 | /// ``` |
| 76 | #[macro_export ] |
| 77 | macro_rules! error { |
| 78 | // error!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log") |
| 79 | // error!(target: "my_target", "a {} event", "log") |
| 80 | (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Error, $($arg)+)); |
| 81 | |
| 82 | // error!("a {} event", "log") |
| 83 | ($($arg:tt)+) => ($crate::log!($crate::Level::Error, $($arg)+)) |
| 84 | } |
| 85 | |
| 86 | /// Logs a message at the warn level. |
| 87 | /// |
| 88 | /// # Examples |
| 89 | /// |
| 90 | /// ``` |
| 91 | /// use log::warn; |
| 92 | /// |
| 93 | /// # fn main() { |
| 94 | /// let warn_description = "Invalid Input" ; |
| 95 | /// |
| 96 | /// warn!("Warning! {warn_description}!" ); |
| 97 | /// warn!(target: "input_events" , "App received warning: {warn_description}" ); |
| 98 | /// # } |
| 99 | /// ``` |
| 100 | #[macro_export ] |
| 101 | macro_rules! warn { |
| 102 | // warn!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log") |
| 103 | // warn!(target: "my_target", "a {} event", "log") |
| 104 | (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Warn, $($arg)+)); |
| 105 | |
| 106 | // warn!("a {} event", "log") |
| 107 | ($($arg:tt)+) => ($crate::log!($crate::Level::Warn, $($arg)+)) |
| 108 | } |
| 109 | |
| 110 | /// Logs a message at the info level. |
| 111 | /// |
| 112 | /// # Examples |
| 113 | /// |
| 114 | /// ``` |
| 115 | /// use log::info; |
| 116 | /// |
| 117 | /// # fn main() { |
| 118 | /// # struct Connection { port: u32, speed: f32 } |
| 119 | /// let conn_info = Connection { port: 40, speed: 3.20 }; |
| 120 | /// |
| 121 | /// info!("Connected to port {} at {} Mb/s" , conn_info.port, conn_info.speed); |
| 122 | /// info!(target: "connection_events" , "Successful connection, port: {}, speed: {}" , |
| 123 | /// conn_info.port, conn_info.speed); |
| 124 | /// # } |
| 125 | /// ``` |
| 126 | #[macro_export ] |
| 127 | macro_rules! info { |
| 128 | // info!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log") |
| 129 | // info!(target: "my_target", "a {} event", "log") |
| 130 | (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Info, $($arg)+)); |
| 131 | |
| 132 | // info!("a {} event", "log") |
| 133 | ($($arg:tt)+) => ($crate::log!($crate::Level::Info, $($arg)+)) |
| 134 | } |
| 135 | |
| 136 | /// Logs a message at the debug level. |
| 137 | /// |
| 138 | /// # Examples |
| 139 | /// |
| 140 | /// ``` |
| 141 | /// use log::debug; |
| 142 | /// |
| 143 | /// # fn main() { |
| 144 | /// # struct Position { x: f32, y: f32 } |
| 145 | /// let pos = Position { x: 3.234, y: -1.223 }; |
| 146 | /// |
| 147 | /// debug!("New position: x: {}, y: {}" , pos.x, pos.y); |
| 148 | /// debug!(target: "app_events" , "New position: x: {}, y: {}" , pos.x, pos.y); |
| 149 | /// # } |
| 150 | /// ``` |
| 151 | #[macro_export ] |
| 152 | macro_rules! debug { |
| 153 | // debug!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log") |
| 154 | // debug!(target: "my_target", "a {} event", "log") |
| 155 | (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Debug, $($arg)+)); |
| 156 | |
| 157 | // debug!("a {} event", "log") |
| 158 | ($($arg:tt)+) => ($crate::log!($crate::Level::Debug, $($arg)+)) |
| 159 | } |
| 160 | |
| 161 | /// Logs a message at the trace level. |
| 162 | /// |
| 163 | /// # Examples |
| 164 | /// |
| 165 | /// ``` |
| 166 | /// use log::trace; |
| 167 | /// |
| 168 | /// # fn main() { |
| 169 | /// # struct Position { x: f32, y: f32 } |
| 170 | /// let pos = Position { x: 3.234, y: -1.223 }; |
| 171 | /// |
| 172 | /// trace!("Position is: x: {}, y: {}" , pos.x, pos.y); |
| 173 | /// trace!(target: "app_events" , "x is {} and y is {}" , |
| 174 | /// if pos.x >= 0.0 { "positive" } else { "negative" }, |
| 175 | /// if pos.y >= 0.0 { "positive" } else { "negative" }); |
| 176 | /// # } |
| 177 | /// ``` |
| 178 | #[macro_export ] |
| 179 | macro_rules! trace { |
| 180 | // trace!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log") |
| 181 | // trace!(target: "my_target", "a {} event", "log") |
| 182 | (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Trace, $($arg)+)); |
| 183 | |
| 184 | // trace!("a {} event", "log") |
| 185 | ($($arg:tt)+) => ($crate::log!($crate::Level::Trace, $($arg)+)) |
| 186 | } |
| 187 | |
| 188 | /// Determines if a message logged at the specified level in that module will |
| 189 | /// be logged. |
| 190 | /// |
| 191 | /// This can be used to avoid expensive computation of log message arguments if |
| 192 | /// the message would be ignored anyway. |
| 193 | /// |
| 194 | /// # Examples |
| 195 | /// |
| 196 | /// ``` |
| 197 | /// use log::Level::Debug; |
| 198 | /// use log::{debug, log_enabled}; |
| 199 | /// |
| 200 | /// # fn foo() { |
| 201 | /// if log_enabled!(Debug) { |
| 202 | /// let data = expensive_call(); |
| 203 | /// debug!("expensive debug data: {} {}" , data.x, data.y); |
| 204 | /// } |
| 205 | /// if log_enabled!(target: "Global" , Debug) { |
| 206 | /// let data = expensive_call(); |
| 207 | /// debug!(target: "Global" , "expensive debug data: {} {}" , data.x, data.y); |
| 208 | /// } |
| 209 | /// # } |
| 210 | /// # struct Data { x: u32, y: u32 } |
| 211 | /// # fn expensive_call() -> Data { Data { x: 0, y: 0 } } |
| 212 | /// # fn main() {} |
| 213 | /// ``` |
| 214 | #[macro_export ] |
| 215 | macro_rules! log_enabled { |
| 216 | (target: $target:expr, $lvl:expr) => {{ |
| 217 | let lvl = $lvl; |
| 218 | lvl <= $crate::STATIC_MAX_LEVEL |
| 219 | && lvl <= $crate::max_level() |
| 220 | && $crate::__private_api::enabled(lvl, $target) |
| 221 | }}; |
| 222 | ($lvl:expr) => { |
| 223 | $crate::log_enabled!(target: $crate::__private_api::module_path!(), $lvl) |
| 224 | }; |
| 225 | } |
| 226 | |
| 227 | // These macros use a pattern of #[cfg]s to produce nicer error |
| 228 | // messages when log features aren't available |
| 229 | |
| 230 | #[doc (hidden)] |
| 231 | #[macro_export ] |
| 232 | #[cfg (feature = "kv" )] |
| 233 | macro_rules! __log_key { |
| 234 | // key1 = 42 |
| 235 | ($($args:ident)*) => { |
| 236 | $crate::__private_api::stringify!($($args)*) |
| 237 | }; |
| 238 | // "key1" = 42 |
| 239 | ($($args:expr)*) => { |
| 240 | $($args)* |
| 241 | }; |
| 242 | } |
| 243 | |
| 244 | #[doc (hidden)] |
| 245 | #[macro_export ] |
| 246 | #[cfg (not(feature = "kv" ))] |
| 247 | macro_rules! __log_key { |
| 248 | ($($args:tt)*) => { |
| 249 | compile_error!("key value support requires the `kv` feature of `log`" ) |
| 250 | }; |
| 251 | } |
| 252 | |
| 253 | #[doc (hidden)] |
| 254 | #[macro_export ] |
| 255 | #[cfg (feature = "kv" )] |
| 256 | macro_rules! __log_value { |
| 257 | // Entrypoint |
| 258 | ($key:tt = $args:expr) => { |
| 259 | $crate::__log_value!(($args):value) |
| 260 | }; |
| 261 | ($key:tt :$capture:tt = $args:expr) => { |
| 262 | $crate::__log_value!(($args):$capture) |
| 263 | }; |
| 264 | ($key:ident =) => { |
| 265 | $crate::__log_value!(($key):value) |
| 266 | }; |
| 267 | ($key:ident :$capture:tt =) => { |
| 268 | $crate::__log_value!(($key):$capture) |
| 269 | }; |
| 270 | // ToValue |
| 271 | (($args:expr):value) => { |
| 272 | $crate::__private_api::capture_to_value(&&$args) |
| 273 | }; |
| 274 | // Debug |
| 275 | (($args:expr):?) => { |
| 276 | $crate::__private_api::capture_debug(&&$args) |
| 277 | }; |
| 278 | (($args:expr):debug) => { |
| 279 | $crate::__private_api::capture_debug(&&$args) |
| 280 | }; |
| 281 | // Display |
| 282 | (($args:expr):%) => { |
| 283 | $crate::__private_api::capture_display(&&$args) |
| 284 | }; |
| 285 | (($args:expr):display) => { |
| 286 | $crate::__private_api::capture_display(&&$args) |
| 287 | }; |
| 288 | //Error |
| 289 | (($args:expr):err) => { |
| 290 | $crate::__log_value_error!($args) |
| 291 | }; |
| 292 | // sval::Value |
| 293 | (($args:expr):sval) => { |
| 294 | $crate::__log_value_sval!($args) |
| 295 | }; |
| 296 | // serde::Serialize |
| 297 | (($args:expr):serde) => { |
| 298 | $crate::__log_value_serde!($args) |
| 299 | }; |
| 300 | } |
| 301 | |
| 302 | #[doc (hidden)] |
| 303 | #[macro_export ] |
| 304 | #[cfg (not(feature = "kv" ))] |
| 305 | macro_rules! __log_value { |
| 306 | ($($args:tt)*) => { |
| 307 | compile_error!("key value support requires the `kv` feature of `log`" ) |
| 308 | }; |
| 309 | } |
| 310 | |
| 311 | #[doc (hidden)] |
| 312 | #[macro_export ] |
| 313 | #[cfg (feature = "kv_sval" )] |
| 314 | macro_rules! __log_value_sval { |
| 315 | ($args:expr) => { |
| 316 | $crate::__private_api::capture_sval(&&$args) |
| 317 | }; |
| 318 | } |
| 319 | |
| 320 | #[doc (hidden)] |
| 321 | #[macro_export ] |
| 322 | #[cfg (not(feature = "kv_sval" ))] |
| 323 | macro_rules! __log_value_sval { |
| 324 | ($args:expr) => { |
| 325 | compile_error!("capturing values as `sval::Value` requites the `kv_sval` feature of `log`" ) |
| 326 | }; |
| 327 | } |
| 328 | |
| 329 | #[doc (hidden)] |
| 330 | #[macro_export ] |
| 331 | #[cfg (feature = "kv_serde" )] |
| 332 | macro_rules! __log_value_serde { |
| 333 | ($args:expr) => { |
| 334 | $crate::__private_api::capture_serde(&&$args) |
| 335 | }; |
| 336 | } |
| 337 | |
| 338 | #[doc (hidden)] |
| 339 | #[macro_export ] |
| 340 | #[cfg (not(feature = "kv_serde" ))] |
| 341 | macro_rules! __log_value_serde { |
| 342 | ($args:expr) => { |
| 343 | compile_error!( |
| 344 | "capturing values as `serde::Serialize` requites the `kv_serde` feature of `log`" |
| 345 | ) |
| 346 | }; |
| 347 | } |
| 348 | |
| 349 | #[doc (hidden)] |
| 350 | #[macro_export ] |
| 351 | #[cfg (feature = "kv_std" )] |
| 352 | macro_rules! __log_value_error { |
| 353 | ($args:expr) => { |
| 354 | $crate::__private_api::capture_error(&$args) |
| 355 | }; |
| 356 | } |
| 357 | |
| 358 | #[doc (hidden)] |
| 359 | #[macro_export ] |
| 360 | #[cfg (not(feature = "kv_std" ))] |
| 361 | macro_rules! __log_value_error { |
| 362 | ($args:expr) => { |
| 363 | compile_error!( |
| 364 | "capturing values as `std::error::Error` requites the `kv_std` feature of `log`" |
| 365 | ) |
| 366 | }; |
| 367 | } |
| 368 | |