chore: support colorful log

This commit is contained in:
Paul Pan 2023-09-23 21:42:38 +08:00
parent 177ac3aea8
commit 09684652c6
2 changed files with 22 additions and 2 deletions

View File

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["arch_riscv64", "board_virt"]
default = ["arch_riscv64", "board_virt", "log_color"]
arch_riscv64 = []
arch_riscv32 = []
arch_arm = []
@ -14,6 +14,7 @@ arch_x86 = []
board_default = []
board_virt = []
board_thead = []
log_color = []
[profile.dev]
panic = "abort"

View File

@ -14,8 +14,27 @@ impl Log for SimpleLogger {
return;
}
let (color_prefix, color_reset) = if cfg!(feature = "log_color") {
let color = match record.level() {
log::Level::Error => "\x1b[31m",
log::Level::Warn => "\x1b[33m",
log::Level::Info => "\x1b[32m",
log::Level::Debug => "\x1b[34m",
log::Level::Trace => "\x1b[37m",
};
(color, "\x1b[0m")
} else {
("", "")
};
RawConsole
.write_fmt(format_args!("[{}] {}\n", record.level(), record.args()))
.write_fmt(format_args!(
"{}[{}] {}{}\n",
color_prefix,
record.level(),
record.args(),
color_reset
))
.unwrap();
}