1
0
Fork 0
forked from AbleOS/ableos
ableos-idl/ableos/src/logger.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

2022-01-25 19:40:37 -06:00
use core::sync::atomic::Ordering;
use crate::kmain::TICK;
2022-01-16 19:42:11 -06:00
use crate::serial_println;
use lliw::{Fg, Reset};
pub use log::{debug, info, trace, warn};
use log::{Level, Metadata, Record};
2022-01-25 19:40:37 -06:00
use crate::arch::drivers::timer::TIMER_INTERRUPT_HERTZ;
2022-01-16 19:42:11 -06:00
struct SimpleLogger;
impl log::Log for SimpleLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Trace
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
2022-01-25 19:40:37 -06:00
let color;
let time = TICK.load(Ordering::Relaxed) as f64;
let time_float = time / TIMER_INTERRUPT_HERTZ;
2022-01-16 19:42:11 -06:00
match record.level() {
log::Level::Error => color = (Fg::Red, "$RED$"),
log::Level::Warn => color = (Fg::LightYellow, "$LIGHTYELLOW$"),
log::Level::Info => color = (Fg::LightWhite, "$LIGHTGRAY$"),
log::Level::Debug => color = (Fg::Blue, "$BLUE$"),
log::Level::Trace => color = (Fg::Yellow, "$YELLOW$"),
}
2022-01-25 19:40:37 -06:00
2022-01-16 19:42:11 -06:00
serial_println!(
2022-01-25 19:40:37 -06:00
"[{}{}{}][{}{}{}] {}",
2022-01-16 19:42:11 -06:00
color.0,
record.level(),
Fg::Reset,
Fg::Green,
2022-01-25 19:40:37 -06:00
time_float,
2022-01-16 19:42:11 -06:00
Reset,
2022-01-25 19:40:37 -06:00
record.args(),
2022-01-16 19:42:11 -06:00
);
}
}
/// Clear the log buffer
2022-01-16 19:42:11 -06:00
fn flush(&self) {}
}
use log::{LevelFilter, SetLoggerError};
static LOGGER: SimpleLogger = SimpleLogger;
pub fn init() -> Result<(), SetLoggerError> {
log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Trace))
}