Crate defmt_logger_rtrb

Crate defmt_logger_rtrb 

Source
Expand description

§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());
        }
    }
}

Functions§

get_read_chunk
Get log data from global buffer consumer
init
Initialize global logger.
init_global
Initialize global logger and store the buffer consumer as a global static variable. You can then get the log data by calling get_read_chunk.