Skip to content

Commit

Permalink
logging: bugfix #137: Init log module to enable logging macros
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfcyx committed Feb 10, 2025
1 parent fb2a6a0 commit 81f9e8b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
47 changes: 47 additions & 0 deletions os/src/logging.rs
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,
});
}
12 changes: 8 additions & 4 deletions os/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ extern crate alloc;
#[macro_use]
extern crate bitflags;

use log::*;

#[path = "boards/qemu.rs"]
mod board;

Expand All @@ -19,6 +21,7 @@ mod config;
mod drivers;
mod fs;
mod lang_items;
mod logging;
mod mm;
mod net;
mod sbi;
Expand Down Expand Up @@ -55,15 +58,16 @@ lazy_static! {
#[no_mangle]
pub fn rust_main() -> ! {
clear_bss();
logging::init();
mm::init();
UART.init();
println!("KERN: init gpu");
info!("KERN: init gpu");
let _gpu = GPU_DEVICE.clone();
println!("KERN: init keyboard");
info!("KERN: init keyboard");
let _keyboard = KEYBOARD_DEVICE.clone();
println!("KERN: init mouse");
info!("KERN: init mouse");
let _mouse = MOUSE_DEVICE.clone();
println!("KERN: init trap");
info!("KERN: init trap");
trap::init();
trap::enable_timer_interrupt();
timer::set_next_trigger();
Expand Down

0 comments on commit 81f9e8b

Please sign in to comment.