1use anyhow::Result;
2use std::path::PathBuf;
3
4pub trait Logger {
6 fn log(&mut self, message: &str, args: &[&str]);
8
9 fn toggle_log_view(&mut self);
11
12 fn get_log_directory(&self) -> PathBuf;
14
15 fn get_log_file_path(&self) -> PathBuf;
17
18 fn write_log_to_file(&self, message: &str) -> Result<()>;
20}
21
22#[derive(Debug, Clone, Copy, PartialEq)]
24pub enum LogLevel {
25 Debug,
27 Info,
29 Warning,
31 Error,
33}
34
35impl LogLevel {
36 pub fn as_str(&self) -> &'static str {
38 match self {
39 LogLevel::Debug => "DEBUG",
40 LogLevel::Info => "INFO",
41 LogLevel::Warning => "WARN",
42 LogLevel::Error => "ERROR",
43 }
44 }
45
46 pub fn color_code(&self) -> &'static str {
48 match self {
49 LogLevel::Debug => "\x1b[36m", LogLevel::Info => "\x1b[32m", LogLevel::Warning => "\x1b[33m", LogLevel::Error => "\x1b[31m", }
54 }
55}
56
57pub fn format_log(level: LogLevel, message: &str) -> String {
59 let now = chrono::Local::now();
60 let timestamp = now.format("%Y-%m-%d %H:%M:%S%.3f");
61
62 format!("[{}] [{}] {}", timestamp, level.as_str(), message)
63}
64
65pub fn format_log_with_color(level: LogLevel, message: &str) -> String {
67 let now = chrono::Local::now();
68 let timestamp = now.format("%Y-%m-%d %H:%M:%S%.3f");
69 let reset = "\x1b[0m";
70
71 format!(
72 "[{}] [{}{}{}] {}",
73 timestamp,
74 level.color_code(),
75 level.as_str(),
76 reset,
77 message
78 )
79}