1use serde_json::to_string_pretty;
2use std::fs;
3use std::path::Path;
4
5#[macro_export]
6macro_rules! info {
7 ($($arg:tt)*) => {
8 println!("\x1b[32m[INFO] [{}]\x1b[0m {}", chrono::Utc::now().format("%H:%M:%S"), format!($($arg)*))
9 };
10}
11
12#[macro_export]
13macro_rules! log {
14 ($($arg:tt)*) => {
15 println!("\x1b[33m[LOG] [{}]\x1b[0m {}", chrono::Utc::now().format("%H:%M:%S"), format!($($arg)*))
16 };
17}
18
19#[macro_export]
20macro_rules! warn {
21 ($($arg:tt)*) => {
22 println!("\x1b[35m[WARN] [{}]\x1b[0m {}", chrono::Utc::now().format("%H:%M:%S"), format!($($arg)*))
23 };
24}
25
26#[macro_export]
27macro_rules! error {
28 ($($arg:tt)*) => {
29 println!("\x1b[31m[ERROR][{}]\x1b[0m {}", chrono::Utc::now().format("%H:%M:%S"), format!($($arg)*))
30 };
31}
32
33#[macro_export]
34macro_rules! fatal {
35 ($($arg:tt)*) => {
36 panic!("\x1b[1;31m[FATAL][{}]\x1b[0m {}", chrono::Utc::now().format("%H:%M:%S"), format!($($arg)*))
37 };
38}
39
40#[macro_export]
41macro_rules! obj {
42 ($($arg:tt)*) => {
43 println!("\x1b[1;34m[OBJ] [{}]\x1b[0m {}", chrono::Utc::now().format("%H:%M:%S"), format!($($arg)*))
44 };
45}
46
47#[macro_export]
48macro_rules! event {
49 ($($arg:tt)*) => {
50 if std::env::var("LOG_MELVIN_EVENTS").is_ok_and(|s| s == "1") {
51 println!("\x1b[36m[EVENT][{}]\x1b[0m {}", chrono::Utc::now().format("%H:%M:%S"), format!($($arg)*))
52 }
53 };
54}
55
56#[macro_export]
57macro_rules! log_burn {
58 ($($arg:tt)*) => {
59 println!("\x1b[36m[BURN] [{}]\x1b[0m {}", chrono::Utc::now().format("%H:%M:%S"), format!($($arg)*))
60 };
61}
62
63pub trait JsonDump: serde::Serialize {
64 fn file_name(&self) -> String;
65 fn dir_name(&self) -> &'static str;
66 fn dump_json(&self) {
67 let path_str = format!("./dumps/{}/{}.json", self.dir_name(), self.file_name());
68 let path = Path::new(&path_str);
69
70 if let Ok(json_data) = to_string_pretty(&self) {
71 if let Some(parent) = Path::new(&path).parent() {
72 fs::create_dir_all(parent)
73 .is_err()
74 .then(|| warn!("Failed creating directory for JSON file: {parent:?}."));
75 }
76 fs::write(path, json_data)
77 .is_err()
78 .then(|| warn!("Failed writing JSON to file {path:?}."));
79 };
80 }
81}