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
use log;
use LogSpecification;
use primary_writer::PrimaryWriter;
use writers::LogWriter;
use regex::Regex;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
pub enum LogSpec {
STATIC(LogSpecification),
DYNAMIC(Arc<RwLock<LogSpecification>>),
}
pub struct FlexiLogger {
log_specification: LogSpec,
primary_writer: Arc<PrimaryWriter>,
other_writers: HashMap<String, Box<LogWriter>>,
}
pub struct ReconfigurationHandle {
spec: Arc<RwLock<LogSpecification>>,
primary_writer: Arc<PrimaryWriter>,
}
impl ReconfigurationHandle {
pub fn set_new_spec(&mut self, new_spec: LogSpecification) {
let mut guard = self.spec.write().unwrap();
guard.reconfigure(new_spec);
}
pub fn parse_new_spec(&mut self, spec: &str) {
let mut guard = self.spec.write().unwrap();
guard.reconfigure(LogSpecification::parse(spec));
}
#[doc(hidden)]
pub fn validate_logs(&self, expected: &[(&'static str, &'static str, &'static str)]) -> bool {
Borrow::<PrimaryWriter>::borrow(&self.primary_writer).validate_logs(expected)
}
}
pub fn reconfiguration_handle(
spec: Arc<RwLock<LogSpecification>>,
primary_writer: Arc<PrimaryWriter>,
) -> ReconfigurationHandle {
ReconfigurationHandle {
spec,
primary_writer,
}
}
impl FlexiLogger {
pub fn new(
log_specification: LogSpec,
primary_writer: Arc<PrimaryWriter>,
other_writers: HashMap<String, Box<LogWriter>>,
) -> FlexiLogger {
FlexiLogger {
log_specification,
primary_writer,
other_writers,
}
}
fn fl_enabled(&self, level: log::Level, target: &str) -> bool {
match self.log_specification {
LogSpec::STATIC(ref ls) => ls.enabled(level, target),
LogSpec::DYNAMIC(ref locked_ls) => {
let guard = locked_ls.read();
guard.as_ref()
.unwrap()
.enabled(level, target)
}
}
}
}
impl log::Log for FlexiLogger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
self.fl_enabled(metadata.level(), metadata.target())
}
fn log(&self, record: &log::Record) {
let target = record.metadata().target();
if target.starts_with('{') {
let mut use_default = false;
let targets: Vec<&str> = target[1..(target.len() - 1)].split(',').collect();
for t in targets {
if t == "_Default" {
use_default = true;
} else {
match self.other_writers.get(t) {
None => eprintln!("bad writer spec: {}", t),
Some(writer) => {
writer.write(record);
}
}
}
}
if !use_default {
return;
}
}
if !self.enabled(record.metadata()) {
return;
}
let check_text_filter = |text_filter: &Option<Regex>| {
if let Some(filter) = text_filter.as_ref() {
filter.is_match(&*record.args().to_string())
} else {
true
}
};
if !match self.log_specification {
LogSpec::STATIC(ref ls) => check_text_filter(ls.text_filter()),
LogSpec::DYNAMIC(ref locked_ls) => {
let guard = locked_ls.read();
check_text_filter(
guard.as_ref().unwrap().text_filter(),
)
}
} {
return;
}
self.primary_writer.write(record);
}
fn flush(&self) {
self.primary_writer.flush();
for writer in self.other_writers.values() {
writer.flush();
}
}
}