Skip to main content

better_logger/
lib.rs

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