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