Skip to main content

ant_node/
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//! ## Unused variables with `--no-default-features`
11//!
12//! Variables that exist only for logging (e.g. `let addr_hex = hex::encode(...)`)
13//! become unused when macros are compiled out. The crate-level attribute
14//! `#![cfg_attr(not(feature = "logging"), allow(unused_variables, unused_assignments))]`
15//! in `lib.rs` (and the binary entry points) suppresses these expected warnings.
16
17// ---- Feature enabled: re-export tracing ----
18#[cfg(feature = "logging")]
19pub use tracing::{debug, enabled, error, info, trace, warn, Level};
20
21// ---- Feature disabled: no-op macros ----
22//
23// `#[macro_export]` places macros at the crate root. We use prefixed names
24// to avoid clashing with built-in attributes (e.g. `warn`), then re-export
25// them here under the expected names.
26
27#[cfg(not(feature = "logging"))]
28#[macro_export]
29#[doc(hidden)]
30macro_rules! __log_noop_info {
31    ($($arg:tt)*) => {
32        ()
33    };
34}
35
36#[cfg(not(feature = "logging"))]
37#[macro_export]
38#[doc(hidden)]
39macro_rules! __log_noop_warn {
40    ($($arg:tt)*) => {
41        ()
42    };
43}
44
45#[cfg(not(feature = "logging"))]
46#[macro_export]
47#[doc(hidden)]
48macro_rules! __log_noop_debug {
49    ($($arg:tt)*) => {
50        ()
51    };
52}
53
54#[cfg(not(feature = "logging"))]
55#[macro_export]
56#[doc(hidden)]
57macro_rules! __log_noop_error {
58    ($($arg:tt)*) => {
59        ()
60    };
61}
62
63#[cfg(not(feature = "logging"))]
64#[macro_export]
65#[doc(hidden)]
66macro_rules! __log_noop_trace {
67    ($($arg:tt)*) => {
68        ()
69    };
70}
71
72#[cfg(not(feature = "logging"))]
73#[macro_export]
74#[doc(hidden)]
75macro_rules! __log_noop_enabled {
76    ($($arg:tt)*) => {
77        false
78    };
79}
80
81// Re-export under short names so `use crate::logging::info;` works.
82#[cfg(not(feature = "logging"))]
83pub use __log_noop_debug as debug;
84#[cfg(not(feature = "logging"))]
85pub use __log_noop_enabled as enabled;
86#[cfg(not(feature = "logging"))]
87pub use __log_noop_error as error;
88#[cfg(not(feature = "logging"))]
89pub use __log_noop_info as info;
90#[cfg(not(feature = "logging"))]
91pub use __log_noop_trace as trace;
92#[cfg(not(feature = "logging"))]
93pub use __log_noop_warn as warn;
94
95/// Stub for `tracing::Level` when logging is disabled.
96#[cfg(not(feature = "logging"))]
97#[allow(dead_code)]
98pub struct Level;
99
100#[cfg(not(feature = "logging"))]
101#[allow(dead_code)]
102impl Level {
103    /// Debug level stub.
104    pub const DEBUG: Self = Self;
105    /// Info level stub.
106    pub const INFO: Self = Self;
107    /// Warn level stub.
108    pub const WARN: Self = Self;
109    /// Error level stub.
110    pub const ERROR: Self = Self;
111    /// Trace level stub.
112    pub const TRACE: Self = Self;
113}