use anyhow::Result;
use std::path::PathBuf;
pub trait Logger {
fn log(&mut self, message: &str, args: &[&str]);
fn toggle_log_view(&mut self);
fn get_log_directory(&self) -> PathBuf;
fn get_log_file_path(&self) -> PathBuf;
fn write_log_to_file(&self, message: &str) -> Result<()>;
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LogLevel {
Debug,
Info,
Warning,
Error,
}
impl LogLevel {
pub fn as_str(&self) -> &'static str {
match self {
LogLevel::Debug => "DEBUG",
LogLevel::Info => "INFO",
LogLevel::Warning => "WARN",
LogLevel::Error => "ERROR",
}
}
pub fn color_code(&self) -> &'static str {
match self {
LogLevel::Debug => "\x1b[36m", LogLevel::Info => "\x1b[32m", LogLevel::Warning => "\x1b[33m", LogLevel::Error => "\x1b[31m", }
}
}
pub fn format_log(level: LogLevel, message: &str) -> String {
let now = chrono::Local::now();
let timestamp = now.format("%Y-%m-%d %H:%M:%S%.3f");
format!("[{}] [{}] {}", timestamp, level.as_str(), message)
}
pub fn format_log_with_color(level: LogLevel, message: &str) -> String {
let now = chrono::Local::now();
let timestamp = now.format("%Y-%m-%d %H:%M:%S%.3f");
let reset = "\x1b[0m";
format!(
"[{}] [{}{}{}] {}",
timestamp,
level.color_code(),
level.as_str(),
reset,
message
)
}