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
#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;
use std::io::Write;
use std::sync::atomic::{AtomicBool, Ordering};
use env_logger::{Builder, Color};
pub fn init() {
lazy_static! {
static ref INITIALISED: AtomicBool = AtomicBool::new(false);
}
if !INITIALISED.swap(true, Ordering::Relaxed) {
let mut builder = Builder::from_env("RUST_OPCUA_LOG");
builder.format(|buf, record| {
let now = chrono::Utc::now();
let time_fmt = now.format("%Y-%m-%d %H:%M:%S%.3f");
let mut style = buf.style();
match record.metadata().level() {
log::Level::Error => {
style.set_color(Color::White);
style.set_bg(Color::Red);
}
log::Level::Warn => {
style.set_color(Color::Yellow);
}
log::Level::Info => {
style.set_color(Color::Cyan);
}
_ => {}
}
writeln!(buf, "{} - {} - {} - {}", time_fmt, style.value(record.level()), record.target(), record.args())
});
builder.init();
info!("Logging is enabled, use RUST_OPCUA_LOG environment variable to control filtering, logging level");
}
}