1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
//! A highly efficient logging framework that targets resource-constrained devices, like //! microcontrollers. //! //! Check out the defmt book at <https://defmt.ferrous-systems.com> for more information about how //! to use it. //! //! # Compatibility //! //! The `defmt` wire format might change between major versions. Attempting to read a defmt stream //! with an incompatible version will result in an error. This means that you have to update both //! the host and target side if a breaking change in defmt is released. #![cfg_attr(not(feature = "unstable-test"), no_std)] // NOTE if you change this URL you'll also need to update all other crates in this repo #![doc(html_logo_url = "https://knurling.ferrous-systems.com/knurling_logo_light_text.svg")] #![warn(missing_docs)] #[cfg(feature = "alloc")] extern crate alloc; mod adapter; #[doc(hidden)] pub mod export; mod formatter; mod impls; mod leb; #[cfg(all(test, feature = "unstable-test"))] mod tests; mod traits; pub use crate::{ adapter::{Debug2Format, Display2Format}, formatter::{Formatter, InternalFormatter, Str}, traits::{Format, Logger, Write}, }; #[cfg(all(test, not(feature = "unstable-test")))] compile_error!( "to run unit tests enable the `unstable-test` feature, e.g. `cargo t --features unstable-test`" ); /// Just like the [`core::assert!`] macro but `defmt` is used to log the panic message /// /// [`core::assert!`]: https://doc.rust-lang.org/core/macro.assert.html /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::assert_ as assert; /// Just like the [`core::assert_eq!`] macro but `defmt` is used to log the panic message /// /// [`core::assert_eq!`]: https://doc.rust-lang.org/core/macro.assert_eq.html /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::assert_eq_ as assert_eq; /// Just like the [`core::assert_ne!`] macro but `defmt` is used to log the panic message /// /// [`core::assert_ne!`]: https://doc.rust-lang.org/core/macro.assert_ne.html /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::assert_ne_ as assert_ne; /// Just like the [`core::debug_assert!`] macro but `defmt` is used to log the panic message /// /// [`core::debug_assert!`]: https://doc.rust-lang.org/core/macro.debug_assert.html /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::debug_assert_ as debug_assert; /// Just like the [`core::debug_assert_eq!`] macro but `defmt` is used to log the panic message /// /// [`core::debug_assert_eq!`]: https://doc.rust-lang.org/core/macro.debug_assert_eq.html /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::debug_assert_eq_ as debug_assert_eq; /// Just like the [`core::debug_assert_ne!`] macro but `defmt` is used to log the panic message /// /// [`core::debug_assert_ne!`]: https://doc.rust-lang.org/core/macro.debug_assert_ne.html /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::debug_assert_ne_ as debug_assert_ne; /// Just like the [`core::unreachable!`] macro but `defmt` is used to log the panic message /// /// [`core::unreachable!`]: https://doc.rust-lang.org/core/macro.unreachable.html /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::unreachable_ as unreachable; /// Just like the [`core::todo!`] macro but `defmt` is used to log the panic message /// /// [`core::todo!`]: https://doc.rust-lang.org/core/macro.todo.html /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::todo_ as todo; /// Just like the [`core::unimplemented!`] macro but `defmt` is used to log the panic message /// /// [`core::unimplemented!`]: https://doc.rust-lang.org/core/macro.unimplemented.html /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::todo_ as unimplemented; /// Just like the [`core::panic!`] macro but `defmt` is used to log the panic message /// /// [`core::panic!`]: https://doc.rust-lang.org/core/macro.panic.html /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::panic_ as panic; /// Unwraps an `Option` or `Result`, panicking if it is `None` or `Err`. /// /// This macro is roughly equivalent to `{Option,Result}::{expect,unwrap}` but invocation looks /// a bit different because this is a macro and not a method. The other difference is that /// `unwrap!`-ing a `Result<T, E>` value requires that the error type `E` implements the `Format` /// trait /// /// The following snippet shows the differences between core's unwrap method and defmt's unwrap /// macro: /// /// ``` /// use defmt::unwrap; /// /// # let option = Some(()); /// let x = option.unwrap(); /// let x = unwrap!(option); /// /// # let result = Ok::<(), ()>(()); /// let x = result.unwrap(); /// let x = unwrap!(result); /// /// # let value = result; /// let x = value.expect("text"); /// let x = unwrap!(value, "text"); /// /// # let arg = (); /// let x = value.expect(&format!("text {:?}", arg)); /// let x = unwrap!(value, "text {:?}", arg); // arg must be implement `Format` /// ``` /// /// If used, the format string must follow the defmt syntax (documented in [the manual]) /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::unwrap; /// Overrides the panicking behavior of `defmt::panic!` /// /// By default, `defmt::panic!` calls `core::panic!` after logging the panic message using `defmt`. /// This can result in the panic message being printed twice in some cases. To avoid that issue use /// this macro. See [the manual] for details. /// /// [the manual]: https://defmt.ferrous-systems.com/panic.html /// /// # Inter-operation with built-in attributes /// /// This attribute cannot be used together with the `export_name` or `no_mangle` attributes pub use defmt_macros::panic_handler; /// Creates an interned string ([`Str`]) from a string literal. /// /// This must be called on a string literal, and will allocate the literal in the object file. At /// runtime, only a small string index is required to refer to the string, represented as the /// [`Str`] type. /// /// # Example /// /// ``` /// let interned = defmt::intern!("long string literal taking up little space"); /// ``` /// /// [`Str`]: struct.Str.html pub use defmt_macros::intern; /// Logs data at *debug* level. /// /// Please refer to [the manual] for documentation on the syntax. /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::debug; /// Logs data at *error* level. /// /// Please refer to [the manual] for documentation on the syntax. /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::error; /// Logs data at *info* level. /// /// Please refer to [the manual] for documentation on the syntax. /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::info; /// Logs data at *trace* level. /// /// Please refer to [the manual] for documentation on the syntax. /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::trace; /// Logs data at *warn* level. /// /// Please refer to [the manual] for documentation on the syntax. /// /// [the manual]: https://defmt.ferrous-systems.com/macros.html pub use defmt_macros::warn; /// Just like the [`std::dbg!`] macro but `defmt` is used to log the message at `TRACE` level. /// /// [`std::dbg!`]: https://doc.rust-lang.org/std/macro.dbg.html pub use defmt_macros::dbg; /// Writes formatted data to a [`Formatter`]. /// /// [`Formatter`]: struct.Formatter.html pub use defmt_macros::write; /// Defines the global defmt logger. /// /// `#[global_logger]` needs to be put on a unit struct type declaration. This struct has to /// implement the [`Logger`] trait. /// /// # Example /// /// ``` /// use defmt::{Logger, Write, global_logger}; /// use core::ptr::NonNull; /// /// #[global_logger] /// struct MyLogger; /// /// unsafe impl Logger for MyLogger { /// fn acquire() -> Option<NonNull<dyn Write>> { /// # todo!() /// // ... /// } /// unsafe fn release(writer: NonNull<dyn Write>) { /// # todo!() /// // ... /// } /// } /// ``` /// /// [`Logger`]: trait.Logger.html pub use defmt_macros::global_logger; /// Defines the global timestamp provider for defmt. /// /// This macro can be used to attach a timestamp or other data to every defmt message. Its syntax /// works exactly like the logging macros, except that no local variables can be accessed and the /// macro should be placed in a module instead of a function. /// /// `timestamp!` must only be used once across the crate graph. /// /// If no crate defines a timestamp, no timestamp will be included in the logged messages. /// /// # Examples /// /// ``` /// # use core::sync::atomic::{AtomicU32, Ordering}; /// /// static COUNT: AtomicU32 = AtomicU32::new(0); /// defmt::timestamp!("{=u32:µs}", COUNT.fetch_add(1, Ordering::Relaxed)); /// ``` pub use defmt_macros::timestamp; #[doc(hidden)] // documented as the `Format` trait instead pub use defmt_macros::Format; #[export_name = "__defmt_default_timestamp"] fn default_timestamp(_f: Formatter<'_>) { // By default, no timestamp is used. } // There is no default timestamp format. Instead, the decoder looks for a matching ELF symbol. If // absent, timestamps are turned off. #[export_name = "__defmt_default_panic"] fn default_panic() -> ! { core::panic!() }