Skip to main content

aimdb_core/
log.rs

1//! Crate-private logging macros.
2//!
3//! `log_trace!`/`log_debug!`/`log_info!`/`log_warn!`/`log_error!` forward to every enabled
4//! *destination* — the `tracing` event macro under the `tracing` feature, the
5//! `log` crate under the `log` feature — and otherwise expand to a no-op that
6//! still borrows the arguments, so call sites compile identically (no
7//! unused-variable warnings) under every feature combination. This replaces the
8//! per-call-site `#[cfg(feature = "tracing")]` gates.
9//!
10//! Notes:
11//! - The no-op branch *borrows* (and therefore evaluates) the arguments — keep
12//!   them cheap (getters, lengths, references), never allocate in hot paths.
13//! - `defmt` is intentionally not folded in: most call sites use `{:?}` with
14//!   types that do not implement `defmt::Format` (e.g. `DbError`, `String`,
15//!   `Vec<String>`). The few sites that mirror events to defmt (router.rs)
16//!   keep explicit `#[cfg(feature = "defmt")]` gates next to these macros.
17//!
18//! # Mirroring the feature
19//!
20//! These macros are `#[macro_export]`ed, so `#[cfg(feature = ...)]` inside them
21//! resolves against the *calling* crate. A crate expanding the facade must
22//! declare a feature of the same name and forward it, or its events silently go
23//! nowhere:
24//!
25//! ```toml
26//! tracing = ["aimdb-core/tracing"]   # no `dep:tracing`
27//! log     = ["aimdb-core/log"]       # no `dep:log`
28//! ```
29//!
30//! Neither arm needs the dependency declared: both go through `$crate::__private`.
31//! The *feature* is what cannot be dropped — a `#[cfg]` in a `#[macro_export]`ed
32//! macro is resolved where it expands, so a crate without it would emit nothing.
33//!
34//! That is not a silent failure any more: an undeclared feature name makes
35//! `unexpected_cfgs` fire at every call site, which is a warning in an ordinary
36//! build and an error under `-D warnings`, as `make clippy` runs it. A dropped
37//! mirror therefore breaks CI rather than quietly muting a crate.
38//! `aimdb-sync/tests/log_facade.rs` additionally proves delivery end to end.
39
40#[macro_export]
41macro_rules! log_trace {
42    ($s:literal $(, $x:expr)* $(,)?) => {{
43        #[cfg(feature = "tracing")]
44        $crate::__private::tracing::trace!($s $(, $x)*);
45        #[cfg(feature = "log")]
46        $crate::__private::log::trace!($s $(, $x)*);
47        #[cfg(not(any(feature = "tracing", feature = "log")))]
48        { let _ = ($( & $x ),*); }
49    }};
50}
51
52#[macro_export]
53macro_rules! log_debug {
54    ($s:literal $(, $x:expr)* $(,)?) => {{
55        #[cfg(feature = "tracing")]
56        $crate::__private::tracing::debug!($s $(, $x)*);
57        #[cfg(feature = "log")]
58        $crate::__private::log::debug!($s $(, $x)*);
59        #[cfg(not(any(feature = "tracing", feature = "log")))]
60        { let _ = ($( & $x ),*); }
61    }};
62}
63
64#[macro_export]
65macro_rules! log_info {
66    ($s:literal $(, $x:expr)* $(,)?) => {{
67        #[cfg(feature = "tracing")]
68        $crate::__private::tracing::info!($s $(, $x)*);
69        #[cfg(feature = "log")]
70        $crate::__private::log::info!($s $(, $x)*);
71        #[cfg(not(any(feature = "tracing", feature = "log")))]
72        { let _ = ($( & $x ),*); }
73    }};
74}
75
76#[macro_export]
77macro_rules! log_warn {
78    ($s:literal $(, $x:expr)* $(,)?) => {{
79        #[cfg(feature = "tracing")]
80        $crate::__private::tracing::warn!($s $(, $x)*);
81        #[cfg(feature = "log")]
82        $crate::__private::log::warn!($s $(, $x)*);
83        #[cfg(not(any(feature = "tracing", feature = "log")))]
84        { let _ = ($( & $x ),*); }
85    }};
86}
87
88#[macro_export]
89macro_rules! log_error {
90    ($s:literal $(, $x:expr)* $(,)?) => {{
91        #[cfg(feature = "tracing")]
92        $crate::__private::tracing::error!($s $(, $x)*);
93        #[cfg(feature = "log")]
94        $crate::__private::log::error!($s $(, $x)*);
95        #[cfg(not(any(feature = "tracing", feature = "log")))]
96        { let _ = ($( & $x ),*); }
97    }};
98}