Skip to main content

ra_hal/
fmt.rs

1#![macro_use]
2
3#[doc(hidden)]
4#[macro_export]
5macro_rules! maybe_log {
6    ($level:ident, $s:literal $(, $x:expr)* $(,)?) => {
7        {
8        #[cfg(feature = "defmt")]
9        ::defmt::$level!($s $(, $x)*);
10        #[cfg(not(feature="defmt"))]
11        let _ = ($( & $x ),*);
12        }
13    };
14}
15
16/// Expands to `defmt::assert_eq!` if the `defmt` feature is enabled, core impl otherwise.
17#[macro_export]
18#[collapse_debuginfo(yes)]
19macro_rules! assert_eq {
20    ($($x:tt)*) => {
21        {
22            #[cfg(not(feature = "defmt"))]
23            ::core::assert_eq!($($x)*);
24            #[cfg(feature = "defmt")]
25            ::defmt::assert_eq!($($x)*);
26        }
27    };
28}
29
30/// Log at the `trace` level if the `defmt` feature is enabled. No-op otherwise.
31#[macro_export]
32#[collapse_debuginfo(yes)]
33macro_rules! trace {
34    ($s:literal $(, $x:expr)* $(,)?) => {
35        {
36            $crate::maybe_log!(trace, $s $(, $x)*);
37        }
38    };
39}
40
41/// Log at the `debug` level if the `defmt` feature is enabled. No-op otherwise.
42#[macro_export]
43#[collapse_debuginfo(yes)]
44macro_rules! debug {
45    ($s:literal $(, $x:expr)* $(,)?) => {
46        {
47            $crate::maybe_log!(debug, $s $(, $x)*);
48        }
49    };
50}
51
52/// Log at the `info` level if the `defmt` feature is enabled. No-op otherwise.
53#[macro_export]
54#[collapse_debuginfo(yes)]
55macro_rules! info {
56    ($s:literal $(, $x:expr)* $(,)?) => {
57        {
58            $crate::maybe_log!(info, $s $(, $x)*);
59        }
60    };
61}
62
63/// Log at the `warn` level if the `defmt` feature is enabled. No-op otherwise.
64#[macro_export]
65#[collapse_debuginfo(yes)]
66macro_rules! warn {
67    ($s:literal $(, $x:expr)* $(,)?) => {
68        {
69            $crate::maybe_log!(warn, $s $(, $x)*);
70        }
71    };
72}
73
74/// Log at the `error` level if the `defmt` feature is enabled. No-op otherwise.
75#[macro_export]
76#[collapse_debuginfo(yes)]
77macro_rules! error {
78    ($s:literal $(, $x:expr)* $(,)?) => {
79        {
80            $crate::maybe_log!(error, $s $(, $x)*);
81        }
82    };
83}