Skip to content

Commit 7aacc8f

Browse files
committed
pass global logger by value, supplied logger by ref
1 parent a438c6e commit 7aacc8f

File tree

2 files changed

+92
-36
lines changed

2 files changed

+92
-36
lines changed

src/__private_api.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn log_impl<L: Log>(
8585
}
8686

8787
pub fn log<'a, K, L>(
88-
logger: &L,
88+
logger: L,
8989
args: Arguments,
9090
level: Level,
9191
target_module_path_and_loc: &(&str, &'static str, &'static Location),

src/macros.rs

+91-35
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,56 @@
6666
#[macro_export]
6767
#[clippy::format_args]
6868
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 {
69113
// log!(logger: my_logger, target: "my_target", Level::Info, key1:? = 42, key2 = true; "a {} event", "log");
70114
(logger: $logger:expr, target: $target:expr, $lvl:expr, $($key:tt $(:$capture:tt)? $(= $value:expr)?),+; $($arg:tt)+) => ({
71115
let lvl = $lvl;
72116
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
73-
$crate::__private_api::log::<&_, _>(
74-
&($logger),
117+
$crate::__private_api::log(
118+
$logger,
75119
$crate::__private_api::format_args!($($arg)+),
76120
lvl,
77121
&($target, $crate::__private_api::module_path!(), $crate::__private_api::loc()),
@@ -85,29 +129,14 @@ macro_rules! log {
85129
let lvl = $lvl;
86130
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
87131
$crate::__private_api::log(
88-
&($logger),
132+
$logger,
89133
$crate::__private_api::format_args!($($arg)+),
90134
lvl,
91135
&($target, $crate::__private_api::module_path!(), $crate::__private_api::loc()),
92136
(),
93137
);
94138
}
95139
});
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-
});
111140
}
112141

113142
/// Logs a message at the error level.
@@ -130,13 +159,13 @@ macro_rules! error {
130159
// error!(logger: my_logger, target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
131160
// error!(logger: my_logger, target: "my_target", "a {} event", "log")
132161
(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)+)
134163
});
135164

136165
// error!(logger: my_logger, key1 = 42, key2 = true; "a {} event", "log")
137166
// error!(logger: my_logger, "a {} event", "log")
138167
(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)+)
140169
});
141170

142171
// error!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
@@ -169,13 +198,13 @@ macro_rules! warn {
169198
// warn!(logger: my_logger, target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
170199
// warn!(logger: my_logger, target: "my_target", "a {} event", "log")
171200
(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)+)
173202
});
174203

175204
// warn!(logger: my_logger, key1 = 42, key2 = true; "a {} event", "log")
176205
// warn!(logger: my_logger, "a {} event", "log")
177206
(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)+)
179208
});
180209

181210
// warn!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
@@ -217,13 +246,13 @@ macro_rules! info {
217246
// info!(logger: my_logger, target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
218247
// info!(logger: my_logger, target: "my_target", "a {} event", "log")
219248
(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)+)
221250
});
222251

223252
// info!(logger: my_logger, key1 = 42, key2 = true; "a {} event", "log")
224253
// info!(logger: my_logger, "a {} event", "log")
225254
(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)+)
227256
});
228257

229258
// info!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
@@ -257,13 +286,13 @@ macro_rules! debug {
257286
// debug!(logger: my_logger, target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
258287
// debug!(logger: my_logger, target: "my_target", "a {} event", "log")
259288
(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)+)
261290
});
262291

263292
// debug!(logger: my_logger, key1 = 42, key2 = true; "a {} event", "log")
264293
// debug!(logger: my_logger, "a {} event", "log")
265294
(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)+)
267296
});
268297

269298
// debug!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
@@ -301,13 +330,13 @@ macro_rules! trace {
301330
// trace!(logger: my_logger, target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
302331
// trace!(logger: my_logger, target: "my_target", "a {} event", "log")
303332
(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)+)
305334
});
306335

307336
// trace!(logger: my_logger, key1 = 42, key2 = true; "a {} event", "log")
308337
// trace!(logger: my_logger, "a {} event", "log")
309338
(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)+)
311340
});
312341

313342
// trace!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
@@ -351,23 +380,50 @@ macro_rules! trace {
351380
/// ```
352381
#[macro_export]
353382
macro_rules! log_enabled {
383+
// log_enabled!(logger: my_logger, target: "my_target", Level::Info)
354384
(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)
359386
});
360387

388+
// log_enabled!(logger: my_logger, Level::Info)
361389
(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)
363391
});
364392

393+
// log_enabled!(target: "my_target", Level::Info)
365394
(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)
367396
});
368397

398+
// log_enabled!(Level::Info)
369399
($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)
371427
});
372428
}
373429

0 commit comments

Comments
 (0)