defmt-logger-rtrb

Crates.iodefmt-logger-rtrb
lib.rsdefmt-logger-rtrb
version1.0.2
created_at2026-01-20 21:44:08.589706+00
updated_at2026-01-21 19:06:20.225755+00
descriptionA defmt global logger based on rtrb ring buffer.
homepage
repositoryhttps://github.com/mcu-rust/defmt-logger-rtrb
max_upload_size
id2057640
size15,184
Jalon Wong (JalonWong)

documentation

https://docs.rs/defmt-logger-rtrb

README

defmt-logger-rtrb

CI Crates.io Docs.rs

A defmt global logger based on rtrb ring buffer.

This crate needs a global allocator. If you are using it on a bare-metal platform, you can use embedded-alloc or heap1 as global allocator.

Usage

cargo add defmt-logger-rtrb

Add following to your .cargo\config.toml:

[target.thumbv7m-none-eabi]
linker = "flip-link"
rustflags = [
    "-C", "link-arg=-Tlink.x",
    "-C", "link-arg=-Tdefmt.x", # add this
]

[env]
DEFMT_LOG = "info" # add this

Your code:

fn main() {
    // Initialize it before any `defmt` interfaces are called.
    let mut log_consumer = defmt_logger_rtrb::init(1024);

    defmt::info!("foo");

    // get log data from buffer and send it via UART or something similar
    loop {
        let n = log_consumer.slots();
        if n > 0 && let Ok(chunk) = log_consumer.read_chunk(n) {
            let (data, _) = chunk.as_slices();
            // send data ...
            chunk.commit(data.len());
        }
    }
}

Global Consumer

You can also use the global log consumer:

fn main() {
    // Initialize it before any `defmt` interfaces are called.
    defmt_logger_rtrb::init_global(1024);

    defmt::info!("foo");

    // get log data from buffer and send it via UART or something similar
    loop {
        if let Some(chunk) = unsafe { defmt_logger_rtrb::get_read_chunk() } {
            let (data, _) = chunk.as_slices();
            // send data ...
            chunk.commit(data.len());
        }
    }
}

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    loop {
        if let Some(chunk) = unsafe { defmt_logger_rtrb::get_read_chunk() } {
            let (data, _) = chunk.as_slices();
            // send data ...
            chunk.commit(data.len());
        }
    }
}
Commit count: 6

cargo fmt