aerosocket_server/
logging.rs1#[macro_export]
7macro_rules! log_error {
8 ($($arg:tt)*) => {
9 #[cfg(feature = "logging")]
10 {
11 tracing::error!($($arg)*);
12 }
13 #[cfg(not(feature = "logging"))]
14 {
15 eprintln!("[ERROR] {}", format!($($arg)*));
16 }
17 };
18}
19
20#[macro_export]
22macro_rules! log_warn {
23 ($($arg:tt)*) => {
24 #[cfg(feature = "logging")]
25 {
26 tracing::warn!($($arg)*);
27 }
28 #[cfg(not(feature = "logging"))]
29 {
30 eprintln!("[WARN] {}", format!($($arg)*));
31 }
32 };
33}
34
35#[macro_export]
37macro_rules! log_info {
38 ($($arg:tt)*) => {
39 #[cfg(feature = "logging")]
40 {
41 tracing::info!($($arg)*);
42 }
43 #[cfg(not(feature = "logging"))]
44 {
45 eprintln!("[INFO] {}", format!($($arg)*));
46 }
47 };
48}
49
50#[macro_export]
52macro_rules! log_debug {
53 ($($arg:tt)*) => {
54 #[cfg(feature = "logging")]
55 {
56 tracing::debug!($($arg)*);
57 }
58 #[cfg(not(feature = "logging"))]
59 {
60 eprintln!("[DEBUG] {}", format!($($arg)*));
61 }
62 };
63}
64
65#[macro_export]
67macro_rules! log_trace {
68 ($($arg:tt)*) => {
69 #[cfg(feature = "logging")]
70 {
71 tracing::trace!($($arg)*);
72 }
73 #[cfg(not(feature = "logging"))]
74 {
75 eprintln!("[TRACE] {}", format!($($arg)*));
76 }
77 };
78}
79
80#[cfg(feature = "logging")]
82pub fn init_logging() -> Result<(), Box<dyn std::error::Error>> {
83 use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
84
85 tracing_subscriber::registry()
86 .with(tracing_subscriber::EnvFilter::new(
87 std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string()),
88 ))
89 .with(tracing_subscriber::fmt::layer())
90 .init();
91
92 Ok(())
93}
94
95#[cfg(not(feature = "logging"))]
97pub fn init_logging() -> Result<(), Box<dyn std::error::Error>> {
98 Ok(())
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn test_logging_macros() {
107 log_info!("Test info message");
108 log_warn!("Test warning message");
109 log_error!("Test error message");
110 log_debug!("Test debug message");
111 }
112
113 #[test]
114 fn test_init_logging() {
115 assert!(init_logging().is_ok());
116 }
117}