1pub mod foreground_colors;
2pub mod background_colors;
3pub mod font_mode;
4pub mod levels;
5
6use chrono::Utc;
7use crate::foreground_colors::ForegroundColors;
8use crate::background_colors::BackgroundColors;
9use crate::levels::Levels;
10
11pub struct Apollo {
12 pub logging_level: Levels
13}
14
15impl Default for Apollo {
16 fn default() -> Self {
17 Self::new()
18 }
19}
20
21impl Apollo {
22
23 pub fn new() -> Apollo {
46 Apollo { logging_level: Levels::DEBUG }
47 }
48
49 pub fn debug(&self, s: &str) -> Option<String> {
65
66 if self.logging_level.as_u8() > Levels::DEBUG.as_u8() {
68 return None;
69 }
70
71 let current_time = Utc::now().format("%D %H:%M:%S").to_string();
73
74 let color_date = ForegroundColors::bright_green();
76 let color_label = ForegroundColors::cyan();
77 let color_text = ForegroundColors::bright_white();
78
79 let message = format!("{color_date}[{current_time}]\x1B[0m {color_label}[ DEBUG ]\x1B[0m | {color_text}{s}\x1B[0m");
81 println!("{message}");
82
83 Some(message)
84 }
85
86 pub fn info(&self, s: &str) -> Option<String> {
102
103 if self.logging_level.as_u8() > Levels::INFO.as_u8() {
105 return None;
106 }
107
108 let current_time = Utc::now().format("%D %H:%M:%S").to_string();
110
111 let color_date = ForegroundColors::bright_green();
113 let color_label = ForegroundColors::blue();
114 let color_text = ForegroundColors::bright_white();
115
116 let message = format!("{color_date}[{current_time}]\x1B[0m {color_label}[ INFO ]\x1B[0m | {color_text}{s}\x1B[0m");
118 println!("{message}");
119
120 Some(message)
121 }
122
123 pub fn warn(&self, s: &str) -> Option<String> {
139
140 if self.logging_level.as_u8() > Levels::WARN.as_u8() {
142 return None;
143 }
144
145 let current_time = Utc::now().format("%D %H:%M:%S").to_string();
147
148 let color_date = ForegroundColors::bright_green();
150 let color_label = ForegroundColors::yellow();
151 let color_text = ForegroundColors::bright_white();
152
153 let message = format!("{color_date}[{current_time}]\x1B[0m {color_label}[ WARN ]\x1B[0m | {color_text}{s}\x1B[0m");
155 println!("{message}");
156
157 Some(message)
158 }
159
160 pub fn error(&self, s: &str) -> Option<String> {
176
177 if self.logging_level.as_u8() > Levels::ERROR.as_u8() {
179 return None;
180 }
181
182 let current_time = Utc::now().format("%D %H:%M:%S").to_string();
184
185 let color_date = ForegroundColors::bright_green();
187 let color_label = ForegroundColors::red();
188 let color_text = ForegroundColors::bright_white();
189
190 let message = format!("{color_date}[{current_time}]\x1B[0m {color_label}[ ERROR ]\x1B[0m | {color_text}{s}\x1B[0m");
192 eprintln!("{message}");
193
194 Some(message)
195 }
196
197 pub fn critical(&self, s: &str) -> Option<String> {
213
214 if self.logging_level.as_u8() > Levels::CRITICAL.as_u8() {
216 return None;
217 }
218
219 let current_time = Utc::now().format("%D %H:%M:%S").to_string();
221
222 let color_date = ForegroundColors::bright_green();
224 let color_label = ForegroundColors::bright_red();
225 let color_text = ForegroundColors::bright_white() + BackgroundColors::bright_red();
226
227 let message = format!("{color_date}[{current_time}]\x1B[0m {color_label}[ CRIT ]\x1B[0m | {color_text}{s}\x1B[0m");
229 eprintln!("{message}");
230
231 Some(message)
232 }
233}
234
235#[cfg(test)]
236mod tests {
237 use super::*;
239
240 #[test]
242 fn test_debug() {
243 let logger = Apollo::new();
244 assert!(logger.debug("This is a test debug message").is_some());
245 }
246
247 #[test]
249 fn test_debug_under_level() {
250 let logger = Apollo { logging_level: Levels::INFO };
251 assert!(logger.debug("This is a test debug message").is_none());
252 }
253
254 #[test]
256 fn test_info() {
257 let logger = Apollo::new();
258 assert!(logger.info("This is a test info message").is_some());
259 }
260
261 #[test]
263 fn test_info_under_level() {
264 let logger = Apollo { logging_level: Levels::WARN };
265 assert!(logger.info("This is a test info message").is_none());
266 }
267
268 #[test]
270 fn test_warn() {
271 let logger = Apollo::new();
272 assert!(logger.warn("This is a test warning message").is_some());
273 }
274
275 #[test]
277 fn test_warn_under_level() {
278 let logger = Apollo { logging_level: Levels::ERROR };
279 assert!(logger.warn("This is a test warning message").is_none());
280 }
281
282 #[test]
284 fn test_error() {
285 let logger = Apollo::new();
286 assert!(logger.error("This is a test error message").is_some());
287 }
288
289 #[test]
291 fn test_error_under_level() {
292 let logger = Apollo { logging_level: Levels::CRITICAL };
293 assert!(logger.error("This is a test error message").is_none());
294 }
295
296 #[test]
298 fn test_critical() {
299 let logger = Apollo::new();
300 assert!(logger.critical("This is a test critical message").is_some());
301 }
302
303 #[test]
305 fn test_critical_under_level() {
306 let logger = Apollo { logging_level: Levels::NONE };
307 assert!(logger.critical("This is a test critical message").is_none());
308 }
309
310 #[test]
312 fn test_logging_level_none() {
313 let logger = Apollo { logging_level: Levels::NONE };
314 assert!(logger.debug("This is a test debug message").is_none());
315 assert!(logger.info("This is a test info message").is_none());
316 assert!(logger.warn("This is a test warning message").is_none());
317 assert!(logger.error("This is a test error message").is_none());
318 assert!(logger.critical("This is a test critical message").is_none());
319 }
320}