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

73 lines
2.2 KiB
Rust
Raw Normal View History

2022-01-25 19:40:37 -06:00
use core::sync::atomic::Ordering;
use crate::network::socket::{SimpleSock, Socket};
2022-03-11 14:18:07 -06:00
// use crate::serial_println;
use kernel::TICK;
2022-01-16 19:42:11 -06:00
use lliw::{Fg, Reset};
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$"),
}
let msg = format!(
"[{}{}$RESET$][$GREEN${}$RESET$]{}\n",
color.1,
record.level(),
time_float,
record.args()
);
2022-03-11 14:18:07 -06:00
// NOTE: This needs to be fixed before merge
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-02-18 10:04:10 -06:00
record.args()
2022-01-16 19:42:11 -06:00
);
let log_socket_id = SimpleSock::grab_socket("Logger".to_string());
match log_socket_id {
Some(mut log_socket_id) => {
log_socket_id.write(msg.as_bytes().to_vec());
}
None => warn!("No socket found for Logger"),
}
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))
}