sketching/
macros.rs

1#[macro_export]
2macro_rules! spanned {
3    ($name:expr, $code:block) => {{
4        // Block: can short circuit outer function
5        use tracing::debug_span;
6        let _entered_span = debug_span!($name).entered();
7        $code
8    }};
9    ($name:expr, || $code:block) => {{
10        // Closure: cannot short circuit outer function
11        use tracing::debug_span;
12        let _entered_span = debug_span!($name).entered();
13        (|| $code)()
14    }};
15}
16
17#[macro_export]
18macro_rules! tagged_event {
19    ($level:ident, $event_tag:path, $($arg:tt)*) => {{
20        use tracing;
21        fn assert_eventtag(_: &EventTag) {}
22        assert_eventtag(&$event_tag);
23        let event_tag_id: u64 = $event_tag.into();
24        tracing::event!(tracing::Level::$level, event_tag_id, $($arg)*)
25    }}
26}
27
28#[macro_export]
29macro_rules! admin_debug {
30    ($($arg:tt)*) => { tagged_event!(DEBUG, EventTag::AdminDebug, $($arg)*) }
31}
32
33#[macro_export]
34macro_rules! admin_error {
35    ($($arg:tt)*) => { tagged_event!(ERROR, EventTag::AdminError, $($arg)*) }
36}
37
38#[macro_export]
39macro_rules! admin_warn {
40    ($($arg:tt)*) => { tagged_event!(WARN, EventTag::AdminWarn, $($arg)*) }
41}
42
43#[macro_export]
44macro_rules! admin_info {
45    ($($arg:tt)*) => { tagged_event!(INFO, EventTag::AdminInfo, $($arg)*) }
46}
47
48#[macro_export]
49macro_rules! request_error {
50    ($($arg:tt)*) => { tagged_event!(ERROR, EventTag::RequestError, $($arg)*) }
51}
52
53#[macro_export]
54macro_rules! request_warn {
55    ($($arg:tt)*) => { tagged_event!(WARN, EventTag::RequestWarn, $($arg)*) }
56}
57
58#[macro_export]
59macro_rules! request_info {
60    ($($arg:tt)*) => { tagged_event!(INFO, EventTag::RequestInfo, $($arg)*) }
61}
62
63#[macro_export]
64macro_rules! request_trace {
65    ($($arg:tt)*) => { tagged_event!(TRACE, EventTag::RequestTrace, $($arg)*) }
66}
67
68#[macro_export]
69macro_rules! security_critical {
70    ($($arg:tt)*) => { tagged_event!(INFO, EventTag::SecurityCritical, $($arg)*) }
71}
72
73#[macro_export]
74macro_rules! security_error {
75    ($($arg:tt)*) => { tagged_event!(ERROR, EventTag::SecurityError, $($arg)*) }
76}
77
78#[macro_export]
79macro_rules! security_info {
80    ($($arg:tt)*) => { tagged_event!(INFO, EventTag::SecurityInfo, $($arg)*) }
81}
82
83#[macro_export]
84macro_rules! security_debug {
85    ($($arg:tt)*) => { tagged_event!(DEBUG, EventTag::SecurityDebug, $($arg)*) }
86}
87
88#[macro_export]
89macro_rules! security_access {
90    ($($arg:tt)*) => { tagged_event!(INFO, EventTag::SecurityAccess, $($arg)*) }
91}
92
93#[macro_export]
94macro_rules! filter_error {
95    ($($arg:tt)*) => { tagged_event!(ERROR, EventTag::FilterError, $($arg)*) }
96}
97
98#[macro_export]
99macro_rules! filter_warn {
100    ($($arg:tt)*) => { tagged_event!(WARN, EventTag::FilterWarn, $($arg)*) }
101}
102
103#[macro_export]
104macro_rules! filter_info {
105    ($($arg:tt)*) => { tagged_event!(INFO, EventTag::FilterInfo, $($arg)*) }
106}
107
108#[macro_export]
109macro_rules! filter_debug {
110    ($($arg:tt)*) => { tagged_event!(DEBUG, EventTag::FilterTrace, $($arg)*) }
111}
112
113#[macro_export]
114macro_rules! filter_trace {
115    ($($arg:tt)*) => { tagged_event!(TRACE, EventTag::FilterTrace, $($arg)*) }
116}
117
118#[macro_export]
119macro_rules! perf_trace {
120    ($($arg:tt)*) => { tagged_event!(TRACE, EventTag::PerfTrace, $($arg)*) }
121}
122
123#[macro_export]
124macro_rules! event_dynamic_lvl {
125    ( $(target: $target:expr,)? $(parent: $parent:expr,)? $lvl:expr, $($tt:tt)* ) => {
126        match $lvl {
127            tracing::Level::ERROR => {
128                tracing::event!(
129                    $(target: $target,)?
130                    $(parent: $parent,)?
131                    tracing::Level::ERROR,
132                    $($tt)*
133                );
134            }
135            tracing::Level::WARN => {
136                tracing::event!(
137                    $(target: $target,)?
138                    $(parent: $parent,)?
139                    tracing::Level::WARN,
140                    $($tt)*
141                );
142            }
143            tracing::Level::INFO => {
144                tracing::event!(
145                    $(target: $target,)?
146                    $(parent: $parent,)?
147                    tracing::Level::INFO,
148                    $($tt)*
149                );
150            }
151            tracing::Level::DEBUG => {
152                tracing::event!(
153                    $(target: $target,)?
154                    $(parent: $parent,)?
155                    tracing::Level::DEBUG,
156                    $($tt)*
157                );
158            }
159            tracing::Level::TRACE => {
160                tracing::event!(
161                    $(target: $target,)?
162                    $(parent: $parent,)?
163                    tracing::Level::TRACE,
164                    $($tt)*
165                );
166            }
167        }
168    };
169}