Crate iceoryx2_log

Crate iceoryx2_log 

Source
Expand description

The Logging API for iceoryx2. It has 6 LogLevels which can be set via set_log_level() and read via get_log_level().

The API includes convinience macros to combine error/panic handling directly with a logger selected from the iceoryx2_loggers crate. 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_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_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_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");
    }
}

Macros§

cerr
cout
debug
Logs a debug message.
error
Logs an error message.
fail
Macro to combine error handling with log messages. It automatically fails and converts the error with From.
fatal_panic
Logs a fatal error message and calls panic.
info
Logs a info message.
trace
Logs a trace message.
warn
Logs a warn message.

Enums§

LogLevel
Log levels from least to most severe

Traits§

Log
Core logging trait to be implemented by loggers

Functions§

get_log_level
Returns the current log level
set_log_level
Sets the current log level. This is ignored for external frameworks like log or tracing. Here you have to use the log-level settings of that framework.
set_logger
Sets the Logger. Can be only called once at the beginning of the program. If the Logger is already set it returns false and does not update it.
stderr
stdout