#logging #defmt

no-std defmt-or-log

Logging macros that forward to either defmt or log

6 releases

0.2.3 Oct 29, 2025
0.2.2 Jun 25, 2025
0.2.1 Jan 16, 2024
0.2.0 Nov 20, 2023
0.1.1 Nov 20, 2023

#181 in Embedded development

Download history 1622/week @ 2025-08-12 2297/week @ 2025-08-19 2395/week @ 2025-08-26 1671/week @ 2025-09-02 2367/week @ 2025-09-09 2820/week @ 2025-09-16 1919/week @ 2025-09-23 1804/week @ 2025-09-30 2215/week @ 2025-10-07 2994/week @ 2025-10-14 3142/week @ 2025-10-21 2364/week @ 2025-10-28 2524/week @ 2025-11-04 3040/week @ 2025-11-11 3095/week @ 2025-11-18 1944/week @ 2025-11-25

11,007 downloads per month
Used in 14 crates (9 directly)

MIT/Apache

16KB
336 lines

defmt-or-log

In embedded projects you often want to log something either via core::fmt::Debug or defmt::Format, depending on the configuration. The crate defmt-or-log provides the usual logger macros and either forwards them to the log or defmt crate, depending on the enabled features.

Furthermore, the defmt-or-log crate provides:

  • the FormatOrDebug trait, that uses core::fmt::Debug or defmt::Format as super-trait and provides matching blanket implementations. This is useful for specifying trait bounds.
  • the Debug2Format struct, which behaves similar to defmt::Debug2Format, but does nothing extra if using log.
  • the Display2Format struct, which behaves similar to defmt::Display2Format, but does nothing extra if using log.
  • the Hex and Cbor structs, which allow formatting &[u8]-slices with a format specifier that works for log and defmt:
    info!("Found bytes {}", Hex(&data)) instead of {:02x} (defmt) resp {:02x?} (log).

See examples/derive.rs.

You may either enable the log or the defmt feature but not both.
The feature at_least_one (enabled per default) generates a compiler error if neither log nor defmt are enabled.

This repository, also provides the defmt-or-log-macros crate, that contains Proc Macros to conditonally derive core::fmt::Debug or defmt::Format, depending on the enabled features.

Supported macros (defmt-or-log crate):

  • trace

  • debug

  • info

  • warn

  • error

  • assert

  • assert_eq

  • assert_ne

  • debug_assert

  • debug_assert_eq

  • debug_assert_ne

  • todo

  • unreachable

  • panic

  • unwrap

  • intern

  • expect

  • unimplemented

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~160–650KB
~15K SLoC