Skip to content

Commit

Permalink
Merge pull request #10 from thvdveld/fix-logging
Browse files Browse the repository at this point in the history
Fix logging
  • Loading branch information
thvdveld authored Mar 12, 2024
2 parents 677cd61 + 6da4f49 commit 109fdde
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
8 changes: 4 additions & 4 deletions dot15d4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0"
[dependencies]
dot15d4-macros = { path = "../dot15d4-macros" }

tracing = { version = "0.1.38", default-features = false, optional = true }
log = { version = "0.4.21", optional = true }
defmt = { version = "0.3", optional = true }

bitflags = "2.4.2"
Expand All @@ -22,17 +22,17 @@ hex = { version = "0.4.3", optional = true }

[dev-dependencies]
pollster = "0.3"
tracing-subscriber = "0.3"
critical-section = { version = "1.1", features = ["std"] }
env_logger = "0.11.3"

[features]
## Enable std only features
std = ["tracing"]
std = ["log"]
default = ["std"]
dot15d4-cat = ["std", "colored", "clap", "hex"]

## Use tracing for logging
tracing = ["dep:tracing"]
log = ["dep:log"]
## Use defmt for logging
defmt = ["dep:defmt"]

Expand Down
14 changes: 8 additions & 6 deletions dot15d4/examples/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
use dot15d4::frame::*;

fn main() {
env_logger::init();

let data: [u8; 35] = [
0x40, 0xeb, 0xcd, 0xab, 0xff, 0xff, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
0x3f, 0x11, 0x88, 0x06, 0x1a, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x1c, 0x00, 0x01,
0xc8, 0x00, 0x01, 0x1b, 0x00,
];

let frame = Frame::new(&data[..]).unwrap();
println!("{frame}");
log::trace!("{frame}");
let parsed = FrameRepr::parse(&frame);
println!("{parsed:#?}");
log::trace!("{parsed:#?}");

let data = [
0x02, 0x2e, 0x37, 0xcd, 0xab, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x0f,
0xe1, 0x8f,
];
let frame = Frame::new(&data[..]).unwrap();
println!("{frame}");
log::trace!("{frame}");
let parsed = FrameRepr::parse(&frame);
println!("{parsed:#?}");
log::trace!("{parsed:#?}");

let data = [
0x41, 0xd8, 0x01, 0xcd, 0xab, 0xff, 0xff, 0xc7, 0xd9, 0xb5, 0x14, 0x00, 0x4b, 0x12, 0x00,
0x2b, 0x00, 0x00, 0x00,
];
let frame = Frame::new(&data[..]).unwrap();
println!("{frame}");
log::trace!("{frame}");
let parsed = FrameRepr::parse(&frame);
println!("{parsed:#?}");
log::trace!("{parsed:#?}");
}
36 changes: 18 additions & 18 deletions dot15d4/src/utils/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,49 @@
#[macro_export]
macro_rules! error {
($($arg:tt)*) => {
#[cfg(feature = "log-defmt")]
#[cfg(feature = "defmt")]
defmt::error!($($arg)*);
#[cfg(feature = "log-tracing")]
tracing::error!($($arg)*);
#[cfg(feature = "log")]
::log::error!($($arg)*);
};
}

#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {
#[cfg(feature = "log-defmt")]
#[cfg(feature = "defmt")]
defmt::warn!($($arg)*);
#[cfg(feature = "log-tracing")]
tracing::warn!($($arg)*);
#[cfg(feature = "log")]
::log::warn!($($arg)*);
};
}

#[macro_export]
macro_rules! info {
($($arg:tt)*) => {
#[cfg(feature = "log-defmt")]
defmt::info!($($arg)*);
#[cfg(feature = "log-tracing")]
tracing::info!($($arg)*);
#[cfg(feature = "defmt")]
::defmt::info!($($arg)*);
#[cfg(feature = "log")]
::log::info!($($arg)*);
};
}

#[macro_export]
macro_rules! debug {
($($arg:tt)*) => {
#[cfg(feature = "log-defmt")]
defmt::debug!($($arg)*);
#[cfg(feature = "log-tracing")]
tracing::debug!($($arg)*);
#[cfg(feature = "defmt")]
::defmt::debug!($($arg)*);
#[cfg(feature = "log")]
::log::debug!($($arg)*);
};
}

#[macro_export]
macro_rules! trace {
($($arg:tt)*) => {
#[cfg(feature = "log-defmt")]
defmt::trace!($($arg)*);
#[cfg(feature = "log-tracing")]
tracing::trace!($($arg)*);
#[cfg(feature = "defmt")]
::defmt::trace!($($arg)*);
#[cfg(feature = "log")]
::log::trace!($($arg)*);
};
}

0 comments on commit 109fdde

Please sign in to comment.