x11rb/
tracing.rs

1//! Wrapper around tracing so that tracing can be an optional dependency.
2
3#[derive(Debug, Clone, Copy)]
4#[allow(dead_code)]
5pub(crate) enum Level {
6    Error,
7    Warn,
8    Info,
9    Debug,
10    Trace,
11}
12
13#[cfg(feature = "tracing")]
14pub(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"))]
44pub(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
73macro_rules! error {
74    ( $($arg:tt)+ ) => { $crate::tracing::implementation::event!($crate::tracing::Level::Error, $($arg)+) };
75}
76macro_rules! warning {
77    ( $($arg:tt)+ ) => { $crate::tracing::implementation::event!($crate::tracing::Level::Warn, $($arg)+) };
78}
79macro_rules! info {
80    ( $($arg:tt)+ ) => { $crate::tracing::implementation::event!($crate::tracing::Level::Info, $($arg)+) };
81}
82macro_rules! debug {
83    ( $($arg:tt)+ ) => { $crate::tracing::implementation::event!($crate::tracing::Level::Debug, $($arg)+) };
84}
85macro_rules! trace {
86    ( $($arg:tt)+ ) => { $crate::tracing::implementation::event!($crate::tracing::Level::Trace, $($arg)+) };
87}
88
89pub(crate) use debug;
90pub(crate) use error;
91pub(crate) use info;
92pub(crate) use trace;
93pub(crate) use warning;
94
95#[allow(unused_macros)]
96macro_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)]
101macro_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#[allow(unused_macro_rules)]
106macro_rules! info_span {
107    ( $name:expr ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Info, $name, ) };
108    ( $name:expr, $($fields:tt)* ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Info, $name, $($fields)*) };
109}
110macro_rules! debug_span {
111    ( $name:expr ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Debug, $name, ) };
112    ( $name:expr, $($fields:tt)* ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Debug, $name, $($fields)*) };
113}
114#[allow(unused_macro_rules)]
115macro_rules! trace_span {
116    ( $name:expr ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Trace, $name, ) };
117    ( $name:expr, $($fields:tt)* ) => { $crate::tracing::implementation::span!($crate::tracing::Level::Trace, $name, $($fields)*) };
118}
119
120pub(crate) use debug_span;
121#[allow(unused_imports)]
122pub(crate) use error_span;
123pub(crate) use info_span;
124pub(crate) use trace_span;
125#[allow(unused_imports)]
126pub(crate) use warning_span;