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;
2022-01-16 19:42:11 -06:00
use crate::serial_println;
use kernel::TICK;
2022-01-16 19:42:11 -06:00
use lliw::{Fg, Reset};
pub use log::{debug, info, trace, warn};
use log::{Level, Metadata, Record};
struct SimpleLogger;
2022-02-07 07:33:40 -06:00
// TODO: Rebuild this to take advantage of sockets
// DETAIL: Log to a socket instead of the screen
// So that we can log in the kernel and display it in userland
2022-01-16 19:42:11 -06:00
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;
2022-02-19 07:17:44 -06:00
let time_float = time;
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))
}