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
extern crate log;
#[cfg(feature="timestamp")]
extern crate chrono;
use std::fmt;
pub struct Logger;
impl Logger {
#[cfg(feature="timestamp")]
fn get_date() -> impl fmt::Display {
chrono::offset::Local::now().format("%F %H:%M:%S%.3f %z")
}
pub fn print(record: &log::Record) {
#[cfg(feature="timestamp")]
{
println!("{:<5} [{}] {}:{} - {}",
record.level(),
Self::get_date(),
record.file().unwrap_or("UNKNOWN"), record.line().unwrap_or(0),
record.args());
}
#[cfg(not(feature="timestamp"))]
{
println!("{:<5} {}:{} - {}",
record.level(),
record.file().unwrap_or("UNKNOWN"), record.line().unwrap_or(0),
record.args());
}
}
}
impl log::Log for Logger {
fn enabled(&self, _: &log::Metadata) -> bool {
true
}
fn log(&self, record: &log::Record) {
Self::print(record);
}
fn flush(&self) {
}
}
pub fn init() -> Result<(), log::SetLoggerError> {
static INSTANCE: Logger = Logger;
log::set_logger(&INSTANCE)
}