pub trait Log { fn debug(val: &str); fn error(val: &str); fn info(val: &str); fn trace(val: &str); } pub struct LogState { pub log_to_serial: bool, pub log_to_screen: bool, pub debug: bool, pub error: bool, pub info: bool, pub trace: bool, } lazy_static::lazy_static! { pub static ref LOG_STATE: Mutex = spin::Mutex::new(LogState { log_to_screen: true, log_to_serial: true, debug: true, error: true, info: true, trace: true, }); } use crate::serial_println; use lliw::{Fg, Reset}; use spin::Mutex; pub struct ANSISerialLogger; impl Log for ANSISerialLogger { fn debug(val: &str) { if LOG_STATE.lock().log_to_serial { serial_println!( "[{}Debug{}][{}FakeTempTime{}] {}", Fg::Blue, Reset, Fg::Green, Reset, val ); } if LOG_STATE.lock().log_to_screen { println!("[$BLUE$Debug$RESET$][$GREEN$FakeTempTime$RESET$] {}", val); } } fn error(val: &str) { if LOG_STATE.lock().log_to_serial { serial_println!( "[{}Error{}][{}FakeTempTime{}] {}", Fg::Red, Reset, Fg::Green, Reset, val ); } if LOG_STATE.lock().log_to_screen { println!("[$RED$Error$RESET$][$GREEN$FakeTempTime$RESET$] {}", val); } } fn info(val: &str) { if LOG_STATE.lock().log_to_serial { serial_println!( "[{}Info{} ][{}FakeTempTime{}] {}", Fg::LightWhite, Reset, Fg::Green, Reset, val ); } if LOG_STATE.lock().log_to_screen { println!( "[$LIGHTGRAY$Info$RESET$ ][$GREEN$FakeTempTime$RESET$] {}", val ); } } fn trace(val: &str) { if LOG_STATE.lock().log_to_serial { serial_println!( "[{}Trace{}][{}FakeTempTime{}] {}", Fg::Yellow, Reset, Fg::Green, Reset, val ); } if LOG_STATE.lock().log_to_screen { println!("[$YELLOW$Trace$RESET$][$GREEN$FakeTempTime$RESET$] {}", val); } } } #[macro_export] macro_rules! debug { ($($arg:tt)*) => ({ if $crate::log::LOG_STATE.lock().debug{ use crate::log::Log; let _ = &log::ANSISerialLogger::debug(&alloc::format!($($arg)*)); } }) } #[macro_export] macro_rules! error { ($($arg:tt)*) => ({ if $crate::log::LOG_STATE.lock().error{ use crate::log::Log; crate::log::ANSISerialLogger::error(&alloc::format!($($arg)*)); } }); } #[macro_export] macro_rules! info { ($($arg:tt)*) => ({ if $crate::log::LOG_STATE.lock().info{ use crate::log::Log; crate::log::ANSISerialLogger::info(&alloc::format!($($arg)*)); } }) } #[macro_export] macro_rules! trace { ($($arg:tt)*) => ({ if $crate::log::LOG_STATE.lock().trace{ use crate::log::Log; crate::log::ANSISerialLogger::trace(&alloc::format!($($arg)*)); } }) }