1 | //! Wrapper around tracing so that tracing can be an optional dependency. |
2 | |
3 | #[derive (Debug, Clone, Copy)] |
4 | #[allow (dead_code)] |
5 | pub(crate) enum Level { |
6 | Error, |
7 | Warn, |
8 | Info, |
9 | Debug, |
10 | Trace, |
11 | } |
12 | |
13 | #[cfg (feature = "tracing" )] |
14 | pub(crate) mod implementation { |
15 | impl super::Level { |
16 | pub(crate) const fn to_tracing(self) -> tracing::Level { |
17 | match self { |
18 | Self::Error => tracing::Level::ERROR, |
19 | Self::Warn => tracing::Level::WARN, |
20 | Self::Info => tracing::Level::INFO, |
21 | Self::Debug => tracing::Level::DEBUG, |
22 | Self::Trace => tracing::Level::TRACE, |
23 | } |
24 | } |
25 | } |
26 | |
27 | macro_rules! event { |
28 | ( $lvl:expr, $($arg:tt)+ ) => { |
29 | tracing::event!($crate::tracing::Level::to_tracing($lvl), $($arg)+) |
30 | } |
31 | } |
32 | |
33 | macro_rules! span { |
34 | ( $lvl:expr, $name:expr, $($fields:tt)* ) => { |
35 | tracing::span!($crate::tracing::Level::to_tracing($lvl), $name, $($fields)*) |
36 | } |
37 | } |
38 | |
39 | pub(crate) use event; |
40 | pub(crate) use span; |
41 | } |
42 | |
43 | #[cfg (not(feature = "tracing" ))] |
44 | pub(crate) mod implementation { |
45 | macro_rules! event { |
46 | ( $lvl:expr, { $($fields:tt)+ }, $($arg:tt)+ ) => { |
47 | let _ = format_args!($($arg)+); |
48 | }; |
49 | ( $lvl:expr, $($arg:tt)+ ) => { |
50 | let _ = format_args!($($arg)+); |
51 | }; |
52 | } |
53 | |
54 | pub(crate) struct Span; |
55 | pub(crate) struct EnteredSpan; |
56 | |
57 | impl Span { |
58 | pub(crate) fn entered(&self) -> EnteredSpan { |
59 | EnteredSpan |
60 | } |
61 | } |
62 | |
63 | macro_rules! span { |
64 | ( $lvl:expr, $name:expr, $($fields:tt)* ) => { |
65 | $crate::tracing::implementation::Span |
66 | }; |
67 | } |
68 | |
69 | pub(crate) use event; |
70 | pub(crate) use span; |
71 | } |
72 | |
73 | macro_rules! error { |
74 | ( $($arg:tt)+ ) => { $crate::tracing::implementation::event!($crate::tracing::Level::Error, $($arg)+) }; |
75 | } |
76 | macro_rules! warning { |
77 | ( $($arg:tt)+ ) => { $crate::tracing::implementation::event!($crate::tracing::Level::Warn, $($arg)+) }; |
78 | } |
79 | macro_rules! info { |
80 | ( $($arg:tt)+ ) => { $crate::tracing::implementation::event!($crate::tracing::Level::Info, $($arg)+) }; |
81 | } |
82 | macro_rules! debug { |
83 | ( $($arg:tt)+ ) => { $crate::tracing::implementation::event!($crate::tracing::Level::Debug, $($arg)+) }; |
84 | } |
85 | macro_rules! trace { |
86 | ( $($arg:tt)+ ) => { $crate::tracing::implementation::event!($crate::tracing::Level::Trace, $($arg)+) }; |
87 | } |
88 | |
89 | pub(crate) use debug; |
90 | pub(crate) use error; |
91 | pub(crate) use info; |
92 | pub(crate) use trace; |
93 | pub(crate) use warning; |
94 | |
95 | #[allow (unused_macros)] |
96 | macro_rules! error_span { |
97 | ( $name:expr ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Error, $name, ) }; |
98 | ( $name:expr, $($fields:tt)* ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Error, $name, $($fields)*) }; |
99 | } |
100 | #[allow (unused_macros)] |
101 | macro_rules! warning_span { |
102 | ( $name:expr ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Warn, $name, ) }; |
103 | ( $name:expr, $($fields:tt)* ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Warn, $name, $($fields)*) }; |
104 | } |
105 | macro_rules! info_span { |
106 | ( $name:expr ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Info, $name, ) }; |
107 | ( $name:expr, $($fields:tt)* ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Info, $name, $($fields)*) }; |
108 | } |
109 | macro_rules! debug_span { |
110 | ( $name:expr ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Debug, $name, ) }; |
111 | ( $name:expr, $($fields:tt)* ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Debug, $name, $($fields)*) }; |
112 | } |
113 | macro_rules! trace_span { |
114 | ( $name:expr ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Trace, $name, ) }; |
115 | ( $name:expr, $($fields:tt)* ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Trace, $name, $($fields)*) }; |
116 | } |
117 | |
118 | pub(crate) use debug_span; |
119 | #[allow (unused_imports)] |
120 | pub(crate) use error_span; |
121 | pub(crate) use info_span; |
122 | pub(crate) use trace_span; |
123 | #[allow (unused_imports)] |
124 | pub(crate) use warning_span; |
125 | |