logging: bugfix #137: Init log module to enable logging macros
This commit is contained in:
parent
fcf104f124
commit
6699e80a30
2 changed files with 52 additions and 1 deletions
47
os/src/logging.rs
Normal file
47
os/src/logging.rs
Normal 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,
|
||||||
|
});
|
||||||
|
}
|
|
@ -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();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue