File size: 1,991 Bytes
94c29e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | use chrono::Local;
use colored::*;
use std::sync::Mutex;
/// Logger with colored output similar to the Python version
pub struct Logger {
lock: Mutex<()>,
}
impl Logger {
pub fn new() -> Self {
Self {
lock: Mutex::new(()),
}
}
fn get_timestamp() -> String {
Local::now().format("%H:%M:%S").to_string()
}
fn log(&self, _level: &str, prefix: &str, message: &str, color: Color) {
let _guard = self.lock.lock().unwrap();
let timestamp = Self::get_timestamp();
let timestamp_formatted = format!("[{}]", timestamp.magenta());
let prefix_colored = prefix.color(color).bold();
println!(
"{} {} {}",
timestamp_formatted.bright_black(),
prefix_colored,
message
);
}
pub fn success(&self, message: &str) {
self.log("SUCCESS", "[+]", message, Color::Green);
}
pub fn error(&self, message: &str) {
self.log("ERROR", "[!]", message, Color::Red);
}
pub fn info(&self, message: &str) {
self.log("INFO", "[*]", message, Color::White);
}
pub fn warning(&self, message: &str) {
self.log("WARNING", "[!]", message, Color::Yellow);
}
}
impl Default for Logger {
fn default() -> Self {
Self::new()
}
}
lazy_static::lazy_static! {
pub static ref LOG: Logger = Logger::new();
}
/// Convenience macros for logging
#[macro_export]
macro_rules! log_success {
($($arg:tt)*) => {
$crate::utils::logger::LOG.success(&format!($($arg)*))
};
}
#[macro_export]
macro_rules! log_error {
($($arg:tt)*) => {
$crate::utils::logger::LOG.error(&format!($($arg)*))
};
}
#[macro_export]
macro_rules! log_info {
($($arg:tt)*) => {
$crate::utils::logger::LOG.info(&format!($($arg)*))
};
}
#[macro_export]
macro_rules! log_warning {
($($arg:tt)*) => {
$crate::utils::logger::LOG.warning(&format!($($arg)*))
};
}
|