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

84 lines
2.7 KiB
Rust
Raw Normal View History

use crate::{kmain::KERNEL_CONF, time::fetch_time};
use lliw::Fg;
use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
2022-01-16 19:42:11 -06:00
2022-04-11 17:23:11 -05:00
static LOGGER: SimpleLogger = 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
2022-04-11 17:23:11 -05:00
// So that we can log in the kernel and display it in userland
struct SimpleLogger;
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) {
2022-08-01 02:16:19 -05:00
x86_64::instructions::interrupts::without_interrupts(|| {
if self.enabled(record.metadata()) {
let time_float = fetch_time();
use Fg::*;
2022-01-16 19:42:11 -06:00
2022-08-01 02:16:19 -05:00
let color = match record.level() {
log::Level::Error => (Fg::Red, "$RED$"),
log::Level::Warn => (Fg::LightYellow, "$LIGHTYELLOW$"),
log::Level::Info => (Fg::LightWhite, "$LIGHTGRAY$"),
log::Level::Debug => (Fg::Blue, "$BLUE$"),
log::Level::Trace => (Fg::Yellow, "$YELLOW$"),
};
2022-08-01 02:16:19 -05:00
/*
let msg = format!(
"[{}{}$RESET$][$GREEN${}$RESET$]{}\n",
color.1,
2022-03-26 06:35:33 -05:00
record.level(),
time_float,
record.args()
);
2022-08-01 02:16:19 -05:00
*/
let mod_path = match record.module_path() {
Some(p) => {
if KERNEL_CONF.logging.filter.contains(&p.to_string()) {
return;
}
p
}
None => "unknown",
};
let line = match record.line() {
Some(line_number) => line_number,
None => 0,
2022-08-01 02:16:19 -05:00
};
if KERNEL_CONF.logging.log_to_serial {
serial_println!(
"[{}{}{}][{}{}{}][{}{}@{}{}] {}",
color.0,
record.level(),
Fg::Reset,
Fg::Green,
time_float,
Reset,
Fg::Blue,
mod_path,
line,
Reset,
record.args()
);
}
2022-03-26 06:35:33 -05:00
}
2022-08-01 02:16:19 -05:00
});
2022-01-16 19:42:11 -06:00
}
/// Clear the log buffer
2022-01-16 19:42:11 -06:00
fn flush(&self) {}
}
pub fn init() -> Result<(), SetLoggerError> {
log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Trace))
2022-07-31 01:54:01 -05:00
// ?;
// trace!("Logger started");
// Ok(())
2022-01-16 19:42:11 -06:00
}