11个不稳定版本 (3个重大更新)

0.3.0 2024年4月18日
0.2.2 2024年1月17日
0.1.1 2023年12月15日
0.0.5 2023年12月14日

#34#shared-memory

Download history 153/week @ 2024-04-14 23/week @ 2024-04-21 3/week @ 2024-04-28 6/week @ 2024-05-12 32/week @ 2024-05-19 26/week @ 2024-05-26 52/week @ 2024-06-02 118/week @ 2024-06-09 63/week @ 2024-06-16 29/week @ 2024-06-23 9/week @ 2024-06-30 52/week @ 2024-07-07 50/week @ 2024-07-14 39/week @ 2024-07-21 120/week @ 2024-07-28

每月261次 下载
用于 9 个crate(8个直接使用)

MIT/Apache

35KB
585

简单的记录器。它有6个可设置的LogLevel,可以通过 [set_log_level()] 设置并通过 [get_log_level()] 读取。

记录器提供方便的宏,可以将错误/恐慌处理直接与记录器结合。`fail!` 宏在调用函数返回包含结果的错误时返回。`fatal_panic!` 宏调用 `panic!`。

示例

记录

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

错误处理

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

恐慌处理

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");
    }
}

在应用程序启动时设置自定义记录器

在这个例子中,我们使用 crate::logger::buffer::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);
}

依赖项

~8-320KB