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