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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::{
fmt::{self, Write},
time::SystemTime,
};
use chrono::prelude::*;
use crate::{
formatter::{FmtExtraInfo, Formatter},
Error, Record, StringBuf, EOL,
};
pub struct FullFormatter {
local_time_cacher: spin::Mutex<LocalTimeCacher>,
}
impl FullFormatter {
pub fn new() -> FullFormatter {
FullFormatter {
local_time_cacher: spin::Mutex::new(LocalTimeCacher::new()),
}
}
fn format_impl(
&self,
record: &Record,
dest: &mut StringBuf,
) -> Result<FmtExtraInfo, fmt::Error> {
{
let mut local_time_cacher = self.local_time_cacher.lock();
let time = local_time_cacher.get(record.time());
dest.write_str("[")?;
dest.write_str(time.0)?;
dest.write_str(".")?;
write!(dest, "{:03}", time.1)?;
dest.write_str("] [")?;
}
if let Some(logger_name) = record.logger_name() {
dest.write_str(logger_name)?;
dest.write_str("] [")?;
}
let style_range_begin = dest.len();
dest.write_str(record.level().as_str())?;
let style_range_end = dest.len();
if let Some(srcloc) = record.source_location() {
dest.write_str("] [")?;
dest.write_str(srcloc.module_path())?;
dest.write_str(", ")?;
dest.write_str(srcloc.file_name())?;
dest.write_str(":")?;
write!(dest, "{}", srcloc.line())?;
}
dest.write_str("] ")?;
dest.write_str(record.payload())?;
dest.write_str(EOL)?;
Ok(FmtExtraInfo {
style_range: Some(style_range_begin..style_range_end),
})
}
}
impl Formatter for FullFormatter {
fn format(&self, record: &Record, dest: &mut StringBuf) -> crate::Result<FmtExtraInfo> {
self.format_impl(record, dest).map_err(Error::FormatRecord)
}
}
impl Default for FullFormatter {
fn default() -> FullFormatter {
FullFormatter::new()
}
}
#[derive(Clone, Default)]
struct LocalTimeCacher {
last_secs: i64,
local_time_str: Option<String>,
}
impl LocalTimeCacher {
fn new() -> LocalTimeCacher {
LocalTimeCacher::default()
}
fn get(&mut self, system_time: SystemTime) -> (&str, u32) {
let utc_time: DateTime<Utc> = system_time.into();
let millisecond = utc_time.nanosecond() % 1_000_000_000 / 1_000_000;
(self.update(utc_time), millisecond)
}
fn update(&mut self, utc_time: DateTime<Utc>) -> &str {
let secs = utc_time.timestamp();
if self.local_time_str.is_none() || self.last_secs != secs {
let local_time: DateTime<Local> = utc_time.into();
self.local_time_str = Some(format!(
"{}-{:02}-{:02} {:02}:{:02}:{:02}",
local_time.year(),
local_time.month(),
local_time.day(),
local_time.hour(),
local_time.minute(),
local_time.second(),
));
self.last_secs = secs;
}
self.local_time_str.as_ref().unwrap().as_str()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Level, EOL};
#[test]
fn format() {
let record = Record::new(Level::Warn, "test log content");
let mut buf = StringBuf::new();
let extra_info = FullFormatter::new().format(&record, &mut buf).unwrap();
let local_time: DateTime<Local> = record.time().into();
assert_eq!(
format!(
"[{}] [warn] test log content{}",
local_time.format("%Y-%m-%d %H:%M:%S.%3f"),
EOL
),
buf
);
assert_eq!(Some(27..31), extra_info.style_range());
}
}