maitake/
trace.rs

1#![cfg_attr(not(test), allow(dead_code, unused_macros))]
2
3use mycelium_util::fmt;
4
5macro_rules! span {
6    ($level:expr, $($arg:tt)+) => {
7        crate::trace::Span {
8            #[cfg(any(feature = "tracing-01", loom))]
9            span_01: {
10                use tracing_01::Level;
11                tracing_01::span!($level, $($arg)+)
12            },
13
14            #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
15            span_02: {
16                use tracing_02::Level;
17                tracing_02::span!($level, $($arg)+)
18            }
19        }
20    }
21}
22
23macro_rules! trace {
24    ($($arg:tt)+) => {
25        #[cfg(any(feature = "tracing-01", loom))]
26        {
27            tracing_01::trace!($($arg)+)
28        }
29
30        #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
31        {
32            tracing_02::trace!($($arg)+)
33        }
34    };
35}
36
37macro_rules! debug {
38    ($($arg:tt)+) => {
39        #[cfg(any(feature = "tracing-01", loom))]
40        {
41            tracing_01::debug!($($arg)+)
42        }
43
44        #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
45        {
46            tracing_02::debug!($($arg)+)
47        }
48    };
49}
50
51#[cfg(test)]
52macro_rules! info {
53    ($($arg:tt)+) => {
54        #[cfg(any(feature = "tracing-01", loom))]
55        {
56            tracing_01::info!($($arg)+)
57        }
58
59        #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
60        {
61            tracing_02::info!($($arg)+)
62        }
63    };
64}
65
66macro_rules! trace_span {
67    ($($arg:tt)+) => {
68        span!(Level::TRACE, $($arg)+)
69    };
70}
71
72#[allow(unused_macros)]
73macro_rules! debug_span {
74    ($($arg:tt)+) => {
75        span!(Level::DEBUG, $($arg)+)
76    };
77}
78
79#[cfg(all(not(test), not(maitake_ultraverbose)))]
80macro_rules! test_dbg {
81    ($e:expr) => {
82        $e
83    };
84}
85
86#[cfg(any(test, maitake_ultraverbose))]
87macro_rules! test_dbg {
88    ($e:expr) => {
89        match $e {
90            e => {
91                debug!(
92                    location = %core::panic::Location::caller(),
93                    "{} = {:?}",
94                    stringify!($e),
95                    &e
96                );
97                e
98            }
99        }
100    };
101}
102
103#[cfg(all(not(test), not(maitake_ultraverbose)))]
104macro_rules! test_debug {
105    ($($args:tt)+) => {};
106}
107
108#[cfg(any(test, maitake_ultraverbose))]
109macro_rules! test_debug {
110    ($($args:tt)+) => {
111        debug!($($args)+);
112    };
113}
114
115#[cfg(all(not(test), not(maitake_ultraverbose)))]
116macro_rules! test_trace {
117    ($($args:tt)+) => {};
118}
119
120#[cfg(any(test, maitake_ultraverbose))]
121macro_rules! test_trace {
122    ($($args:tt)+) => {
123        trace!($($args)+);
124    };
125}
126
127#[derive(Clone)]
128pub(crate) struct Span {
129    #[cfg(any(feature = "tracing-01", loom))]
130    pub(crate) span_01: tracing_01::Span,
131    #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
132    pub(crate) span_02: tracing_02::Span,
133}
134
135impl Span {
136    #[inline(always)]
137    pub(crate) const fn none() -> Self {
138        Span {
139            #[cfg(any(feature = "tracing-01", loom))]
140            span_01: tracing_01::Span::none(),
141            #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
142            span_02: tracing_02::Span::none(),
143        }
144    }
145
146    #[inline]
147    pub(crate) fn enter(&self) -> Entered<'_> {
148        Entered {
149            #[cfg(any(feature = "tracing-01", loom))]
150            _enter_01: self.span_01.enter(),
151
152            #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
153            _enter_02: self.span_02.enter(),
154
155            _p: core::marker::PhantomData,
156        }
157    }
158
159    #[inline]
160    pub(crate) fn entered(self) -> EnteredSpan {
161        EnteredSpan {
162            #[cfg(any(feature = "tracing-01", loom))]
163            _enter_01: self.span_01.entered(),
164            #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
165            _enter_02: self.span_02.entered(),
166        }
167    }
168
169    #[cfg(any(feature = "tracing-01", loom))]
170    #[inline]
171    pub(crate) fn tracing_01_id(&self) -> Option<core::num::NonZeroU64> {
172        self.span_01.id().map(|id| id.into_non_zero_u64())
173    }
174}
175
176impl fmt::Debug for Span {
177    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178        const TRACING_01_FIELD: &str = "tracing_01";
179        const TRACING_02_FIELD: &str = "tracing_02";
180
181        let mut s = f.debug_struct("Span");
182
183        #[cfg(any(feature = "tracing-01", loom))]
184        if let Some(id) = self.span_01.id() {
185            s.field(TRACING_01_FIELD, &id.into_u64());
186        } else {
187            s.field(TRACING_02_FIELD, &fmt::display("<none>"));
188        }
189
190        #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
191        if let Some(id) = self.span_02.id() {
192            s.field(TRACING_02_FIELD, &id.into_u64());
193        } else {
194            s.field(TRACING_01_FIELD, &fmt::display("<none>"));
195        }
196
197        s.finish()
198    }
199}
200
201#[derive(Debug)]
202pub(crate) struct Entered<'span> {
203    #[cfg(any(feature = "tracing-01", loom))]
204    _enter_01: tracing_01::span::Entered<'span>,
205    #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
206    _enter_02: tracing_02::span::Entered<'span>,
207
208    /// This is just there so that the `'span` lifetime is used even when both
209    /// `tracing` features are disabled.
210    _p: core::marker::PhantomData<&'span ()>,
211}
212
213#[derive(Debug)]
214pub(crate) struct EnteredSpan {
215    #[cfg(any(feature = "tracing-01", loom))]
216    _enter_01: tracing_01::span::EnteredSpan,
217    #[cfg(any(feature = "tracing-02", all(test, not(loom))))]
218    _enter_02: tracing_02::span::EnteredSpan,
219}