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
use {
anyhow::Result,
flexi_logger::{
DeferredNow, Duplicate, FileSpec, Logger, LoggerHandle, Record,
WriteMode,
},
regex::Regex,
std::{fmt::Write as FmtWrite, sync::Once},
textwrap::{termwidth, Options},
};
static mut LOGGER_HANDLE: Option<LoggerHandle> = None;
static INIT: Once = Once::new();
static mut LAST_NEWLINE_DELIM_MACHER: Option<Regex> = None;
pub fn setup() {
INIT.call_once(|| {
let handle = Logger::try_with_env_or_str("trace")
.unwrap()
.log_to_file(FileSpec::default().directory("logs"))
.format(multiline_format)
.duplicate_to_stdout(Duplicate::Warn)
.write_mode(WriteMode::Async)
.start()
.expect("Unable to start the logger!");
let matcher = Regex::new(r"(┃)(.*)$").unwrap();
unsafe {
LOGGER_HANDLE = Some(handle);
LAST_NEWLINE_DELIM_MACHER = Some(matcher)
};
});
}
pub fn multiline_format(
w: &mut dyn std::io::Write,
now: &mut DeferredNow,
record: &Record,
) -> Result<(), std::io::Error> {
let size = termwidth().min(74);
let wrap_options = Options::new(size)
.initial_indent("┏ ")
.subsequent_indent("┃ ");
let mut full_line = String::new();
writeln!(
full_line,
"{} [{}] [{}:{}]",
record.level(),
now.now().format("%H:%M:%S%.6f"),
record.file().unwrap_or("<unnamed>"),
record.line().unwrap_or(0),
)
.expect("unable to format first log line");
write!(&mut full_line, "{}", &record.args())
.expect("unable to format log!");
let wrapped = textwrap::fill(&full_line, wrap_options);
let formatted = unsafe {
LAST_NEWLINE_DELIM_MACHER
.as_ref()
.unwrap()
.replace(&wrapped, "┗$2")
};
writeln!(w, "{formatted}")
}