Skip to main content

ant_protocol/
logging.rs

1//! Logging facade that compiles to no-ops when the `logging` feature is disabled.
2//!
3//! When the `logging` feature is enabled, this module re-exports the
4//! [`tracing`] macros (`info!`, `warn!`, `debug!`, `error!`, `trace!`).
5//!
6//! When disabled, all macros expand to `()` — zero binary size overhead,
7//! zero runtime cost. Argument expressions are **not evaluated**, so
8//! `info!("{}", expensive_call())` costs nothing in release builds.
9
10// ---- Feature enabled: re-export tracing ----
11#[cfg(feature = "logging")]
12pub use tracing::{debug, enabled, error, info, trace, warn, Level};
13
14// ---- Feature disabled: no-op macros ----
15
16#[cfg(not(feature = "logging"))]
17#[macro_export]
18#[doc(hidden)]
19macro_rules! __protocol_log_noop_info {
20    ($($arg:tt)*) => {
21        ()
22    };
23}
24
25#[cfg(not(feature = "logging"))]
26#[macro_export]
27#[doc(hidden)]
28macro_rules! __protocol_log_noop_warn {
29    ($($arg:tt)*) => {
30        ()
31    };
32}
33
34#[cfg(not(feature = "logging"))]
35#[macro_export]
36#[doc(hidden)]
37macro_rules! __protocol_log_noop_debug {
38    ($($arg:tt)*) => {
39        ()
40    };
41}
42
43#[cfg(not(feature = "logging"))]
44#[macro_export]
45#[doc(hidden)]
46macro_rules! __protocol_log_noop_error {
47    ($($arg:tt)*) => {
48        ()
49    };
50}
51
52#[cfg(not(feature = "logging"))]
53#[macro_export]
54#[doc(hidden)]
55macro_rules! __protocol_log_noop_trace {
56    ($($arg:tt)*) => {
57        ()
58    };
59}
60
61#[cfg(not(feature = "logging"))]
62#[macro_export]
63#[doc(hidden)]
64macro_rules! __protocol_log_noop_enabled {
65    ($($arg:tt)*) => {
66        false
67    };
68}
69
70#[cfg(not(feature = "logging"))]
71pub use __protocol_log_noop_debug as debug;
72#[cfg(not(feature = "logging"))]
73pub use __protocol_log_noop_enabled as enabled;
74#[cfg(not(feature = "logging"))]
75pub use __protocol_log_noop_error as error;
76#[cfg(not(feature = "logging"))]
77pub use __protocol_log_noop_info as info;
78#[cfg(not(feature = "logging"))]
79pub use __protocol_log_noop_trace as trace;
80#[cfg(not(feature = "logging"))]
81pub use __protocol_log_noop_warn as warn;
82
83/// Stub for `tracing::Level` when logging is disabled.
84#[cfg(not(feature = "logging"))]
85#[allow(dead_code)]
86pub struct Level;
87
88#[cfg(not(feature = "logging"))]
89#[allow(dead_code)]
90impl Level {
91    /// Debug level stub.
92    pub const DEBUG: Self = Self;
93    /// Info level stub.
94    pub const INFO: Self = Self;
95    /// Warn level stub.
96    pub const WARN: Self = Self;
97    /// Error level stub.
98    pub const ERROR: Self = Self;
99    /// Trace level stub.
100    pub const TRACE: Self = Self;
101}