better_logger/
lib.rs

1// better-logger/src/lib.rs
2
3#[cfg(any(feature = "native", feature = "wasm"))]
4pub mod interface;
5
6#[cfg(feature = "native")]
7pub(crate) mod native;
8#[cfg(feature = "wasm")]
9pub(crate) mod wasm;
10
11#[cfg(any(feature = "native", feature = "wasm"))]
12pub use interface::settings::LoggerSettings as LoggerSettings;
13#[cfg(any(feature = "native", feature = "wasm"))]
14pub use interface::settings::NetworkFormat as NetworkFormat;
15#[cfg(any(feature = "native", feature = "wasm"))]
16pub use interface::logger as logger;
17#[cfg(any(feature = "native", feature = "wasm"))]
18pub use interface::logger::init as init;
19
20///0
21///1
22///2
23///3
24///4
25///5
26///6
27///7
28///8
29///9
30
31#[cfg(any(feature = "native", feature = "wasm"))]
32use crate::interface::settings::RUNNING_SETTINGS;
33
34///0
35///1
36///2
37///3
38///4
39///5
40///6
41///7
42///8
43///9
44
45#[cfg(any(feature = "native", feature = "wasm"))]
46#[doc(hidden)]
47pub fn debug_extra_enabled() -> bool {
48    let running_settings = RUNNING_SETTINGS.get().expect("better-logger: macro called before logger::init()");
49    if running_settings.debug_extra == true {
50        return true;
51    }
52    else {
53        return false;
54    }
55}
56
57#[cfg(any(feature = "native", feature = "wasm"))]
58#[doc(hidden)]
59pub fn is_async() -> bool {
60    let running_settings = RUNNING_SETTINGS.get().expect("better-logger: macro called before logger::init()");
61    if running_settings.async_logging == true {
62        return true;
63    }
64    else {
65        return false
66    }
67}
68
69#[cfg(any(feature = "native", feature = "wasm"))]
70#[doc(hidden)]
71pub fn log_async(level: &str, target: &str, msg: &str) {
72    #[cfg(feature = "native")]
73    crate::native::log::native_log_async(level, target, msg);
74
75    #[cfg(feature = "wasm")]
76    crate::wasm::log::wasm_log_async(level, target, msg);
77}
78
79#[cfg(any(feature = "native", feature = "wasm"))]
80#[doc(hidden)]
81pub fn log_sync(level: &str, target: &str, msg: &str) {
82    #[cfg(feature = "native")]
83    crate::native::log::native_log_sync(level, target, msg);
84
85    #[cfg(feature = "wasm")]
86    crate::wasm::log::wasm_log_sync(level, target, msg);
87}
88
89///0
90///1
91///2
92///3
93///4
94///5
95///6
96///7
97///8
98///9
99
100#[cfg(any(feature = "native", feature = "wasm"))]
101#[macro_export]
102macro_rules! trace {
103    ($($arg:tt)*) => {
104        {   
105            let target = module_path!();
106            let message = format!($($arg)*);
107            if $crate::is_async() == true {
108                $crate::log_async("trace", target, &message);
109            } 
110            else {
111                $crate::log_sync("trace", target, &message);
112            }
113        }
114    };
115}
116
117#[cfg(any(feature = "native", feature = "wasm"))]
118#[macro_export]
119macro_rules! debug {
120    ($($arg:tt)*) => {
121        {   
122            let target = module_path!();
123            let message = format!($($arg)*);
124            if $crate::is_async() == true {
125                $crate::log_async("debug", target, &message);
126            } 
127            else {
128                $crate::log_sync("debug", target, &message);
129            }
130        }
131    };
132}
133
134#[cfg(any(feature = "native", feature = "wasm"))]
135#[macro_export]
136macro_rules! debugx {
137    ($($arg:tt)*) => {
138        {   
139            if $crate::debug_extra_enabled() == true {
140                let target = module_path!();
141                let message = format!($($arg)*);
142                if $crate::is_async() == true {
143                    $crate::log_async("debug", target, &message);
144                } else {
145                    $crate::log_sync("debug", target, &message);
146                }
147            }
148        }
149    };
150}
151
152#[cfg(any(feature = "native", feature = "wasm"))]
153#[macro_export]
154macro_rules! info {
155    ($($arg:tt)*) => {
156        {
157            let target = module_path!();
158            let message = format!($($arg)*);
159            if $crate::is_async() == true {
160                $crate::log_async("info", target, &message);
161            } 
162            else {
163                $crate::log_sync("info", target, &message);
164            }
165        }
166    };
167}
168
169#[cfg(any(feature = "native", feature = "wasm"))]
170#[macro_export]
171macro_rules! warn {
172    ($($arg:tt)*) => {
173        {
174            let target = module_path!();
175            let message = format!($($arg)*);
176            if $crate::is_async() == true {
177                $crate::log_async("warn", target, &message);
178            } 
179            else {
180                $crate::log_sync("warn", target, &message);
181            }
182        }
183    };
184}
185
186#[cfg(any(feature = "native", feature = "wasm"))]
187#[macro_export]
188macro_rules! error {
189    ($($arg:tt)*) => {
190        {
191            let target = module_path!();
192            let message = format!($($arg)*);
193            if $crate::is_async() == true {
194                $crate::log_async("error", target, &message);
195            } 
196            else {
197                $crate::log_sync("error", target, &message);
198            }
199        }
200    };
201}