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 5c06386 commit 44fb3a9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
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,
});
}
6 changes: 5 additions & 1 deletion os/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ extern crate alloc;
#[macro_use]
extern crate bitflags;

use log::*;

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

Expand All @@ -37,6 +39,7 @@ mod console;
mod config;
mod lang_items;
mod loader;
mod logging;
pub mod mm;
mod sbi;
pub mod sync;
Expand Down Expand Up @@ -65,7 +68,8 @@ fn clear_bss() {
/// the rust entry-point of os
pub fn rust_main() -> ! {
clear_bss();
println!("[kernel] Hello, world!");
logging::init();
info!("[kernel] Hello, world!");
mm::init();
mm::remap_test();
task::add_initproc();
Expand Down

0 comments on commit 44fb3a9

Please sign in to comment.