-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
238 lines (213 loc) · 8.4 KB
/
config.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
230
231
232
233
234
235
236
237
238
use log::LevelFilter;
use alloc::string::String;
#[derive(Debug, Clone, Copy)]
/// Padding to be used for logging the level
pub enum LevelPadding {
/// Add spaces on the left side
Left,
/// Add spaces on the right side
Right,
/// Do not pad the level
Off,
}
#[derive(Debug, Clone, Copy)]
/// Padding to be used for logging the thread id/name
pub enum ThreadPadding {
/// Add spaces on the left side, up to usize many
Left(usize),
/// Add spaces on the right side, up to usize many
Right(usize),
/// Do not pad the thread id/name
Off,
}
#[derive(Debug, Clone, Copy, PartialEq)]
/// Mode for logging the thread name or id or both.
pub enum ThreadLogMode {
/// Log thread ids only
IDs,
/// Log the thread names only
Names,
/// If this thread is named, log the name. Otherwise, log the thread id.
Both,
}
/// Configuration for the Loggers
///
/// All loggers print the message in the following form:
/// `00:00:00 [LEVEL] crate::module: [lib.rs::100] your_message`
/// Every space delimited part except the actual message is optional.
///
/// Pass this struct to your logger to change when these information shall
/// be logged.
///
/// Construct using `Default` or using `ConfigBuilder`
#[derive(Debug, Clone)]
pub struct Config {
pub(crate) time: LevelFilter,
pub(crate) level: LevelFilter,
pub(crate) level_padding: LevelPadding,
pub(crate) thread: LevelFilter,
pub(crate) thread_log_mode: ThreadLogMode,
pub(crate) thread_padding: ThreadPadding,
pub(crate) target: LevelFilter,
pub(crate) location: LevelFilter,
// pub(crate) time_format: Cow<'static, str>,
// pub(crate) filter_allow: Cow<'static, [Cow<'static, str>]>,
// pub(crate) filter_ignore: Cow<'static, [Cow<'static, str>]>,
}
/// Builder for the Logger Configurations (`Config`)
///
/// All loggers print the message in the following form:
/// `00:00:00 [LEVEL] crate::module: [lib.rs::100] your_message`
/// Every space delimited part except the actual message is optional.
///
/// Use this struct to create a custom `Config` changing when these information shall
/// be logged. Every part can be enabled for a specific Level and is then
/// automatically enable for all lower levels as well.
///
/// The Result is that the logging gets more detailed the more verbose it gets.
/// E.g. to have one part shown always use `Level::Error`. But if you
/// want to show the source line only on `Trace` use that.
#[derive(Debug, Clone)]
pub struct ConfigBuilder(Config);
impl ConfigBuilder {
/// Create a new default ConfigBuilder
pub fn new() -> ConfigBuilder {
ConfigBuilder(Config::new())
}
/// Set at which level and below the level itself shall be logged (default is Error)
pub fn set_max_level<'a>(&'a mut self, level: LevelFilter) -> &'a mut ConfigBuilder {
self.0.level = level;
self
}
/// Set at which level and below the current time shall be logged (default is Error)
pub fn set_time_level<'a>(&'a mut self, time: LevelFilter) -> &'a mut ConfigBuilder {
self.0.time = time;
self
}
/// Set at which level and below the thread id shall be logged. (default is Debug)
pub fn set_thread_level<'a>(&'a mut self, thread: LevelFilter) -> &'a mut ConfigBuilder {
self.0.thread = thread;
self
}
/// Set at which level and below the target shall be logged. (default is Debug)
pub fn set_target_level<'a>(&'a mut self, target: LevelFilter) -> &'a mut ConfigBuilder {
self.0.target = target;
self
}
/// Set at which level and below a source code reference shall be logged (default is Trace)
pub fn set_location_level<'a>(&'a mut self, location: LevelFilter) -> &'a mut ConfigBuilder {
self.0.location = location;
self
}
/// Set how the levels should be padded, when logging (default is Left)
pub fn set_level_padding<'a>(&'a mut self, padding: LevelPadding) -> &'a mut ConfigBuilder {
self.0.level_padding = padding;
self
}
/// Set how the thread should be padded
pub fn set_thread_padding<'a>(&'a mut self, padding: ThreadPadding) -> &'a mut ConfigBuilder {
self.0.thread_padding = padding;
self
}
/// Set the mode for logging the thread
pub fn set_thread_mode<'a>(&'a mut self, mode: ThreadLogMode) -> &'a mut ConfigBuilder {
self.0.thread_log_mode = mode;
self
}
/// Set time chrono [strftime] format string.
///
/// [strftime]: https://docs.rs/chrono/0.4.0/chrono/format/strftime/index.html#specifiers
pub fn set_time_format_str<'a>(
&'a mut self,
time_format: &'static str,
) -> &'a mut ConfigBuilder {
// self.0.time_format = Cow::Borrowed(time_format);
self
}
/// Set time chrono [strftime] format string.
///
/// [strftime]: https://docs.rs/chrono/0.4.0/chrono/format/strftime/index.html#specifiers
pub fn set_time_format<'a>(&'a mut self, time_format: String) -> &'a mut ConfigBuilder {
// self.0.time_format = Cow::Owned(time_format);
self
}
/// Add allowed module filters.
/// If any are specified, only records from modules starting with one of these entries will be printed
///
/// For example, `add_filter_allow_str("tokio::uds")` would allow only logging from the `tokio` crates `uds` module.
pub fn add_filter_allow_str<'a>(
&'a mut self,
time_format: &'static str,
) -> &'a mut ConfigBuilder {
// let mut list = Vec::from(&*self.0.filter_allow);
// list.push(Cow::Borrowed(time_format));
// self.0.filter_allow = Cow::Owned(list);
self
}
/// Add allowed module filters.
/// If any are specified, only records from modules starting with one of these entries will be printed
///
/// For example, `add_filter_allow(format!("{}{}","tokio", "uds"))` would allow only logging from the `tokio` crates `uds` module.
pub fn add_filter_allow<'a>(&'a mut self, time_format: String) -> &'a mut ConfigBuilder {
// let mut list = Vec::from(&*self.0.filter_allow);
// list.push(Cow::Owned(time_format));
// self.0.filter_allow = Cow::Owned(list);
self
}
/// Clear allowed module filters.
/// If none are specified, nothing is filtered out
pub fn clear_filter_allow<'a>(&'a mut self) -> &'a mut ConfigBuilder {
// self.0.filter_allow = Cow::Borrowed(&[]);
self
}
/// Add denied module filters.
/// If any are specified, records from modules starting with one of these entries will be ignored
///
/// For example, `add_filter_ignore_str("tokio::uds")` would deny logging from the `tokio` crates `uds` module.
pub fn add_filter_ignore_str<'a>(
&'a mut self,
time_format: &'static str,
) -> &'a mut ConfigBuilder {
// let mut list = Vec::from(&*self.0.filter_ignore);
// list.push(Cow::Borrowed(time_format));
// self.0.filter_ignore = Cow::Owned(list);
self
}
/// Add denied module filters.
/// If any are specified, records from modules starting with one of these entries will be ignored
///
/// For example, `add_filter_ignore(format!("{}{}","tokio", "uds"))` would deny logging from the `tokio` crates `uds` module.
pub fn add_filter_ignore<'a>(&'a mut self, time_format: String) -> &'a mut ConfigBuilder {
// let mut list = Vec::from(&*self.0.filter_ignore);
// list.push(Cow::Owned(time_format));
// self.0.filter_ignore = Cow::Owned(list);
self
}
/// Clear ignore module filters.
/// If none are specified, nothing is filtered
pub fn clear_filter_ignore<'a>(&'a mut self) -> &'a mut ConfigBuilder {
// self.0.filter_ignore = Cow::Borrowed(&[]);
self
}
/// Build new `Config`
pub fn build(&mut self) -> Config {
self.0.clone()
}
}
impl Config {
pub const fn new() -> Config {
Config {
time: LevelFilter::Error,
level: LevelFilter::Error,
level_padding: LevelPadding::Left,
thread: LevelFilter::Off,
thread_log_mode: ThreadLogMode::IDs,
thread_padding: ThreadPadding::Left(8),
target: LevelFilter::Error,
location: LevelFilter::Off,
// time_format: Cow::Borrowed("{:02}:{:02}:{:02}"),
// filter_allow: Cow::Borrowed(&[]),
// filter_ignore: Cow::Borrowed(&[]),
}
}
}