logging: bugfix #137: Init log module to enable logging macros

This commit is contained in:
Yifan Wu 2025-02-10 00:29:05 +08:00
parent fcf104f124
commit 6699e80a30
2 changed files with 52 additions and 1 deletions

47
os/src/logging.rs Normal file
View file

@ -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,
});
}

View file

@ -31,6 +31,8 @@ extern crate alloc;
#[macro_use] #[macro_use]
extern crate bitflags; extern crate bitflags;
use log::*;
#[path = "boards/qemu.rs"] #[path = "boards/qemu.rs"]
mod board; mod board;
@ -40,6 +42,7 @@ mod config;
mod drivers; mod drivers;
pub mod fs; pub mod fs;
pub mod lang_items; pub mod lang_items;
mod logging;
pub mod mm; pub mod mm;
pub mod sbi; pub mod sbi;
pub mod sync; pub mod sync;
@ -67,7 +70,8 @@ fn clear_bss() {
/// the rust entry-point of os /// the rust entry-point of os
pub fn rust_main() -> ! { pub fn rust_main() -> ! {
clear_bss(); clear_bss();
println!("[kernel] Hello, world!"); logging::init();
info!("[kernel] Hello, world!");
mm::init(); mm::init();
mm::remap_test(); mm::remap_test();
trap::init(); trap::init();