asupersync/
tracing_compat.rs1#[cfg(feature = "tracing-integration")]
30pub use tracing::{
31 debug, debug_span, error, error_span, event, info, info_span, instrument, span, trace,
32 trace_span, warn, warn_span, Instrument, Level, Span,
33};
34
35#[cfg(not(feature = "tracing-integration"))]
37mod noop {
38 #[macro_export]
44 macro_rules! trace {
45 ($($arg:tt)*) => {};
46 }
47
48 #[macro_export]
50 macro_rules! debug {
51 ($($arg:tt)*) => {};
52 }
53
54 #[macro_export]
56 macro_rules! info {
57 ($($arg:tt)*) => {};
58 }
59
60 #[macro_export]
62 macro_rules! warn {
63 ($($arg:tt)*) => {};
64 }
65
66 #[macro_export]
68 macro_rules! error {
69 ($($arg:tt)*) => {};
70 }
71
72 #[macro_export]
74 macro_rules! event {
75 ($($arg:tt)*) => {};
76 }
77
78 #[macro_export]
80 macro_rules! span {
81 ($($arg:tt)*) => {
82 $crate::tracing_compat::NoopSpan
83 };
84 }
85
86 #[macro_export]
88 macro_rules! trace_span {
89 ($($arg:tt)*) => {
90 $crate::tracing_compat::NoopSpan
91 };
92 }
93
94 #[macro_export]
96 macro_rules! debug_span {
97 ($($arg:tt)*) => {
98 $crate::tracing_compat::NoopSpan
99 };
100 }
101
102 #[macro_export]
104 macro_rules! info_span {
105 ($($arg:tt)*) => {
106 $crate::tracing_compat::NoopSpan
107 };
108 }
109
110 #[macro_export]
112 macro_rules! warn_span {
113 ($($arg:tt)*) => {
114 $crate::tracing_compat::NoopSpan
115 };
116 }
117
118 #[macro_export]
120 macro_rules! error_span {
121 ($($arg:tt)*) => {
122 $crate::tracing_compat::NoopSpan
123 };
124 }
125
126 #[macro_export]
129 macro_rules! instrument {
130 ($($arg:tt)*) => {};
131 }
132
133 pub use crate::{
135 debug, debug_span, error, error_span, event, info, info_span, instrument, span, trace,
136 trace_span, warn, warn_span,
137 };
138}
139
140#[cfg(not(feature = "tracing-integration"))]
141pub use noop::*;
142
143#[cfg(not(feature = "tracing-integration"))]
149#[derive(Debug, Clone, Copy)]
150pub struct NoopSpan;
151
152#[cfg(not(feature = "tracing-integration"))]
153impl NoopSpan {
154 #[inline]
156 #[must_use]
157 pub fn enter(&self) -> NoopGuard {
158 NoopGuard
159 }
160
161 #[inline]
163 #[must_use]
164 pub fn entered(self) -> Self {
165 self
166 }
167
168 #[inline]
170 #[must_use]
171 pub fn or_current(self) -> Self {
172 self
173 }
174
175 #[inline]
177 pub fn follows_from(&self, _span: &Self) {}
178
179 #[inline]
181 #[must_use]
182 pub fn is_disabled(&self) -> bool {
183 true
184 }
185
186 #[inline]
188 pub fn record<V>(&self, _field: &str, _value: V) {}
189
190 #[inline]
192 #[must_use]
193 pub fn current() -> Self {
194 Self
195 }
196
197 #[inline]
199 #[must_use]
200 pub fn none() -> Self {
201 Self
202 }
203}
204
205#[cfg(not(feature = "tracing-integration"))]
207#[derive(Debug)]
208pub struct NoopGuard;
209
210#[cfg(not(feature = "tracing-integration"))]
212#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
213pub struct Level;
214
215#[cfg(not(feature = "tracing-integration"))]
216impl Level {
217 pub const TRACE: Self = Self;
219 pub const DEBUG: Self = Self;
221 pub const INFO: Self = Self;
223 pub const WARN: Self = Self;
225 pub const ERROR: Self = Self;
227}
228
229#[cfg(not(feature = "tracing-integration"))]
231pub type Span = NoopSpan;
232
233#[cfg(not(feature = "tracing-integration"))]
238pub trait Instrument: Sized {
239 #[must_use]
241 fn instrument(self, _span: NoopSpan) -> Self {
242 self
243 }
244
245 #[must_use]
247 fn in_current_span(self) -> Self {
248 self
249 }
250}
251
252#[cfg(not(feature = "tracing-integration"))]
253impl<T> Instrument for T {}
254
255#[cfg(test)]
256mod tests {
257 use super::*;
258 use crate::test_utils::init_test_logging;
259
260 fn init_test(test_name: &str) {
261 init_test_logging();
262 crate::test_phase!(test_name);
263 }
264
265 #[test]
266 fn test_noop_macros_compile() {
267 init_test("test_noop_macros_compile");
268 trace!("trace message");
270 debug!("debug message");
271 info!("info message");
272 warn!("warn message");
273 error!("error message");
274
275 trace!(field = "value", "trace with field");
276 debug!(count = 42, "debug with field");
277 info!(name = "test", "info with field");
278 crate::test_complete!("test_noop_macros_compile");
279 }
280
281 #[test]
282 fn test_noop_span_compile() {
283 init_test("test_noop_span_compile");
284 let span = span!(Level::INFO, "test_span");
285 let _guard = span.enter();
286
287 let span2 = info_span!("info_span");
288 let _entered = span2.entered();
289
290 let span3 = debug_span!("debug_span", task_id = 42);
291 span3.record("field", "value");
292 crate::test_complete!("test_noop_span_compile");
293 }
294
295 #[test]
296 fn test_noop_level_constants() {
297 init_test("test_noop_level_constants");
298 #[cfg(not(feature = "tracing-integration"))]
299 {
300 let _ = Level::TRACE;
301 let _ = Level::DEBUG;
302 let _ = Level::INFO;
303 let _ = Level::WARN;
304 let _ = Level::ERROR;
305 }
306 crate::test_complete!("test_noop_level_constants");
307 }
308
309 #[test]
310 fn test_noop_span_methods() {
311 init_test("test_noop_span_methods");
312 #[cfg(not(feature = "tracing-integration"))]
313 {
314 let span = NoopSpan;
315 let disabled = span.is_disabled();
316 crate::assert_with_log!(disabled, "noop span should be disabled", true, disabled);
317
318 let current = NoopSpan::current();
319 let none = NoopSpan::none();
320 let _ = current.or_current();
321 none.follows_from(&span);
322 }
323 crate::test_complete!("test_noop_span_methods");
324 }
325}