defmt in a library you only need to add
defmt as a dependency.
$ cargo add defmt
defmt in an application you need the additional steps documented below.
💡 The prefferred way to create a new
defmtapplication is to use our app-template. Tag along if you want to add
defmtto an existing application.
The application must be linked using a custom linking process that includes the
defmt.x linker script.
Custom linking is usual for embedded applications and configured in the
defmt.x to the linker add the
-C link-arg=-Tdefmt.x flag to the rustflags section of
# .cargo/config.toml [target.thumbv7m-none-eabi] rustflags = [ # --- KEEP existing `link-arg` flags --- "-C", "link-arg=-Tlink.x", "-C", "link-arg=--nmagic", # --- ADD following new flag --- "-C", "link-arg=-Tdefmt.x", ]
The application must link to or define a
global_logger specifies how logs are sent from the device running the app to the host where the logs are displayed.
The application must link to exactly one
global_logger can appear anywhere in the dependency graph and usually it will be its own crate.
global_loggers are provided as part of the project:
defmt-rtt, logs over RTT. Note that this crate can not be used together with
defmt-itm, logs over ITM (Instrumentation Trace Macrocell) stimulus port 0.
defmt-semihosting, logs over semihosting. Meant only for testing
defmton a virtual Cortex-M device (QEMU).
Information about how to write a
global_logger can be found in the
By default, only ERROR level messages are logged. To learn how to enable other logging levels and filters logs per modules read the Filtering section.
When in a tight memory situation and logging over RTT, the buffer size (default: 1024 bytes) can be configured with the
DEFMT_RTT_BUFFER_SIZE environment variable. Use a power of 2 for best performance.