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
mod enums;
mod write_buffer;
pub use enums::{Level, OutputKind};
pub use write_buffer::WriteBuffer;
use crate::timestamp::get_timestamp;
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::io;
pub struct Logger<'o> {
file_options: OpenOptions,
level_outputs: HashMap<Level, OutputKind<'o>>,
write_buffers: HashMap<OutputKind<'o>, WriteBuffer>,
format: String,
}
impl<'o> Logger<'o> {
pub fn new() -> Option<Self> {
let mut options = OpenOptions::new();
options.write(true).create(true).truncate(true);
let mut map_level = HashMap::new();
map_level.insert(Level::INFO, OutputKind::STDOUT);
map_level.insert(Level::DEBUG, OutputKind::STDOUT);
map_level.insert(Level::WARNING, OutputKind::STDERR);
map_level.insert(Level::ERROR, OutputKind::STDERR);
let mut stdout = if let Ok(buff) = WriteBuffer::new(&OutputKind::STDOUT, &options) {
buff
} else {
return None;
};
let mut stderr = if let Ok(buff) = WriteBuffer::new(&OutputKind::STDERR, &options) {
buff
} else {
return None;
};
stdout.increase_count();
stderr.increase_count();
let mut map_buffer = HashMap::new();
map_buffer.insert(OutputKind::STDOUT, stdout);
map_buffer.insert(OutputKind::STDERR, stderr);
let log = Logger {
file_options: options,
level_outputs: map_level,
write_buffers: map_buffer,
format: String::from("[%l](%t) - %m"),
};
return Some(log);
}
pub fn config_format<S: ToString + ?Sized>(&mut self, new_format: &S) {
self.format = new_format.to_string();
}
fn config_output(&mut self, kind: OutputKind<'o>, level: Level) -> io::Result<()> {
match self.write_buffers.get_mut(&kind) {
None => {
let buffer = WriteBuffer::new(&kind, &self.file_options)?;
self.write_buffers.insert(kind.clone(), buffer);
}
Some(buffer) => buffer.increase_count(),
};
let output = self.level_outputs.get_mut(&level).unwrap();
if match self.write_buffers.get_mut(output) {
None => false,
Some(buffer) => buffer.decrease_count(),
} {
self.write_buffers.remove(output);
}
*output = kind;
Ok(())
}
pub fn config_info(&mut self, kind: OutputKind<'o>) {
if let Err(err) = self.config_output(kind, Level::INFO) {
self.error(&format!("Failed to configure INFO: {}", err));
}
}
pub fn config_debug(&mut self, kind: OutputKind<'o>) {
if let Err(err) = self.config_output(kind, Level::DEBUG) {
self.error(&format!("Failed to configure DEBUG: {}", err));
}
}
pub fn config_warning(&mut self, kind: OutputKind<'o>) {
if let Err(err) = self.config_output(kind, Level::WARNING) {
self.error(&format!("Failed to configure WARNING: {}", err));
}
}
pub fn config_error(&mut self, kind: OutputKind<'o>) {
if let Err(err) = self.config_output(kind, Level::ERROR) {
self.error(&format!("Failed to configure ERROR: {}", err));
}
}
fn write_log(&mut self, msg: &str, level: Level) {
if let Some(buffer) = self
.write_buffers
.get_mut(self.level_outputs.get(&level).unwrap())
{
let mut log_msg = self
.format
.replace("%l", &level.to_string())
.replace("%t", &get_timestamp().to_string())
.replace("%m", msg);
log_msg.push('\n');
if let Err(err) = buffer.log(log_msg) {
match level {
Level::ERROR => {}
_ => self.error(&format!("Failed to log to `{}`: {}", level, err)),
}
};
};
}
pub fn info(&mut self, msg: &str) {
self.write_log(msg, Level::INFO);
}
pub fn debug(&mut self, msg: &str) {
self.write_log(msg, Level::DEBUG);
}
pub fn warning(&mut self, msg: &str) {
self.write_log(msg, Level::WARNING);
}
pub fn error(&mut self, msg: &str) {
self.write_log(msg, Level::ERROR);
}
}