defmt
In this chapter, we will cover defmt, a highly efficient logging framework, and how to use it in the no_std environment.
defmt Ecosystem
esp-println, esp-backtrace and espflash/cargo-espflash provide mechanisms to use defmt:
espflashhas support for different logging formats, one of them beingdefmt.espflashrequires framming bytes as when usingdefmtit also needs to print non-defmtmessages, like the bootloader prints.- It's important to note that other
defmt-enabled tools likeprobe-rswon't be able to parse these messages due to the extra framing bytes.
- It's important to note that other
- Uses rzcobs encoding
esp-printlnhas adefmt-espflashfeature, which adds framming bytes soespflashknows that is adefmtmessage.esp-backtracehas adefmtfeature that usesdefmtlogging to print panic and exception handler messages.
Setup
✅ Go to intro/defmt directory.
✅ Open the prepared project skeleton in intro/defmt.
intro/defmt/examples/defmt.rs contains the solution. You can run it with the following command:
cargo run --release --example defmt
Exercise
✅ Make sure the defmt-espflash feature of esp-println is enabled.
✅ Make sure the defmt feature of esp-backtrace is enabled.
✅ Update the linking process in the .cargo/config.toml.
✅ Make sure, the defmt crate is added to the dependencies.
✅ Make sure you are building esp_println and esp_backtrace
use esp_backtrace as _;
use esp_println as _;
✅ Use the defmt::println! or any of the logging defmt macros to print a message.
- If you want to use any of the logging macros like
info,debug- Enable the
logfeature ofesp-println - When building the app, set
DEFMT_LOGlevel.
- Enable the
✅ Add a panic! macro to trigger a panic with a defmt message.