使用旧的 Rust 2015
0.1.0 |
|
---|
6 在 #offering
7KB
121 行
分层日志记录器
对 log crate 的简单包装,允许在运行时为将来的调试宏调用添加任意信息。
背后的魔法是一个 thread_local RefCell<Vec<String>>
,当填充时将包含包含的标识符,用 ": " 分隔。
#[macro_use] extern crate log;
#[macro_use] extern crate scoped_log;
extern crate env_logger;
fn main () {
let _ = env_logger::init();
scoped_info!("1");
{
push_log_scope!("outer-scope");
scoped_info!("2: {}", "some args");
if false { // Enable to test assert
scoped_assert!(false);
scoped_assert!(false, "I failed!");
}
push_log_scope!("inner-scope");
scoped_info!(target: "some-target", "2: {}", "some args");
Foo.foo();
}
scoped_info!("4");
}
#[derive(Debug)]
struct Foo;
impl Foo {
fn foo(&self) {
push_log_scope!("{:?}-scope", self);
scoped_info!("3");
}
}
以下是从: cargo run --example example && RUST_LOG=example=trace target/debug/examples/example
Running `target/debug/examples/example`
INFO:example: 1
INFO:example: outer-scope: 2: some args
INFO:example: outer-scope: inner-scope: Foo-scope: 3
INFO:example: 4
依赖项
~87KB