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:

  • espflash has support for different logging formats, one of them being defmt.
    • espflash requires framming bytes as when using defmt it also needs to print non-defmt messages, like the bootloader prints.
      • It's important to note that other defmt-enabled tools like probe-rs won't be able to parse these messages due to the extra framing bytes.
    • Uses rzcobs encoding
  • esp-println has a defmt-espflash feature, which adds framming bytes so espflash knows that is a defmt message.
  • esp-backtrace has a defmt feature that uses defmt logging 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 log feature of esp-println
    • When building the app, set DEFMT_LOG level.

✅ Add a panic! macro to trigger a panic with a defmt message.