sustenet_shared/
logging.rs

1pub enum LogLevel {
2    Debug,
3    Info,
4    Warning,
5    Error,
6    Success,
7}
8
9pub enum LogType {
10    Master,
11    Cluster,
12    Client,
13    System,
14}
15
16#[macro_export]
17macro_rules! log_message {
18    (
19        $level:expr,
20        $type:expr,
21        $($arg:tt)*
22    ) => {
23        {
24            use $crate::logging::LogLevel;
25            use $crate::logging::LogType;
26            use $crate::utils::constants::{
27                TERMINAL_BLUE,
28                TERMINAL_GREEN,
29                TERMINAL_ORANGE,
30                TERMINAL_RED,
31                TERMINAL_DEFAULT,
32            };
33
34            let level_str = match $level {
35                LogLevel::Debug => format!("{}[DEBUG]", TERMINAL_BLUE),
36                LogLevel::Info => format!("{}[INFO]", TERMINAL_DEFAULT),
37                LogLevel::Warning => format!("{}[WARNING]", TERMINAL_ORANGE),
38                LogLevel::Error => format!("{}[ERROR]", TERMINAL_RED),
39                LogLevel::Success => format!("{}[SUCCESS]", TERMINAL_GREEN),
40            };
41
42            let type_str = match $type {
43                LogType::Master => "[Master]",
44                LogType::Cluster => "[Cluster]",
45                LogType::Client => "[Client]",
46                LogType::System => "[System]",
47            };
48
49            println!("{}{} {}{}", level_str, type_str, format!($($arg)*), TERMINAL_DEFAULT);
50        }
51    };
52}
53
54use crate::{ log_message, utils::constants::DEBUGGING };
55
56pub struct Logger {
57    plugin_info: std::sync::OnceLock<Box<dyn Fn(&str) + Send + Sync + 'static>>,
58    log_type: LogType,
59}
60impl Logger {
61    pub fn new(log_type: LogType) -> Self {
62        Logger {
63            plugin_info: std::sync::OnceLock::new(),
64            log_type,
65        }
66    }
67
68    pub fn set_plugin<F>(&self, plugin: F) where F: Fn(&str) + Send + Sync + 'static {
69        let _ = self.plugin_info.set(Box::new(plugin));
70    }
71
72    pub fn debug(&self, message: &str) {
73        if !DEBUGGING {
74            return;
75        }
76        if let Some(plugin_info) = self.plugin_info.get() {
77            plugin_info(message);
78        }
79        log_message!(LogLevel::Debug, self.log_type, "{}", message);
80    }
81
82    pub fn info(&self, message: &str) {
83        if !DEBUGGING {
84            return;
85        }
86        if let Some(plugin_info) = self.plugin_info.get() {
87            plugin_info(message);
88        }
89        log_message!(LogLevel::Info, self.log_type, "{}", message);
90    }
91
92    pub fn warning(&self, message: &str) {
93        if !DEBUGGING {
94            return;
95        }
96        if let Some(plugin_info) = self.plugin_info.get() {
97            plugin_info(message);
98        }
99        log_message!(LogLevel::Warning, self.log_type, "{}", message);
100    }
101
102    pub fn error(&self, message: &str) {
103        if !DEBUGGING {
104            return;
105        }
106        if let Some(plugin_info) = self.plugin_info.get() {
107            plugin_info(message);
108        }
109        log_message!(LogLevel::Error, self.log_type, "{}", message);
110    }
111
112    pub fn success(&self, message: &str) {
113        if !DEBUGGING {
114            return;
115        }
116        if let Some(plugin_info) = self.plugin_info.get() {
117            plugin_info(message);
118        }
119        log_message!(LogLevel::Success, self.log_type, "{}", message);
120    }
121}