66
66
#[ macro_export]
67
67
#[ clippy:: format_args]
68
68
macro_rules! log {
69
+ // log!(logger: my_logger, target: "my_target", Level::Info, "a {} event", "log");
70
+ ( logger: $logger: expr, target: $target: expr, $lvl: expr, $( $arg: tt) +) => ( {
71
+ $crate:: __log!(
72
+ logger: $crate:: __log_logger!( $logger) ,
73
+ target: $target,
74
+ $lvl,
75
+ $( $arg) +
76
+ )
77
+ } ) ;
78
+
79
+ // log!(logger: my_logger, Level::Info, "a log event")
80
+ ( logger: $logger: expr, $lvl: expr, $( $arg: tt) +) => ( {
81
+ $crate:: __log!(
82
+ logger: $crate:: __log_logger!( $logger) ,
83
+ target: $crate:: __private_api:: module_path!( ) ,
84
+ $lvl,
85
+ $( $arg) +
86
+ )
87
+ } ) ;
88
+
89
+ // log!(target: "my_target", Level::Info, "a log event")
90
+ ( target: $target: expr, $lvl: expr, $( $arg: tt) +) => ( {
91
+ $crate:: __log!(
92
+ logger: $crate:: __log_logger!( __log_global_logger) ,
93
+ target: $target,
94
+ $lvl,
95
+ $( $arg) +
96
+ )
97
+ } ) ;
98
+
99
+ // log!(Level::Info, "a log event")
100
+ ( $lvl: expr, $( $arg: tt) +) => ( {
101
+ $crate:: __log!(
102
+ logger: $crate:: __log_logger!( __log_global_logger) ,
103
+ target: $crate:: __private_api:: module_path!( ) ,
104
+ $lvl,
105
+ $( $arg) +
106
+ )
107
+ } ) ;
108
+ }
109
+
110
+ #[ doc( hidden) ]
111
+ #[ macro_export]
112
+ macro_rules! __log {
69
113
// log!(logger: my_logger, target: "my_target", Level::Info, key1:? = 42, key2 = true; "a {} event", "log");
70
114
( logger: $logger: expr, target: $target: expr, $lvl: expr, $( $key: tt $( : $capture: tt) ? $( = $value: expr) ?) ,+; $( $arg: tt) +) => ( {
71
115
let lvl = $lvl;
72
116
if lvl <= $crate:: STATIC_MAX_LEVEL && lvl <= $crate:: max_level( ) {
73
- $crate:: __private_api:: log:: < & _ , _> (
74
- & ( $logger) ,
117
+ $crate:: __private_api:: log(
118
+ $logger,
75
119
$crate:: __private_api:: format_args!( $( $arg) +) ,
76
120
lvl,
77
121
& ( $target, $crate:: __private_api:: module_path!( ) , $crate:: __private_api:: loc( ) ) ,
@@ -85,29 +129,14 @@ macro_rules! log {
85
129
let lvl = $lvl;
86
130
if lvl <= $crate:: STATIC_MAX_LEVEL && lvl <= $crate:: max_level( ) {
87
131
$crate:: __private_api:: log(
88
- & ( $logger) ,
132
+ $logger,
89
133
$crate:: __private_api:: format_args!( $( $arg) +) ,
90
134
lvl,
91
135
& ( $target, $crate:: __private_api:: module_path!( ) , $crate:: __private_api:: loc( ) ) ,
92
136
( ) ,
93
137
) ;
94
138
}
95
139
} ) ;
96
-
97
- // log!(logger: my_logger, Level::Info, "a log event")
98
- ( logger: $logger: expr, $lvl: expr, $( $arg: tt) +) => ( {
99
- $crate:: log!( logger: $logger, target: $crate:: __private_api:: module_path!( ) , $lvl, $( $arg) +)
100
- } ) ;
101
-
102
- // log!(target: "my_target", Level::Info, "a log event")
103
- ( target: $target: expr, $lvl: expr, $( $arg: tt) +) => ( {
104
- $crate:: log!( logger: $crate:: __private_api:: GlobalLogger , target: $target, $lvl, $( $arg) +)
105
- } ) ;
106
-
107
- // log!(Level::Info, "a log event")
108
- ( $lvl: expr, $( $arg: tt) +) => ( {
109
- $crate:: log!( target: $crate:: __private_api:: module_path!( ) , $lvl, $( $arg) +)
110
- } ) ;
111
140
}
112
141
113
142
/// Logs a message at the error level.
@@ -130,13 +159,13 @@ macro_rules! error {
130
159
// error!(logger: my_logger, target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
131
160
// error!(logger: my_logger, target: "my_target", "a {} event", "log")
132
161
( logger: $logger: expr, target: $target: expr, $( $arg: tt) +) => ( {
133
- $crate:: log!( logger: $logger, target: $target, $crate:: Level :: Error , $( $arg) +)
162
+ $crate:: log!( logger: $crate :: __log_logger! ( $ logger) , target: $target, $crate:: Level :: Error , $( $arg) +)
134
163
} ) ;
135
164
136
165
// error!(logger: my_logger, key1 = 42, key2 = true; "a {} event", "log")
137
166
// error!(logger: my_logger, "a {} event", "log")
138
167
( logger: $logger: expr, $( $arg: tt) +) => ( {
139
- $crate:: log!( logger: $logger, $crate:: Level :: Error , $( $arg) +)
168
+ $crate:: log!( logger: $crate :: __log_logger! ( $ logger) , $crate:: Level :: Error , $( $arg) +)
140
169
} ) ;
141
170
142
171
// error!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
@@ -169,13 +198,13 @@ macro_rules! warn {
169
198
// warn!(logger: my_logger, target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
170
199
// warn!(logger: my_logger, target: "my_target", "a {} event", "log")
171
200
( logger: $logger: expr, target: $target: expr, $( $arg: tt) +) => ( {
172
- $crate:: log!( logger: $logger, target: $target, $crate:: Level :: Warn , $( $arg) +)
201
+ $crate:: log!( logger: $crate :: __log_logger! ( $ logger) , target: $target, $crate:: Level :: Warn , $( $arg) +)
173
202
} ) ;
174
203
175
204
// warn!(logger: my_logger, key1 = 42, key2 = true; "a {} event", "log")
176
205
// warn!(logger: my_logger, "a {} event", "log")
177
206
( logger: $logger: expr, $( $arg: tt) +) => ( {
178
- $crate:: log!( logger: $logger, $crate:: Level :: Warn , $( $arg) +)
207
+ $crate:: log!( logger: $crate :: __log_logger! ( $ logger) , $crate:: Level :: Warn , $( $arg) +)
179
208
} ) ;
180
209
181
210
// warn!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
@@ -217,13 +246,13 @@ macro_rules! info {
217
246
// info!(logger: my_logger, target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
218
247
// info!(logger: my_logger, target: "my_target", "a {} event", "log")
219
248
( logger: $logger: expr, target: $target: expr, $( $arg: tt) +) => ( {
220
- $crate:: log!( logger: $logger, target: $target, $crate:: Level :: Info , $( $arg) +)
249
+ $crate:: log!( logger: $crate :: __log_logger! ( $ logger) , target: $target, $crate:: Level :: Info , $( $arg) +)
221
250
} ) ;
222
251
223
252
// info!(logger: my_logger, key1 = 42, key2 = true; "a {} event", "log")
224
253
// info!(logger: my_logger, "a {} event", "log")
225
254
( logger: $logger: expr, $( $arg: tt) +) => ( {
226
- $crate:: log!( logger: $logger, $crate:: Level :: Info , $( $arg) +)
255
+ $crate:: log!( logger: $crate :: __log_logger! ( $ logger) , $crate:: Level :: Info , $( $arg) +)
227
256
} ) ;
228
257
229
258
// info!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
@@ -257,13 +286,13 @@ macro_rules! debug {
257
286
// debug!(logger: my_logger, target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
258
287
// debug!(logger: my_logger, target: "my_target", "a {} event", "log")
259
288
( logger: $logger: expr, target: $target: expr, $( $arg: tt) +) => ( {
260
- $crate:: log!( logger: $logger, target: $target, $crate:: Level :: Debug , $( $arg) +)
289
+ $crate:: log!( logger: $crate :: __log_logger! ( $ logger) , target: $target, $crate:: Level :: Debug , $( $arg) +)
261
290
} ) ;
262
291
263
292
// debug!(logger: my_logger, key1 = 42, key2 = true; "a {} event", "log")
264
293
// debug!(logger: my_logger, "a {} event", "log")
265
294
( logger: $logger: expr, $( $arg: tt) +) => ( {
266
- $crate:: log!( logger: $logger, $crate:: Level :: Debug , $( $arg) +)
295
+ $crate:: log!( logger: $crate :: __log_logger! ( $ logger) , $crate:: Level :: Debug , $( $arg) +)
267
296
} ) ;
268
297
269
298
// debug!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
@@ -301,13 +330,13 @@ macro_rules! trace {
301
330
// trace!(logger: my_logger, target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
302
331
// trace!(logger: my_logger, target: "my_target", "a {} event", "log")
303
332
( logger: $logger: expr, target: $target: expr, $( $arg: tt) +) => ( {
304
- $crate:: log!( logger: $logger, target: $target, $crate:: Level :: Trace , $( $arg) +)
333
+ $crate:: log!( logger: $crate :: __log_logger! ( $ logger) , target: $target, $crate:: Level :: Trace , $( $arg) +)
305
334
} ) ;
306
335
307
336
// trace!(logger: my_logger, key1 = 42, key2 = true; "a {} event", "log")
308
337
// trace!(logger: my_logger, "a {} event", "log")
309
338
( logger: $logger: expr, $( $arg: tt) +) => ( {
310
- $crate:: log!( logger: $logger, $crate:: Level :: Trace , $( $arg) +)
339
+ $crate:: log!( logger: $crate :: __log_logger! ( $ logger) , $crate:: Level :: Trace , $( $arg) +)
311
340
} ) ;
312
341
313
342
// trace!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
@@ -351,23 +380,50 @@ macro_rules! trace {
351
380
/// ```
352
381
#[ macro_export]
353
382
macro_rules! log_enabled {
383
+ // log_enabled!(logger: my_logger, target: "my_target", Level::Info)
354
384
( logger: $logger: expr, target: $target: expr, $lvl: expr) => ( {
355
- let lvl = $lvl;
356
- lvl <= $crate:: STATIC_MAX_LEVEL
357
- && lvl <= $crate:: max_level( )
358
- && $crate:: __private_api:: enabled( $logger, lvl, $target)
385
+ $crate:: __log_enabled!( logger: $crate:: __log_logger!( $logger) , target: $target, $lvl)
359
386
} ) ;
360
387
388
+ // log_enabled!(logger: my_logger, Level::Info)
361
389
( logger: $logger: expr, $lvl: expr) => ( {
362
- $crate:: log_enabled !( logger: $logger, target: $crate:: __private_api:: module_path!( ) , $lvl)
390
+ $crate:: __log_enabled !( logger: $crate :: __log_logger! ( $ logger) , target: $crate:: __private_api:: module_path!( ) , $lvl)
363
391
} ) ;
364
392
393
+ // log_enabled!(target: "my_target", Level::Info)
365
394
( target: $target: expr, $lvl: expr) => ( {
366
- $crate:: log_enabled !( logger: $crate:: __private_api :: GlobalLogger , target: $target, $lvl)
395
+ $crate:: __log_enabled !( logger: $crate:: __log_logger! ( __log_global_logger ) , target: $target, $lvl)
367
396
} ) ;
368
397
398
+ // log_enabled!(Level::Info)
369
399
( $lvl: expr) => ( {
370
- $crate:: log_enabled!( target: $crate:: __private_api:: module_path!( ) , $lvl)
400
+ $crate:: __log_enabled!( logger: $crate:: __log_logger!( __log_global_logger) , target: $crate:: __private_api:: module_path!( ) , $lvl)
401
+ } ) ;
402
+ }
403
+
404
+ #[ doc( hidden) ]
405
+ #[ macro_export]
406
+ macro_rules! __log_enabled {
407
+ // log_enabled!(logger: my_logger, target: "my_target", Level::Info)
408
+ ( logger: $logger: expr, target: $target: expr, $lvl: expr) => ( {
409
+ let lvl = $lvl;
410
+ lvl <= $crate:: STATIC_MAX_LEVEL
411
+ && lvl <= $crate:: max_level( )
412
+ && $crate:: __private_api:: enabled( $logger, lvl, $target)
413
+ } ) ;
414
+ }
415
+
416
+ // Determine the logger to use, and whether to take it by-value or by reference
417
+
418
+ #[ doc( hidden) ]
419
+ #[ macro_export]
420
+ macro_rules! __log_logger {
421
+ ( __log_global_logger) => ( {
422
+ $crate:: __private_api:: GlobalLogger
423
+ } ) ;
424
+
425
+ ( $logger: expr) => ( {
426
+ & ( $logger)
371
427
} ) ;
372
428
}
373
429
0 commit comments