2022-03-26 11:35:33 +00:00
|
|
|
use crate::kmain::KERNEL_CONF;
|
2022-03-11 19:51:47 +00:00
|
|
|
use crate::network::socket::{SimpleSock, Socket};
|
2022-03-11 23:13:41 +00:00
|
|
|
use crate::time::fetch_time;
|
2022-07-31 06:54:01 +00:00
|
|
|
use alloc::borrow::ToOwned;
|
2022-01-17 01:42:11 +00:00
|
|
|
use lliw::{Fg, Reset};
|
|
|
|
use log::{Level, Metadata, Record};
|
2022-04-11 22:23:11 +00:00
|
|
|
use log::{LevelFilter, SetLoggerError};
|
2022-01-17 01:42:11 +00:00
|
|
|
|
2022-04-11 22:23:11 +00:00
|
|
|
static LOGGER: SimpleLogger = SimpleLogger;
|
2022-02-07 13:33:40 +00:00
|
|
|
// TODO: Rebuild this to take advantage of sockets
|
|
|
|
// DETAIL: Log to a socket instead of the screen
|
2022-04-11 22:23:11 +00:00
|
|
|
// So that we can log in the kernel and display it in userland
|
|
|
|
struct SimpleLogger;
|
2022-01-17 01:42:11 +00: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-03-11 23:13:41 +00:00
|
|
|
let time_float = fetch_time();
|
2022-07-31 06:54:01 +00:00
|
|
|
use log::Level::*;
|
|
|
|
use Fg::*;
|
2022-01-17 01:42:11 +00:00
|
|
|
|
2022-04-11 20:51:54 +00: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-07-31 06:54:01 +00:00
|
|
|
/*
|
2022-03-11 19:51:47 +00:00
|
|
|
let msg = format!(
|
|
|
|
"[{}{}$RESET$][$GREEN${}$RESET$]{}\n",
|
|
|
|
color.1,
|
|
|
|
record.level(),
|
|
|
|
time_float,
|
|
|
|
record.args()
|
|
|
|
);
|
2022-07-31 06:54:01 +00:00
|
|
|
*/
|
|
|
|
let mod_path = match record.module_path() {
|
|
|
|
Some(p) => p,
|
|
|
|
None => "unknown",
|
|
|
|
};
|
|
|
|
|
|
|
|
let line = match record.line() {
|
|
|
|
Some(line_number) => line_number.to_string(),
|
|
|
|
None => "??".to_string(),
|
|
|
|
};
|
|
|
|
|
2022-03-26 11:35:33 +00:00
|
|
|
if KERNEL_CONF.logging.log_to_serial {
|
|
|
|
serial_println!(
|
2022-07-31 06:54:01 +00:00
|
|
|
"[{}{}{}][{}{}{}][{}{}@{}{}] {}",
|
2022-03-26 11:35:33 +00:00
|
|
|
color.0,
|
|
|
|
record.level(),
|
|
|
|
Fg::Reset,
|
|
|
|
Fg::Green,
|
|
|
|
time_float,
|
|
|
|
Reset,
|
2022-07-31 06:54:01 +00:00
|
|
|
Fg::Blue,
|
|
|
|
mod_path,
|
|
|
|
line,
|
|
|
|
Reset,
|
2022-03-26 11:35:33 +00:00
|
|
|
record.args()
|
|
|
|
);
|
|
|
|
}
|
2022-01-17 01:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 01:43:03 +00:00
|
|
|
/// Clear the log buffer
|
2022-01-17 01:42:11 +00:00
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init() -> Result<(), SetLoggerError> {
|
|
|
|
log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Trace))
|
2022-07-31 06:54:01 +00:00
|
|
|
// ?;
|
|
|
|
|
|
|
|
// trace!("Logger started");
|
|
|
|
|
|
|
|
// Ok(())
|
2022-01-17 01:42:11 +00:00
|
|
|
}
|