asupersync/
tracing_compat.rs1#[cfg(feature = "tracing-integration")]
30pub use tracing::{
31 Instrument, Level, Span, debug, debug_span, error, error_span, event, info, info_span,
32 instrument, span, trace, trace_span, warn, warn_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 crate::test_complete!("test_noop_level_constants");
301 }
302
303 #[test]
304 fn test_noop_span_methods() {
305 init_test("test_noop_span_methods");
306 #[cfg(not(feature = "tracing-integration"))]
307 {
308 let span = NoopSpan;
309 let disabled = span.is_disabled();
310 crate::assert_with_log!(disabled, "noop span should be disabled", true, disabled);
311
312 let current = NoopSpan::current();
313 let none = NoopSpan::none();
314 let _ = current.or_current();
315 none.follows_from(&span);
316 }
317 crate::test_complete!("test_noop_span_methods");
318 }
319
320 #[test]
321 fn noop_guard_debug() {
322 #[cfg(not(feature = "tracing-integration"))]
323 {
324 let guard = NoopGuard;
325 let dbg = format!("{guard:?}");
326 assert!(dbg.contains("NoopGuard"), "{dbg}");
327 }
328 }
329
330 #[test]
331 fn noop_span_debug_clone_copy() {
332 #[cfg(not(feature = "tracing-integration"))]
333 {
334 let span = NoopSpan;
335 let dbg = format!("{span:?}");
336 assert!(dbg.contains("NoopSpan"), "{dbg}");
337
338 let copied = span;
339 let cloned = span;
340 assert!(copied.is_disabled());
341 assert!(cloned.is_disabled());
342 }
343 }
344
345 #[test]
346 fn level_equality_and_ordering() {
347 #[cfg(not(feature = "tracing-integration"))]
348 {
349 assert_eq!(Level::TRACE, Level::DEBUG);
351 assert_eq!(Level::INFO, Level::WARN);
352 assert_eq!(Level::WARN, Level::ERROR);
353
354 assert!(Level::TRACE <= Level::ERROR);
356
357 let dbg = format!("{:?}", Level::INFO);
359 assert!(dbg.contains("Level"), "{dbg}");
360
361 let l = Level::INFO;
363 let copied = l;
364 let cloned = l;
365 assert_eq!(copied, cloned);
366 }
367 }
368
369 #[test]
370 fn span_type_alias_works() {
371 #[cfg(not(feature = "tracing-integration"))]
372 {
373 let span: Span = NoopSpan::current();
374 let _guard = span.enter();
375 }
376 }
377
378 #[test]
379 fn all_span_macros_compile() {
380 #[cfg(not(feature = "tracing-integration"))]
381 {
382 let _ = trace_span!("t");
383 let _ = debug_span!("d");
384 let _ = info_span!("i");
385 let _ = warn_span!("w");
386 let _ = error_span!("e");
387 }
388 }
389
390 #[test]
391 fn instrument_trait_noop() {
392 #[cfg(not(feature = "tracing-integration"))]
393 {
394 let fut = async { 42 };
395 let instrumented = fut.instrument(NoopSpan);
396 drop(async { 1 }.in_current_span());
398 drop(instrumented);
399 }
400 }
401}