1
0
Fork 0
forked from AbleOS/ableos
ableos_time/ableos/src/log.rs

139 lines
3.4 KiB
Rust
Raw Normal View History

2021-11-27 09:19:08 -06:00
pub trait Log {
2021-12-24 03:30:27 -06:00
fn debug(val: &str);
2021-12-24 03:40:11 -06:00
fn error(val: &str);
2021-12-24 03:30:27 -06:00
fn info(val: &str);
2021-12-24 03:40:11 -06:00
fn trace(val: &str);
2021-11-27 09:19:08 -06:00
}
2021-12-24 07:03:15 -06:00
pub struct LogState {
2021-12-28 02:56:29 -06:00
pub log_to_serial: bool,
pub log_to_screen: bool,
2021-12-24 07:03:15 -06:00
pub debug: bool,
pub error: bool,
pub info: bool,
pub trace: bool,
}
lazy_static::lazy_static! {
2021-12-28 02:56:29 -06:00
pub static ref LOG_STATE: Mutex<LogState> = spin::Mutex::new(LogState {
log_to_screen: true,
log_to_serial: true,
2021-12-24 07:03:15 -06:00
debug: true,
error: true,
info: true,
trace: true,
2021-12-28 02:56:29 -06:00
});
2021-12-24 07:03:15 -06:00
}
2021-11-27 09:19:08 -06:00
2021-12-28 02:56:29 -06:00
use crate::serial_println;
use lliw::{Fg, Reset};
2021-12-28 02:56:29 -06:00
use spin::Mutex;
2021-11-27 09:19:08 -06:00
pub struct ANSISerialLogger;
impl Log for ANSISerialLogger {
2021-12-24 03:30:27 -06:00
fn debug(val: &str) {
2021-12-28 02:56:29 -06:00
if LOG_STATE.lock().log_to_serial {
2021-12-30 03:32:32 -06:00
serial_println!(
"[{}Debug{}][{}FakeTempTime{}] {}",
Fg::Blue,
Reset,
Fg::Green,
Reset,
val
);
2021-12-28 02:56:29 -06:00
}
if LOG_STATE.lock().log_to_screen {
2021-12-30 03:32:32 -06:00
println!("[$BLUE$Debug$RESET$][$GREEN$FakeTempTime$RESET$] {}", val);
2021-12-28 02:56:29 -06:00
}
2021-11-27 09:19:08 -06:00
}
2021-12-24 03:40:11 -06:00
fn error(val: &str) {
2021-12-28 02:56:29 -06:00
if LOG_STATE.lock().log_to_serial {
2021-12-30 03:32:32 -06:00
serial_println!(
"[{}Error{}][{}FakeTempTime{}] {}",
Fg::Red,
Reset,
Fg::Green,
Reset,
val
);
2021-12-28 02:56:29 -06:00
}
if LOG_STATE.lock().log_to_screen {
2021-12-30 03:32:32 -06:00
println!("[$RED$Error$RESET$][$GREEN$FakeTempTime$RESET$] {}", val);
2021-12-28 02:56:29 -06:00
}
2021-11-27 09:19:08 -06:00
}
2021-12-24 03:30:27 -06:00
fn info(val: &str) {
2021-12-28 02:56:29 -06:00
if LOG_STATE.lock().log_to_serial {
2021-12-30 03:32:32 -06:00
serial_println!(
"[{}Info{} ][{}FakeTempTime{}] {}",
Fg::LightWhite,
Reset,
Fg::Green,
Reset,
val
);
2021-12-28 02:56:29 -06:00
}
if LOG_STATE.lock().log_to_screen {
2021-12-30 03:32:32 -06:00
println!(
"[$LIGHTGRAY$Info$RESET$ ][$GREEN$FakeTempTime$RESET$] {}",
val
);
2021-12-28 02:56:29 -06:00
}
2021-11-27 09:19:08 -06:00
}
2021-12-24 03:40:11 -06:00
fn trace(val: &str) {
2021-12-28 02:56:29 -06:00
if LOG_STATE.lock().log_to_serial {
2021-12-30 03:32:32 -06:00
serial_println!(
"[{}Trace{}][{}FakeTempTime{}] {}",
Fg::Yellow,
Reset,
Fg::Green,
Reset,
val
);
2021-12-28 02:56:29 -06:00
}
if LOG_STATE.lock().log_to_screen {
2021-12-30 03:32:32 -06:00
println!("[$YELLOW$Trace$RESET$][$GREEN$FakeTempTime$RESET$] {}", val);
2021-12-28 02:56:29 -06:00
}
2021-11-27 09:19:08 -06:00
}
2021-12-24 03:39:24 -06:00
}
#[macro_export]
macro_rules! debug {
($($arg:tt)*) => ({
2021-12-28 02:56:29 -06:00
if $crate::log::LOG_STATE.lock().debug{
2022-01-01 18:06:46 -06:00
use crate::log::Log;
2021-12-24 07:03:15 -06:00
2022-01-01 18:06:46 -06:00
let _ = crate::log::ANSISerialLogger::debug(&alloc::format!($($arg)*));
2021-12-24 07:03:15 -06:00
}
2021-12-24 03:39:24 -06:00
})
}
#[macro_export]
macro_rules! error {
($($arg:tt)*) => ({
2021-12-28 02:56:29 -06:00
if $crate::log::LOG_STATE.lock().error{
2021-12-24 07:03:15 -06:00
use crate::log::Log;
crate::log::ANSISerialLogger::error(&alloc::format!($($arg)*));
}
});
2021-12-24 03:39:24 -06:00
}
#[macro_export]
macro_rules! info {
($($arg:tt)*) => ({
2021-12-28 02:56:29 -06:00
if $crate::log::LOG_STATE.lock().info{
2021-12-24 07:03:15 -06:00
use crate::log::Log;
crate::log::ANSISerialLogger::info(&alloc::format!($($arg)*));
}
2021-12-24 03:39:24 -06:00
})
}
#[macro_export]
macro_rules! trace {
($($arg:tt)*) => ({
2021-12-28 02:56:29 -06:00
if $crate::log::LOG_STATE.lock().trace{
2021-12-24 07:03:15 -06:00
use crate::log::Log;
crate::log::ANSISerialLogger::trace(&alloc::format!($($arg)*));
}
2021-12-24 03:39:24 -06:00
})
2021-11-27 09:19:08 -06:00
}