1个不稳定版本
0.1.0 | 2022年5月29日 |
---|
#507 in 调试
3MB
849 行
Prologue Logger
一个用于生成源代码或设置文件的类似Rust的日志的Rust库。
它提供了许多功能,例如:
- 易于使用的构建模式来自定义日志条目;
- 使用类似Rust的
^^^
下划线标注源代码行; - 为多个目标计算警告和错误;
- 将彩色输出到
stderr
(需要console
功能); - 与
log
API集成(需要log
功能); - 与
indicatif
crate集成(需要indicatif
功能); - 使用
console
crate提供颜色支持(需要console
功能)。
用法
最简单的用法是创建一个日志目标并将其日志条目记录到该目标。
use prologue_logger::{Target, Entry, Task};
fn main() -> prologue_logger::error::Result<()> {
// Create a target.
let target = Target::new("my-target");
// Log some task to the above target.
Task::new("Doing", "some work on `my-target`")
.log_to_target(&target);
// Log a more complex entry to the above target.
Entry::new_warning("too lazy to work right now")
// You can add a file reference...
.named_source("my-target.example", 42, 13)
// ... containing a source code line...
.new_line(42, " job.do_work()")
// ... and then underline something on that line...
.annotate_warn(13, 1, "help: add `.await` here")?
// ... and add some other text to help with the warning.
.help("add `.await` to make me do the work")
// Finally, finish the construction of the source code line
// and log it to the target.
.finish()
.log_to_target(&target);
// Log some other task to the above target.
Task::new("Finish", "job on `my-target`")
.log_to_target(&target);
}
示例
examples/simple.rs
功能:console
examples/log.rs
功能:console
,log
examples/file.rs
功能:log
file.log
的内容
warning: starting `file.rs` -- the following is NOT generated by `cargo`
Running example `file.rs`
warning: this is a warning line
--> examples/file.rs:8:36
|
8 | let entry = Entry::new_warning("this is a warning line")
| ----- ------------------ ^^^^^^^^^^^^^^^^^^^^^^^^ this is the text
| | |
| | this is the invoking function
| |
| this is the variable
9 | .bright()
10 | .source(source)
11 | .forward_to_stderr();
| ^^^^^^^^^^^^^^^^^ this function does not increase the warning count
|
= note: this is not the actual source code
= help: to see the actual source code for this example,
see `examples/file.rs`
= note: this output is generated by `prologue-logger` and NOT by `cargo`
examples/indicatif.rs
功能:console
,indicatif
,log