1 个不稳定版本
0.1.0 | 2024 年 6 月 12 日 |
---|
155 在 性能分析
7KB
51 行
Trace_Err
是 tracingcrate 的小型扩展,为 core::result::Result<T, E>
和 core::option::Option<T>
提供了一个单一方法。
为 Result
添加了 trace_expect
,除了解包/期望 Result
外,还调用了 tracing::error!
宏(如果为 Result::Err
)。
为 Option
添加了 trace_expect
,除了解包/期望 Option
外,还调用了 tracing::error!
宏(如果为 Option::None
)。
无耻地借鉴了 Log_Err crate。
简写为
use tracing::error;
fn something() -> Result<(), &'static str> {Err("there was some problem")}
let msg = "Some message";
something().map_err(|e| tracing::error!("{}: {:?}", msg, e)).expect(msg)
示例
use std::fs::File;
use trace_err::*;
let mut file = File::open("foo.txt").trace_expect("Error creating file");
# Error will be traced with the error! macro
2024-06-12T09:31:23.933299Z ERROR expect: trace-err/lib.rs:87:39: Error creating file: Os { code: 2, kind: NotFound, message: "No such file or directory" }
# Main program panic'ing with same message
thread 'main' panicked at trace-err/lib.rs:87:39:
Error creating file: Os { code: 2, kind: NotFound, message: "No such file or directory" }
依赖
~310–415KB