diff --git a/src/logger.rs b/src/logger.rs index bf8024c..282da4d 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -1,8 +1,8 @@ -use crate::common::Loc; - +pub static mut LEVEL: Level = Level::Info; +#[allow(dead_code)] #[repr(u8)] -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum Level { Off = 0, Error, @@ -20,26 +20,38 @@ const C_INFO: &'static str = "\x1B[1;32m"; const C_DEBUG: &'static str = "\x1B[1;35m"; const C_HELP: &'static str = "\x1B[1;36m"; -pub fn _log(level: Level, str: &str) { - match level { - Level::Off => return, - Level::Error => println!("{C_ERROR}error{C_RESET}: {str}"), - Level::Warn => println!("{C_WARN}warn{C_RESET}: {str}"), - Level::Info => println!("{C_INFO}info{C_RESET}: {str}"), - Level::Help => println!("{C_HELP}help{C_RESET}: {str}"), - Level::Debug => println!("{C_DEBUG}debug{C_RESET}: {str}"), - } +pub fn _log(dbg_loc: Option<(&'static str, u32, u32)>, level: Level, str: &str) { + if level > unsafe { LEVEL }{ + return; + } + let dbg = if let Some((file, line, col)) = dbg_loc { + format!("{file}:{line}:{col}: ") + } else {String::new()}; + match level { + Level::Off => return, + Level::Error => println!("{dbg}{C_ERROR}error{C_RESET}: {str}"), + Level::Warn => println!("{dbg}{C_WARN}warn{C_RESET}: {str}"), + Level::Info => println!("{dbg}{C_INFO}info{C_RESET}: {str}"), + Level::Help => println!("{dbg}{C_HELP}help{C_RESET}: {str}"), + Level::Debug => println!("{dbg}{C_DEBUG}debug{C_RESET}: {str}"), + } } -pub fn _log_with_loc(loc: &Loc, level: Level, str: &str) { - match level { +pub fn _log_with_loc(dbg_loc: Option<(&'static str, u32, u32)>, loc: String, level: Level, str: &str) { + if level > unsafe { LEVEL }{ + return; + } + let dbg = if let Some((file, line, col)) = dbg_loc { + format!("{file}:{line}:{col}: ") + } else {String::new()}; + match level { Level::Off => return, - Level::Error => println!("{loc}: {C_ERROR}error{C_RESET}: {str}"), - Level::Warn => println!("{loc}: {C_WARN}warn{C_RESET}: {str}"), - Level::Info => println!("{loc}: {C_INFO}info{C_RESET}: {str}"), - Level::Help => println!("{loc}: {C_HELP}help{C_RESET}: {str}"), - Level::Debug => println!("{loc}: {C_DEBUG}debug{C_RESET}: {str}"), - } + Level::Error => println!("{dbg}{loc}: {C_ERROR}error{C_RESET}: {str}"), + Level::Warn => println!("{dbg}{loc}: {C_WARN}warn{C_RESET}: {str}"), + Level::Info => println!("{dbg}{loc}: {C_INFO}info{C_RESET}: {str}"), + Level::Help => println!("{dbg}{loc}: {C_HELP}help{C_RESET}: {str}"), + Level::Debug => println!("{dbg}{loc}: {C_DEBUG}debug{C_RESET}: {str}"), + } } #[macro_use] @@ -47,31 +59,51 @@ pub mod log { #[macro_export] macro_rules! error { ($($arg:tt)*) => { - crate::logger::_log(crate::logger::Level::Error, &format!($($arg)*)) + if cfg!(debug_assertions) { + crate::logger::_log(Some((file!(), line!(), column!())), crate::logger::Level::Error, &format!($($arg)*)) + } else { + crate::logger::_log(None, crate::logger::Level::Error, &format!($($arg)*)) + } }; } #[macro_export] macro_rules! warn { ($($arg:tt)*) => { - crate::logger::_log(crate::logger::Level::Warn, &format!($($arg)*)) + if cfg!(debug_assertions) { + crate::logger::_log(Some((file!(), line!(), column!())), crate::logger::Level::Warn, &format!($($arg)*)) + } else { + crate::logger::_log(None, crate::logger::Level::Warn, &format!($($arg)*)) + } }; } #[macro_export] macro_rules! info { ($($arg:tt)*) => { - crate::logger::_log(crate::logger::Level::Info, &format!($($arg)*)) + if cfg!(debug_assertions) { + crate::logger::_log(Some((file!(), line!(), column!())), crate::logger::Level::Info, &format!($($arg)*)) + } else { + crate::logger::_log(None, crate::logger::Level::Info, &format!($($arg)*)) + } }; } #[macro_export] macro_rules! help { ($($arg:tt)*) => { - crate::logger::_log(crate::logger::Level::Help, &format!($($arg)*)) + if cfg!(debug_assertions) { + crate::logger::_log(Some((file!(), line!(), column!())), crate::logger::Level::Help, &format!($($arg)*)) + } else { + crate::logger::_log(None, crate::logger::Level::Help, &format!($($arg)*)) + } }; } #[macro_export] macro_rules! debug { ($($arg:tt)*) => { - crate::logger::_log(crate::logger::Level::Debug, &format!($($arg)*)) + if cfg!(debug_assertions) { + crate::logger::_log(Some((file!(), line!(), column!())), crate::logger::Level::Debug, &format!($($arg)*)) + } else { + crate::logger::_log(None, crate::logger::Level::Debug, &format!($($arg)*)) + } }; } @@ -79,31 +111,51 @@ pub mod log { #[macro_export] macro_rules! lerror { ($loc:expr, $($arg:tt)*) => { - crate::logger::_log_with_loc($loc, crate::logger::Level::Error, &format!($($arg)*)) + if cfg!(debug_assertions) { + crate::logger::_log_with_loc(Some((file!(), line!(), column!())), format!("{}", $loc), crate::logger::Level::Error, &format!($($arg)*)) + } else { + crate::logger::_log_with_loc(None, format!("{}", $loc), crate::logger::Level::Error, &format!($($arg)*)) + } }; } #[macro_export] macro_rules! lwarn { ($loc:expr, $($arg:tt)*) => { - crate::logger::_log_with_loc($loc, crate::logger::Level::Warn, &format!($($arg)*)) + if cfg!(debug_assertions) { + crate::logger::_log_with_loc(Some((file!(), line!(), column!())), format!("{}", $loc), crate::logger::Level::Warn, &format!($($arg)*)) + } else { + crate::logger::_log_with_loc(None, format!("{}", $loc), crate::logger::Level::Warn, &format!($($arg)*)) + } }; } #[macro_export] macro_rules! linfo { ($loc:expr, $($arg:tt)*) => { - crate::logger::_log_with_loc($loc, crate::logger::Level::Info, &format!($($arg)*)) + if cfg!(debug_assertions) { + crate::logger::_log_with_loc(Some((file!(), line!(), column!())), format!("{}", $loc), crate::logger::Level::Info, &format!($($arg)*)) + } else { + crate::logger::_log_with_loc(None, format!("{}", $loc), crate::logger::Level::Info, &format!($($arg)*)) + } }; } #[macro_export] macro_rules! lhelp { ($loc:expr, $($arg:tt)*) => { - crate::logger::_log_with_loc($loc, crate::logger::Level::Help, &format!($($arg)*)) + if cfg!(debug_assertions) { + crate::logger::_log_with_loc(Some((file!(), line!(), column!())), format!("{}", $loc), crate::logger::Level::Help, &format!($($arg)*)) + } else { + crate::logger::_log_with_loc(None, format!("{}", $loc), crate::logger::Level::Help, &format!($($arg)*)) + } }; } #[macro_export] macro_rules! ldebug { ($loc:expr, $($arg:tt)*) => { - crate::logger::_log_with_loc($loc, crate::logger::Level::Debug, &format!($($arg)*)) + if cfg!(debug_assertions) { + crate::logger::_log_with_loc(Some((file!(), line!(), column!())), format!("{}", $loc), crate::logger::Level::Debug, &format!($($arg)*)) + } else { + crate::logger::_log_with_loc(None, format!("{}", $loc), crate::logger::Level::Debug, &format!($($arg)*)) + } }; } }