plugin_interfaces/logging/
logging.rs1#[repr(u8)]
2#[allow(dead_code)]
3pub enum ColorCode {
4 Black = 30,
5 Red = 31,
6 Green = 32,
7 Yellow = 33,
8 Blue = 34,
9 Magenta = 35,
10 Cyan = 36,
11 White = 37,
12 BrightBlack = 90,
13 BrightRed = 91,
14 BrightGreen = 92,
15 BrightYellow = 93,
16 BrightBlue = 94,
17 BrightMagenta = 95,
18 BrightCyan = 96,
19 BrightWhite = 97,
20}
21
22#[allow(dead_code)]
23pub enum LogLevel {
24 Error,
25 Warn,
26 Info,
27 Debug,
28 Trace,
29}
30
31impl std::fmt::Display for LogLevel {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 match self {
34 LogLevel::Error => write!(f, "ERROR"),
35 LogLevel::Warn => write!(f, "WARN"),
36 LogLevel::Info => write!(f, "INFO"),
37 LogLevel::Debug => write!(f, "DEBUG"),
38 LogLevel::Trace => write!(f, "TRACE"),
39 }
40 }
41}
42
43#[macro_export]
44macro_rules! with_color {
45 ($color_code:expr, $($arg:tt)*) => {{
46 format_args!("\u{1B}[{}m{}\u{1B}[m", $color_code as u8, format_args!($($arg)*))
47 }};
48}
49
50#[macro_export]
51macro_rules! host_log_print {
52 ($level:expr, $msg:expr) => {
53 {
54 let level_color = match $level {
55 $crate::LogLevel::Error => $crate::ColorCode::BrightRed,
56 $crate::LogLevel::Warn => $crate::ColorCode::BrightYellow,
57 $crate::LogLevel::Info => $crate::ColorCode::BrightGreen,
58 $crate::LogLevel::Debug => $crate::ColorCode::BrightCyan,
59 $crate::LogLevel::Trace => $crate::ColorCode::BrightBlack,
60 };
61 let args_color = match $level {
62 $crate::LogLevel::Error => $crate::ColorCode::Red,
63 $crate::LogLevel::Warn => $crate::ColorCode::Yellow,
64 $crate::LogLevel::Info => $crate::ColorCode::Green,
65 $crate::LogLevel::Debug => $crate::ColorCode::Cyan,
66 $crate::LogLevel::Trace => $crate::ColorCode::BrightBlack,
67 };
68 println!(
69 "[{} {} {}",
70 $crate::with_color!(level_color, "{:<5}", $level),
71 $crate::with_color!($crate::ColorCode::White, "{}:{}]", file!(), line!()),
72 $crate::with_color!(args_color, "{}", $msg)
73 );
74 }
75 };
76
77 ($level:expr, $fmt:expr, $($args:expr),*) => {
78 {
79 let level_color = match $level {
80 $crate::LogLevel::Error => $crate::ColorCode::BrightRed,
81 $crate::LogLevel::Warn => $crate::ColorCode::BrightYellow,
82 $crate::LogLevel::Info => $crate::ColorCode::BrightGreen,
83 $crate::LogLevel::Debug => $crate::ColorCode::BrightCyan,
84 $crate::LogLevel::Trace => $crate::ColorCode::BrightBlack,
85 };
86 let args_color = match $level {
87 $crate::LogLevel::Error => $crate::ColorCode::Red,
88 $crate::LogLevel::Warn => $crate::ColorCode::Yellow,
89 $crate::LogLevel::Info => $crate::ColorCode::Green,
90 $crate::LogLevel::Debug => $crate::ColorCode::Cyan,
91 $crate::LogLevel::Trace => $crate::ColorCode::BrightBlack,
92 };
93 let msg = format!($fmt, $($args),*);
94 println!(
95 "[{} {} {}",
96 $crate::with_color!(level_color, "{:<5}", $level),
97 $crate::with_color!($crate::ColorCode::White, "{}:{}]", file!(), line!()),
98 $crate::with_color!(args_color, "{}", msg)
99 );
100 }
101 };
102}
103
104#[macro_export]
105macro_rules! log_error {
106 ($fmt:expr) => {
107 $crate::host_log_print!($crate::LogLevel::Error, $fmt)
108 };
109 ($fmt:expr, $($args:expr),*) => {
110 $crate::host_log_print!($crate::LogLevel::Error, $fmt, $($args),*)
111 };
112}
113
114#[macro_export]
115macro_rules! log_warn {
116 ($fmt:expr) => {
117 $crate::host_log_print!($crate::LogLevel::Warn, $fmt)
118 };
119 ($fmt:expr, $($args:expr),*) => {
120 $crate::host_log_print!($crate::LogLevel::Warn, $fmt, $($args),*)
121 };
122}
123
124#[macro_export]
125macro_rules! log_info {
126 ($fmt:expr) => {
127 $crate::host_log_print!($crate::LogLevel::Info, $fmt)
128 };
129 ($fmt:expr, $($args:expr),*) => {
130 $crate::host_log_print!($crate::LogLevel::Info, $fmt, $($args),*)
131 };
132}
133
134#[macro_export]
135macro_rules! log_debug {
136 ($fmt:expr) => {
137 $crate::host_log_print!($crate::LogLevel::Debug, $fmt)
138 };
139 ($fmt:expr, $($args:expr),*) => {
140 $crate::host_log_print!($crate::LogLevel::Debug, $fmt, $($args),*)
141 };
142}
143
144#[macro_export]
145macro_rules! log_trace {
146 ($fmt:expr) => {
147 $crate::host_log_print!($crate::LogLevel::Trace, $fmt)
148 };
149 ($fmt:expr, $($args:expr),*) => {
150 $crate::host_log_print!($crate::LogLevel::Trace, $fmt, $($args),*)
151 };
152}