13 unstable releases (4 breaking)

0.4.1 Sep 28, 2024
0.4.0 Sep 28, 2024
0.3.0 Apr 18, 2024
0.2.2 Jan 17, 2024
0.0.5 Dec 14, 2023

#61 in #pub-sub

Download history 61/week @ 2024-08-05 24/week @ 2024-08-12 22/week @ 2024-08-19 66/week @ 2024-08-26 23/week @ 2024-09-02 112/week @ 2024-09-09 37/week @ 2024-09-16 401/week @ 2024-09-23 155/week @ 2024-09-30 97/week @ 2024-10-07 91/week @ 2024-10-14 143/week @ 2024-10-21 106/week @ 2024-10-28 136/week @ 2024-11-04 233/week @ 2024-11-11 363/week @ 2024-11-18

854 downloads per month
Used in 15 crates (13 directly)

MIT/Apache

69KB
1.5K SLoC

Simplistic logger. It has 6 LogLevels which can be set via [set_log_level()] and read via [get_log_level()].

The logger provides convinience macros to combine error/panic handling directly with the logger. The fail! macro can return when the function which was called return an error containing result. The fatal_panic! macro calls panic!.

Example

Logging

use iceoryx2_bb_log::{debug, error, info, trace, warn};

#[derive(Debug)]
struct MyDataType {
    value: u64
}

impl MyDataType {
    fn log_stuff(&self) {
        trace!("trace message");
        trace!(from self, "trace message");
        trace!(from "Custom::Origin", "trace message");

        debug!("hello {} {}", 123, 456);
        debug!(from self, "hello {}", 123);
        debug!(from "Another::Origin", "hello {}", 123);

        info!("world");
        info!(from self, "world");
        info!(from "hello", "world");

        warn!("warn message");
        warn!(from self, "warning");
        warn!(from "Somewhere::Else", "warning!");

        error!("bla {}", 1);
        error!(from self, "bla {}", 1);
        error!(from "error origin", "bla {}", 1);
    }
}

Error Handling

use iceoryx2_bb_log::fail;

#[derive(Debug)]
struct MyDataType {
    value: u64
}

impl MyDataType {
    fn doStuff(&self, value: u64) -> Result<(), ()> {
        if value == 0 { Err(()) } else { Ok(()) }
    }

    fn doMoreStuff(&self) -> Result<(), u64> {
        // fail when doStuff.is_err() and return the error 1234
        fail!(from self, when self.doStuff(0),
                with 1234, "Failed while calling doStuff");
        Ok(())
    }

    fn doMore(&self) -> Result<(), u64> {
        if self.value == 0 {
            // without condition, return error 4567
            fail!(from self, with 4567, "Value is zero");
        }

        Ok(())
    }

    fn evenMore(&self) -> Result<(), u64> {
        // forward error when it is compatible or convertable
        fail!(from self, when self.doMore(), "doMore failed");
        Ok(())
    }
}

Panic Handling

use iceoryx2_bb_log::fatal_panic;

#[derive(Debug)]
struct MyDataType {
    value: u64
}

impl MyDataType {
    fn doStuff(&self, value: u64) {
        if value == 0 {
            fatal_panic!(from self, "value is {}", value);
        }
    }

    fn moreStuff(&self) -> Result<(), ()> {
        if self.value == 0 { Err(()) } else { Ok(()) }
    }

    fn doIt(&self) {
        fatal_panic!(from self, when self.moreStuff(), "moreStuff failed");
    }
}

Setting custom logger on application startup

In this example we use the crate::logger::buffer::Logger, that stores every log message in an internal buffer, and use it as the default logger.

use iceoryx2_bb_log::{set_logger, info};

static LOGGER: iceoryx2_bb_log::logger::buffer::Logger =
    iceoryx2_bb_log::logger::buffer::Logger::new();

assert!(set_logger(&LOGGER));
info!("hello world");
let log_content = LOGGER.content();

for entry in log_content {
    println!("{:?} {} {}", entry.log_level, entry.origin, entry.message);
}

Dependencies

~8–320KB