1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use atomic::Atomic;
use lazy_static::lazy_static;
lazy_static! {
pub static ref LOGGER_LEVEL: Atomic<LoggerLevel> = Atomic::<LoggerLevel>::new(LoggerLevel::Error);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum LoggerLevel {
Failure,
Error,
Info,
Debug,
}
#[macro_export]
macro_rules! log {
($logger_level: ident, $is_err: expr, $msg_prefix: expr, $msg: expr$(, $args: expr)*) => {{
if $crate::logger::LOGGER_LEVEL.load(atomic::Ordering::SeqCst) >= $crate::logger::LoggerLevel::$logger_level {
if $is_err {
eprintln!(concat!($msg_prefix, $msg)$(, $args)*);
} else {
println!(concat!($msg_prefix, $msg)$(, $args)*);
}
}
}}
}
#[macro_export]
macro_rules! debug { ($msg: expr$(, $args: expr)*) => { $crate::log!(Debug, false, "[DEBUG] ", $msg$(, $args)*); } }
#[macro_export]
macro_rules! info { ($msg: expr$(, $args: expr)*) => { $crate::log!(Info, false, "[INFO] ", $msg$(, $args)*); } }
#[macro_export]
macro_rules! err { ($msg: expr$(, $args: expr)*) => { $crate::log!(Error, true, "[ERROR] ", $msg$(, $args)*); } }
#[macro_export]
macro_rules! fail { (exit $code: expr, $msg: expr$(, $args: expr)*) => {{
$crate::log!(Failure, true, "[FAIL] ", $msg$(, $args)*);
std::process::exit($code); }}
}