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
use state::Storage;
use std::sync::RwLock;
use std::fs::File;
use crate::storage::configuration::Configuration;
extern crate log;
static FILELOGGERCONFIG: Storage<RwLock<FileLoggerConfig>> = Storage::new();
pub struct FileLoggerConfig {
pub enable: bool,
}
pub struct FileLogger {
logger: simplelog::WriteLogger<File>,
}
impl FileLogger {
pub fn new(logger: simplelog::WriteLogger<File>) -> Self {
let cfg = Configuration::get();
let config = FileLoggerConfig {
enable: cfg.debug.log,
};
FILELOGGERCONFIG.set(RwLock::new(config));
FileLogger { logger }
}
pub fn enable(enable: bool) {
let mut config = FILELOGGERCONFIG.get().write().unwrap();
config.enable = enable;
}
}
impl log::Log for FileLogger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
let config = FILELOGGERCONFIG.get().read().unwrap();
config.enable && self.logger.enabled(metadata)
}
fn log(&self, record: &log::Record) {
let config = FILELOGGERCONFIG.get().read().unwrap();
if config.enable{
self.logger.log(record);
}
}
fn flush(&self) {
let config = FILELOGGERCONFIG.get().read().unwrap();
if config.enable{
self.logger.flush();
}
}
}