diem_logger/
macros.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8//! Macros for sending logs at predetermined log `Level`s
9
10/// Log at the given level, it's recommended to use a specific level macro
11/// instead
12#[macro_export]
13macro_rules! log {
14    // Entry, Log Level + stuff
15    ($level:expr, $($args:tt)+) => {{
16        const METADATA: $crate::Metadata = $crate::Metadata::new(
17            $level,
18            env!("CARGO_CRATE_NAME"),
19            module_path!(),
20            file!(),
21            line!(),
22            concat!(file!(), ':', line!()),
23        );
24
25        if METADATA.enabled() {
26            $crate::Event::dispatch(
27                &METADATA,
28                $crate::fmt_args!($($args)+),
29                $crate::schema!($($args)+),
30            );
31        }
32    }};
33}
34
35/// Log at the `trace` level
36#[macro_export]
37macro_rules! trace {
38    ($($arg:tt)+) => {
39        $crate::log!($crate::Level::Trace, $($arg)+)
40    };
41}
42
43/// Log at the `debug` level
44#[macro_export]
45macro_rules! debug {
46    ($($arg:tt)+) => {
47        $crate::log!($crate::Level::Debug, $($arg)+)
48    };
49}
50
51/// Log at the `info` level
52#[macro_export]
53macro_rules! info {
54    ($($arg:tt)+) => {
55        $crate::log!($crate::Level::Info, $($arg)+)
56    };
57}
58
59/// Log at the `warn` level
60#[macro_export]
61macro_rules! warn {
62    ($($arg:tt)+) => {
63        $crate::log!($crate::Level::Warn, $($arg)+)
64    };
65}
66
67/// Log at the `error` level
68#[macro_export]
69macro_rules! error {
70    ($($arg:tt)+) => {
71        $crate::log!($crate::Level::Error, $($arg)+)
72    };
73}
74
75#[doc(hidden)]
76#[macro_export]
77macro_rules! schema {
78    //
79    // base case
80    //
81    (@ { $(,)* $($val:expr),* $(,)* } $(,)*) => {
82        &[ $($val),* ]
83    };
84
85    //
86    // recursive cases
87    //
88
89    // format args
90    (@ { $(,)* $($out:expr),* }, $template:literal, $($args:tt)*) => {
91        $crate::schema!(
92            @ { $($out),* }
93        )
94    };
95    (@ { $(,)* $($out:expr),* }, $template:literal) => {
96        $crate::schema!(
97            @ { $($out),* }
98        )
99    };
100
101    // Identifier Keys
102    (@ { $(,)* $($out:expr),* }, $($k:ident).+ = $val:expr, $($args:tt)*) => {
103        $crate::schema!(
104            @ { $($out),*, &$crate::KeyValue::new($crate::__log_stringify!($($k).+), $crate::Value::from_serde(&$val)) },
105            $($args)*
106        )
107    };
108
109    (@ { $(,)* $($out:expr),* }, $($k:ident).+ = $val:expr) => {
110        $crate::schema!(
111            @ { $($out),*, &$crate::KeyValue::new($crate::__log_stringify!($($k).+), $crate::Value::from_serde(&$val)) },
112        )
113    };
114
115    // Identifier Keys debug
116    (@ { $(,)* $($out:expr),* }, $($k:ident).+ = ?$val:expr, $($args:tt)*) => {
117        $crate::schema!(
118            @ { $($out),*, &$crate::KeyValue::new($crate::__log_stringify!($($k).+), $crate::Value::from_debug(&$val)) },
119            $($args)*
120        )
121    };
122
123    (@ { $(,)* $($out:expr),* }, $($k:ident).+ = ?$val:expr) => {
124        $crate::schema!(
125            @ { $($out),*, &$crate::KeyValue::new($crate::__log_stringify!($($k).+), $crate::Value::from_debug($val)) },
126        )
127    };
128
129    // Identifier Keys display
130    (@ { $(,)* $($out:expr),* }, $($k:ident).+ = %$val:expr, $($args:tt)*) => {
131        $crate::schema!(
132            @ { $($out),*, &$crate::KeyValue::new($crate::__log_stringify!($($k).+), $crate::Value::from_display(&$val)) },
133            $($args)*
134        )
135    };
136
137    (@ { $(,)* $($out:expr),* }, $($k:ident).+ = %$val:expr) => {
138        $crate::schema!(
139            @ { $($out),*, &$crate::KeyValue::new($crate::__log_stringify!($($k).+), $crate::Value::from_display(&$val)) },
140        )
141    };
142
143    // Literal Keys
144    (@ { $(,)* $($out:expr),* }, $k:literal = $val:expr, $($args:tt)*) => {
145        $crate::schema!(
146            @ { $($out),*, &$crate::KeyValue::new($k, $crate::Value::from_serde(&$val)) },
147            $($args)*
148        )
149    };
150
151    (@ { $(,)* $($out:expr),* }, $k:literal = $val:expr) => {
152        $crate::schema!(
153            @ { $($out),*, &$crate::KeyValue::new($k, $crate::Value::from_serde(&$val)) },
154        )
155    };
156
157    // Literal Keys debug
158    (@ { $(,)* $($out:expr),* }, $k:literal = ?$val:expr, $($args:tt)*) => {
159        $crate::schema!(
160            @ { $($out),*, &$crate::KeyValue::new($k, $crate::Value::from_debug(&$val)) },
161            $($args)*
162        )
163    };
164
165    (@ { $(,)* $($out:expr),* }, $k:literal = ?$val:expr) => {
166        $crate::schema!(
167            @ { $($out),*, &$crate::KeyValue::new($k, $crate::Value::from_debug(&$val)) },
168        )
169    };
170
171    // Literal Keys display
172    (@ { $(,)* $($out:expr),* }, $k:literal = %$val:expr, $($args:tt)*) => {
173        $crate::schema!(
174            @ { $($out),*, &$crate::KeyValue::new($k, $crate::Value::from_display(&$val)) },
175            $($args)*
176        )
177    };
178
179    (@ { $(,)* $($out:expr),* }, $k:literal = %$val:expr) => {
180        $crate::schema!(
181            @ { $($out),*, &$crate::KeyValue::new($k, $crate::Value::from_display(&$val)) },
182        )
183    };
184
185    // Lone Schemas
186    (@ { $(,)* $($out:expr),* }, $schema:expr, $($args:tt)*) => {
187        $crate::schema!(
188            @ { $($out),*, &$schema },
189            $($args)*
190        )
191    };
192    (@ { $(,)* $($out:expr),* }, $schema:expr) => {
193        $crate::schema!(
194            @ { $($out),*, &$schema },
195        )
196    };
197
198    //
199    // entry
200    //
201    ($($args:tt)*) => {
202        $crate::schema!(@ { }, $($args)*)
203    };
204}
205
206#[doc(hidden)]
207#[macro_export]
208macro_rules! fmt_args {
209    //
210    // base case
211    //
212    () => {
213        None
214    };
215
216    // format args
217    ($template:literal, $($args:tt)*) => {
218        Some(::std::format_args!($template, $($args)*))
219    };
220    ($template:literal) => {
221        Some(::std::format_args!($template))
222    };
223
224    // Identifier Keys
225    ($($k:ident).+ = $val:expr, $($args:tt)*) => {
226        $crate::fmt_args!(
227            $($args)*
228        )
229    };
230    ($($k:ident).+ = $val:expr) => {
231        $crate::fmt_args!()
232    };
233    // Identifier Keys with Debug
234    ($($k:ident).+ = ?$val:expr, $($args:tt)*) => {
235        $crate::fmt_args!(
236            $($args)*
237        )
238    };
239    ($($k:ident).+ = ?$val:expr) => {
240        $crate::fmt_args!()
241    };
242    // Identifier Keys with Display
243    ($($k:ident).+ = %$val:expr, $($args:tt)*) => {
244        $crate::fmt_args!(
245            $($args)*
246        )
247    };
248    ($($k:ident).+ = %$val:expr) => {
249        $crate::fmt_args!()
250    };
251
252    // Literal Keys
253    ($k:literal = $val:expr, $($args:tt)*) => {
254        $crate::fmt_args!(
255            $($args)*
256        )
257    };
258    ($k:literal = $val:expr) => {
259        $crate::fmt_args!()
260    };
261    // Literal Keys with Debug
262    ($k:literal = ?$val:expr, $($args:tt)*) => {
263        $crate::fmt_args!(
264            $($args)*
265        )
266    };
267    ($k:literal = ?$val:expr) => {
268        $crate::fmt_args!()
269    };
270    // Literal Keys with Display
271    ($k:literal = %$val:expr, $($args:tt)*) => {
272        $crate::fmt_args!(
273            $($args)*
274        )
275    };
276    ($k:literal = %$val:expr) => {
277        $crate::fmt_args!()
278    };
279
280    // Lone Schemas
281    ($schema:expr, $($args:tt)*) => {
282        $crate::fmt_args!(
283            $($args)*
284        )
285    };
286    ($schema:expr) => {
287        $crate::fmt_args!()
288    };
289}
290
291#[doc(hidden)]
292#[macro_export]
293macro_rules! __log_stringify {
294    ($s:expr) => {
295        stringify!($s)
296    };
297}