aboutsummaryrefslogtreecommitdiff
path: root/src/log.rs
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-12-05 19:29:52 +0000
committerdiogo464 <[email protected]>2025-12-05 19:29:52 +0000
commit3a3d635adddf5cb16a93827e688e061613a083d7 (patch)
treee6be0a075f80d5cfb11b1882e0086727bfe699e9 /src/log.rs
parentb609a315e7921dcc712da6955890f4dc7c2c4b9f (diff)
reworked logging
Diffstat (limited to 'src/log.rs')
-rw-r--r--src/log.rs115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/log.rs b/src/log.rs
new file mode 100644
index 0000000..1f7bebd
--- /dev/null
+++ b/src/log.rs
@@ -0,0 +1,115 @@
1//! Logging abstraction that works with both defmt and tracing.
2//!
3//! This module provides logging macros that can use either `defmt` (for embedded targets)
4//! or `tracing` (for desktop/testing) depending on the enabled cargo features.
5//!
6//! ## Features
7//!
8//! - `defmt`: Use defmt for logging
9//! - `tracing`: Use tracing for logging
10//! - Neither: Logging is compiled out (no-op)
11//!
12//! ## Usage
13//!
14//! ```rust,ignore
15//! use crate::log::{trace, debug, info, warn, error};
16//!
17//! info!("Application started");
18//! debug!("Value: {}", 42);
19//! warn!("Something unexpected: {:?}", some_value);
20//! ```
21
22// Re-export Format trait when using defmt
23#[cfg(feature = "defmt")]
24pub use defmt::Format;
25
26// For tracing or no logging, we provide a stub Format trait
27#[cfg(not(feature = "defmt"))]
28pub trait Format {}
29
30// When using defmt, also provide Debug2Format for std types
31#[cfg(feature = "defmt")]
32pub use defmt::Debug2Format;
33
34// For tracing or no logging, Debug2Format is a passthrough
35#[cfg(not(feature = "defmt"))]
36#[inline]
37pub fn Debug2Format<T>(value: &T) -> &T {
38 value
39}
40
41// Logging macros that dispatch to the appropriate backend or no-op
42// If both features are enabled, defmt takes precedence
43
44#[macro_export]
45macro_rules! trace {
46 ($($arg:tt)*) => {
47 #[cfg(feature = "defmt")]
48 defmt::trace!($($arg)*);
49
50 #[cfg(all(feature = "tracing", not(feature = "defmt")))]
51 tracing::trace!($($arg)*);
52
53 #[cfg(not(any(feature = "defmt", feature = "tracing")))]
54 { let _ = (); } // no-op
55 };
56}
57
58#[macro_export]
59macro_rules! debug {
60 ($($arg:tt)*) => {
61 #[cfg(feature = "defmt")]
62 defmt::debug!($($arg)*);
63
64 #[cfg(all(feature = "tracing", not(feature = "defmt")))]
65 tracing::debug!($($arg)*);
66
67 #[cfg(not(any(feature = "defmt", feature = "tracing")))]
68 { let _ = (); } // no-op
69 };
70}
71
72#[macro_export]
73macro_rules! info {
74 ($($arg:tt)*) => {
75 #[cfg(feature = "defmt")]
76 defmt::info!($($arg)*);
77
78 #[cfg(all(feature = "tracing", not(feature = "defmt")))]
79 tracing::info!($($arg)*);
80
81 #[cfg(not(any(feature = "defmt", feature = "tracing")))]
82 { let _ = (); } // no-op
83 };
84}
85
86#[macro_export]
87macro_rules! warn {
88 ($($arg:tt)*) => {
89 #[cfg(feature = "defmt")]
90 defmt::warn!($($arg)*);
91
92 #[cfg(all(feature = "tracing", not(feature = "defmt")))]
93 tracing::warn!($($arg)*);
94
95 #[cfg(not(any(feature = "defmt", feature = "tracing")))]
96 { let _ = (); } // no-op
97 };
98}
99
100#[macro_export]
101macro_rules! error {
102 ($($arg:tt)*) => {
103 #[cfg(feature = "defmt")]
104 defmt::error!($($arg)*);
105
106 #[cfg(all(feature = "tracing", not(feature = "defmt")))]
107 tracing::error!($($arg)*);
108
109 #[cfg(not(any(feature = "defmt", feature = "tracing")))]
110 { let _ = (); } // no-op
111 };
112}
113
114// Re-export the macros at the module level for easier use
115pub use crate::{trace, debug, info, warn, error};