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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
extern crate chrono;
#[cfg(feature = "colours")]
extern crate console;
#[cfg(feature = "colours")]
#[doc(hidden)]
pub use console::style;
use chrono::prelude::*;
use std::fmt::Display;
const TIME_FMT: &'static str = "%F %T";
#[doc(hidden)]
pub fn _log(prefix: impl Display, msg: String, err: bool) {
use std::io::{stderr, stdout, Write};
let timestamp = Local::now().format(TIME_FMT).to_string();
#[cfg(feature = "colours")]
let timestamp = match err || cfg!(not(feature = "log2stdout")) {
true => style(timestamp).for_stderr().cyan().dim(),
false => style(timestamp).for_stdout().cyan().dim(),
};
let txt = format!("{} {}{}\n", timestamp, prefix, msg);
if err || cfg!(not(feature = "log2stdout")) {
let _ = stderr().lock().write_all(txt.as_bytes());
} else {
let _ = stdout().lock().write_all(txt.as_bytes());
}
}
#[macro_export]
macro_rules! log {
($($arg:tt)+) => (
$crate::_log("", format!($($arg)+), false);
)
}
#[macro_export]
macro_rules! info {
($($arg:tt)+) => ({
#[cfg(not(feature = "colours"))]
$crate::_log("Info: ", format!($($arg)+), false);
#[cfg(feature = "colours")]
match cfg!(feature = "log2stdout") {
true => $crate::_log($crate::style("Info: ").for_stdout().bold().green(), format!($($arg)+), false),
false => $crate::_log($crate::style("Info: ").for_stderr().bold().green(), format!($($arg)+), false),
}
})
}
#[macro_export]
macro_rules! warn {
($($arg:tt)+) => (
#[cfg(not(feature = "colours"))]
$crate::_log("Warning: ", format!($($arg)+), true);
#[cfg(feature = "colours")]
$crate::_log($crate::style("Warning: ").for_stderr().bold().yellow(), format!($($arg)+), true);
)
}
#[macro_export]
macro_rules! err {
($($arg:tt)+) => (
#[cfg(not(feature = "colours"))]
$crate::_log("Error: ", format!($($arg)+), true);
#[cfg(feature = "colours")]
$crate::_log($crate::style("Error: ").for_stderr().bold().red(), format!($($arg)+), true);
)
}