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:
espflashhas built in support fordefmt,logor none using the-Lflag.esp-printlncan usedefmt-espflashorlog-04features. As their docs state:Using the
defmt-espflashfeature,esp-printlnwill install adefmtglobal logger.esp-backtracecan useprintlnordefmtfeatures, in order to print panic messages and backtraces.
Let's update the package to use defmt.
Exercise
Go to exercises/defmt directory.
-
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
espflashmonitor can decode the format of thedefmtmessages received. -
Update
Cargo.tomlto 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", ]} -
Due to the linking process we need to add
defmtlinker script tocargo/config.toml:rustflags = [ # .... + "-C", "link-arg=-Tdefmt.x", ] -
Add defmt to the dependencies, and remove
log. -
Logging level: Use the
defmt::println!and some defmt macros to print a few messages.- When building the app, set
DEFMT_LOGlevel as done forESP_LOGearlier (within.cargo/config.toml, under[env]table). - An alternative to changing
.cargo/config.tomlis usingDEFMT_LOG=<value> cargo run --release; the same is valid forESP_LOG.
- When building the app, set
-
Add a
panic!macro to trigger a panic with adefmtmessage.
Note
examples/logging_1.rscontains 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:
- defmt linking process for setting the compilation-time linker up.
- defmt DEFMT_LOG environment variable.
- esp-println logging formats.