pub const INFO: usize = 0; pub const DEBUG: usize = 1; pub const WARNING: usize = 2; pub const ERROR: usize = 3; pub const FATAL: usize = 4; pub const LOG_LEVEL: [&str; 5] = [ "INFO", "DEBUG", "WARNING", "ERROR", "FATAL", ]; use chrono::Utc; pub fn colored(text: &str, color: &str) -> String { // For terminals: use ANSI escape codes let ansi_code = match color { "black" => "30", "red" => "31", "green" => "32", "yellow" => "33", "blue" => "34", "purple" => "35", "cyan" => "36", "white" => "37", _ => "0", // default no color }; format!("\x1b[{ansi_code}m{text}\x1b[0m") } pub fn current_timestamp() -> String { Utc::now().format("%Y-%m-%d@%H:%M:%S").to_string() } /// You can use this macro to create error messages that will show in the console /// if run on the client or in the terminal if run on the server /// /// log_levels are : /// - INFO /// - DEBUG /// - WARNING /// - ERROR /// - FATAL /// /// important: FATAL will cause the thread to panic and stop execution #[macro_export] macro_rules! log { ($level:expr, $($arg:tt)*) => {{ let formatted_msg = format!($($arg)*); match $level { $crate::log::INFO => { println!( "[{}] {} | {}", $crate::log::colored($crate::log::LOG_LEVEL[$level], "green"), $crate::log::current_timestamp(), formatted_msg ); }, $crate::log::DEBUG => { println!( "[{}] [{}:{}] {} | {}", $crate::log::LOG_LEVEL[$level], file!(), line!(), $crate::log::current_timestamp(), formatted_msg ); }, $crate::log::WARNING => { println!( "[{}] [{}:{}] {} | {}", $crate::log::colored($crate::log::LOG_LEVEL[$level], "yellow"), file!(), line!(), $crate::log::current_timestamp(), formatted_msg ); }, $crate::log::ERROR => { println!( "[{}] [{}:{}] {} | {}", $crate::log::colored($crate::log::LOG_LEVEL[$level], "red"), file!(), line!(), $crate::log::current_timestamp(), formatted_msg ); }, $crate::log::FATAL => { println!( "[{}] [{}:{}] {} | {}", $crate::log::colored($crate::log::LOG_LEVEL[$level], "red"), file!(), line!(), $crate::log::current_timestamp(), formatted_msg ); if $level == $crate::log::FATAL { panic!("Fatal error encountered: {}", formatted_msg); } }, _ => { println!( "[{}] {} {}", $crate::log::LOG_LEVEL[$crate::log::ERROR], "loggin error: log_level value invalid:", $level ); } } }}; }