Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Logging - Part 2

Dependencies that may log data provide support to use defmt or log. Previously, we configured a package to use log, so now it will be quicker, but other aspects can be examined.

As a recap:

  • espflash has built in support for defmt, log or none using the -L flag.
  • esp-println can use defmt-espflash or log-04 features. As their docs state:

    Using the defmt-espflash feature, esp-println will install a defmt global logger.

  • esp-backtrace can use println or defmt features, in order to print panic messages and backtraces.

Let's update the package to use defmt.

Exercise

Go to exercises/defmt directory.

  1. Update the runner's logger in .cargo/config.toml:

    [target.riscv32imac-unknown-none-elf]
    - runner = "espflash flash --monitor"
    + runner = "espflash flash --monitor -L defmt"
    

    so that espflash monitor can decode the format of the defmt messages received.

  2. Update Cargo.toml to include the needed features:

    esp-println = { version = "0.16.0", features = [
        "esp32c6",
    -     "log-04",
    +     "defmt-espflash",
    ] }
    # (...)
    esp-backtrace = { version = "0.18.0", features = [
        "esp32c6",
        "panic-handler",
    -   "println"
    +   "defmt",
    ]}
    
  3. Due to the linking process we need to add defmt linker script to cargo/config.toml:

    rustflags = [
      # ....
    +  "-C", "link-arg=-Tdefmt.x",
    ]
    
  4. Add defmt to the dependencies, and remove log.

  5. Logging level: Use the defmt::println! and some defmt macros to print a few messages.

    • When building the app, set DEFMT_LOG level as done for ESP_LOG earlier (within .cargo/config.toml, under [env] table).
    • An alternative to changing .cargo/config.toml is using DEFMT_LOG=<value> cargo run --release; the same is valid for ESP_LOG.
  6. Add a panic! macro to trigger a panic with a defmt message.

Note

examples/logging_1.rs contains a solution. You can run it with the following command: cargo run --example logging_1 --release. You will need to have the settings above done correctly though!

Suggested reading

Short articles that give more context: