Skip to main content

grafeo_common/
tracing_macros.rs

1//! Conditional tracing macros.
2//!
3//! When the `tracing` feature is enabled these expand to the corresponding
4//! [`tracing`] crate macros. When disabled they compile to nothing, giving
5//! zero overhead for profiles like `embedded` and `browser`.
6
7/// Zero-sized span guard returned when tracing is disabled.
8///
9/// Allows `let _span = grafeo_debug_span!(...)` to compile without
10/// triggering clippy's `let_unit_value` lint.
11pub struct NoopSpan;
12
13/// Enters an info-level span. Returns a guard that keeps the span open.
14#[macro_export]
15macro_rules! grafeo_info_span {
16    ($($arg:tt)*) => {{
17        #[cfg(feature = "tracing")]
18        { ::tracing::info_span!($($arg)*).entered() }
19        #[cfg(not(feature = "tracing"))]
20        { $crate::tracing_macros::NoopSpan }
21    }};
22}
23
24/// Enters a debug-level span.
25#[macro_export]
26macro_rules! grafeo_debug_span {
27    ($($arg:tt)*) => {{
28        #[cfg(feature = "tracing")]
29        { ::tracing::debug_span!($($arg)*).entered() }
30        #[cfg(not(feature = "tracing"))]
31        { $crate::tracing_macros::NoopSpan }
32    }};
33}
34
35/// Emits a warn-level event.
36///
37/// When the `tracing` feature is disabled, the format string and its arguments
38/// are still referenced (via `format_args!`) so that variables captured in the
39/// message do not trigger `unused_variable` warnings. The compiler optimises
40/// the dead `format_args!` call away entirely.
41#[macro_export]
42macro_rules! grafeo_warn {
43    ($($arg:tt)*) => {{
44        #[cfg(feature = "tracing")]
45        { ::tracing::warn!($($arg)*); }
46        #[cfg(not(feature = "tracing"))]
47        { if false { let _ = format_args!($($arg)*); } }
48    }};
49}
50
51/// Emits an info-level event.
52#[macro_export]
53macro_rules! grafeo_info {
54    ($($arg:tt)*) => {{
55        #[cfg(feature = "tracing")]
56        { ::tracing::info!($($arg)*); }
57        #[cfg(not(feature = "tracing"))]
58        { if false { let _ = format_args!($($arg)*); } }
59    }};
60}
61
62/// Emits a debug-level event.
63#[macro_export]
64macro_rules! grafeo_debug {
65    ($($arg:tt)*) => {{
66        #[cfg(feature = "tracing")]
67        { ::tracing::debug!($($arg)*); }
68        #[cfg(not(feature = "tracing"))]
69        { if false { let _ = format_args!($($arg)*); } }
70    }};
71}
72
73/// Emits an error-level event.
74#[macro_export]
75macro_rules! grafeo_error {
76    ($($arg:tt)*) => {{
77        #[cfg(feature = "tracing")]
78        { ::tracing::error!($($arg)*); }
79        #[cfg(not(feature = "tracing"))]
80        { if false { let _ = format_args!($($arg)*); } }
81    }};
82}