-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logging: bugfix #137: Init log module to enable logging macros
- Loading branch information
Showing
2 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/*! | ||
本模块利用 log crate 为你提供了日志功能,使用方式见 main.rs. | ||
*/ | ||
|
||
use log::{self, Level, LevelFilter, Log, Metadata, Record}; | ||
|
||
struct SimpleLogger; | ||
|
||
impl Log for SimpleLogger { | ||
fn enabled(&self, _metadata: &Metadata) -> bool { | ||
true | ||
} | ||
fn log(&self, record: &Record) { | ||
if !self.enabled(record.metadata()) { | ||
return; | ||
} | ||
let color = match record.level() { | ||
Level::Error => 31, // Red | ||
Level::Warn => 93, // BrightYellow | ||
Level::Info => 34, // Blue | ||
Level::Debug => 32, // Green | ||
Level::Trace => 90, // BrightBlack | ||
}; | ||
println!( | ||
"\u{1B}[{}m[{:>5}] {}\u{1B}[0m", | ||
color, | ||
record.level(), | ||
record.args(), | ||
); | ||
} | ||
fn flush(&self) {} | ||
} | ||
|
||
pub fn init() { | ||
static LOGGER: SimpleLogger = SimpleLogger; | ||
log::set_logger(&LOGGER).unwrap(); | ||
log::set_max_level(match option_env!("LOG") { | ||
Some("ERROR") => LevelFilter::Error, | ||
Some("WARN") => LevelFilter::Warn, | ||
Some("INFO") => LevelFilter::Info, | ||
Some("DEBUG") => LevelFilter::Debug, | ||
Some("TRACE") => LevelFilter::Trace, | ||
_ => LevelFilter::Info, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters