-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.rs
229 lines (207 loc) · 5.94 KB
/
logging.rs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use crate::{Config, LevelPadding, ThreadPadding};
use log::{LevelFilter, Record};
use core::fmt::{self, Display, Error, Formatter, Write};
use core::ops::Deref;
use clock_trait::Instant;
#[inline(always)]
pub fn try_log<Clock, W>(config: &Config, record: &Record<'_>, write: &mut W) -> Result<(), Error>
where
W: Write + Sized,
Clock: Instant + Display,
{
if should_skip(config, record) {
return Ok(());
}
if config.time <= record.level() && config.time != LevelFilter::Off {
write_time::<Clock, _>(write, config)?;
}
if config.level <= record.level() && config.level != LevelFilter::Off {
write_level(record, write, config)?;
}
if config.thread <= record.level() && config.thread != LevelFilter::Off {
write_thread_name(write, config)?;
}
if config.target <= record.level() && config.target != LevelFilter::Off {
write_target(record, write)?;
}
if config.location <= record.level() && config.location != LevelFilter::Off {
write_location(record, write)?;
}
write_args(record, write)
}
// static SYSTEM_CLOCK: Clock = Clock::init(48_000_000);
//
// struct Clock {
// ticks: Mutex<Data = u64>,
// frequency: u64,
// }
//
// impl Clock {
// pub const fn init(frequency: u64) -> Clock {
// Clock{ticks: Mutex::new(0u64), frequency}
// }
//
// pub fn to_nanos(&self) -> u64 {
// let ticks = self.ticks.lock();
// self.frequency * 1_000_000_000 / ticks.deref()
// }
// }
//
// struct Instant(core::time::Duration);
//
// impl Instant {
// pub fn now() -> Instant {
// unsafe {
// Instant(core::time::Duration::from_nanos(
// SYSTEM_CLOCK.to_nanos(),
// ))
// }
// }
//
// pub fn duration_since_epoch(&self) -> Duration {
// todo!()
// }
// }
//
// struct Duration(core::time::Duration);
//
// impl Duration {
// pub fn as_hours(&self) -> u64 {
// self.as_mins() / 60
// }
//
// pub fn as_mins(&self) -> u64 {
// self.as_secs() / 60
// }
// pub fn as_secs(&self) -> u64 {
// self.0.as_secs()
// }
// }
//
// impl core::fmt::Display for Duration {
// fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
// write!(
// f,
// "{}:{}:{}",
// self.as_hours(),
// self.as_mins(),
// self.as_secs()
// )
// }
// }
#[inline(always)]
pub fn write_time<Clock, W>(write: &mut W, config: &Config) -> Result<(), Error>
where
W: Write + Sized,
Clock: Instant + Display,
{
let cur_time = Clock::now();
write!(write, "{} ", cur_time)?;
Ok(())
}
#[inline(always)]
pub fn write_level<W>(record: &Record<'_>, write: &mut W, config: &Config) -> Result<(), Error>
where
W: Write + Sized,
{
match config.level_padding {
LevelPadding::Left => write!(write, "[{: >5}] ", record.level())?,
LevelPadding::Right => write!(write, "[{: <5}] ", record.level())?,
LevelPadding::Off => write!(write, "[{}] ", record.level())?,
};
Ok(())
}
#[inline(always)]
pub fn write_target<W>(record: &Record<'_>, write: &mut W) -> Result<(), Error>
where
W: Write + Sized,
{
write!(write, "{}: ", record.target())?;
Ok(())
}
#[inline(always)]
pub fn write_location<W>(record: &Record<'_>, write: &mut W) -> Result<(), Error>
where
W: Write + Sized,
{
let file = record.file().unwrap_or("<unknown>");
if let Some(line) = record.line() {
write!(write, "[{}:{}] ", file, line)?;
} else {
write!(write, "[{}:<unknown>] ", file)?;
}
Ok(())
}
pub fn write_thread_name<W>(write: &mut W, config: &Config) -> Result<(), Error>
where
W: Write + Sized,
{
let name = module_path!();
match config.thread_padding {
ThreadPadding::Left { 0: qty } => {
write!(write, "({name:>0$}) ", qty, name = name)?;
}
ThreadPadding::Right { 0: qty } => {
write!(write, "({name:<0$}) ", qty, name = name)?;
}
ThreadPadding::Off => {
write!(write, "({}) ", name)?;
}
}
Ok(())
}
// pub fn write_thread_id<W>(write: &mut W, config: &Config) -> Result<(), Error>
// where
// W: Write + Sized,
// {
// let id = format!("{:?}", thread::current().id());
// let id = id.replace("ThreadId(", "");
// let id = id.replace(")", "");
// match config.thread_padding {
// ThreadPadding::Left{0: qty} => {
// write!(write, "({id:>0$}) ", qty, id=id)?;
// }
// ThreadPadding::Right{0: qty} => {
// write!(write, "({id:<0$}) ", qty, id=id)?;
// }
// ThreadPadding::Off => {
// write!(write, "({}) ", id)?;
// }
// }
// Ok(())
// }
#[inline(always)]
pub fn write_args<W>(record: &Record<'_>, write: &mut W) -> Result<(), Error>
where
W: Write + Sized,
{
writeln!(write, "{}", record.args())?;
Ok(())
}
#[inline(always)]
pub fn should_skip(config: &Config, record: &Record<'_>) -> bool {
// If a module path and allowed list are available
// match (record.target(), &*config.filter_allow) {
// (path, allowed) if allowed.len() > 0 => {
// // Check that the module path matches at least one allow filter
// if let None = allowed.iter().find(|v| path.starts_with(&***v)) {
// // If not, skip any further writing
// return true;
// }
// }
// _ => {}
// }
//
// // If a module path and ignore list are available
// match (record.target(), &*config.filter_ignore) {
// (path, ignore) if ignore.len() > 0 => {
// // Check that the module path does not match any ignore filters
// if let Some(_) = ignore.iter().find(|v| path.starts_with(&***v)) {
// // If not, skip any further writing
// return true;
// }
// }
// _ => {}
// }
return false;
}