aptos_logger_link/
macros.rs

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