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
use crate::log_specification::LogSpecification;
use crate::primary_writer::PrimaryWriter;
use crate::writers::LogWriter;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
#[derive(Clone)]
pub struct LoggerHandle {
spec: Arc<RwLock<LogSpecification>>,
spec_stack: Vec<LogSpecification>,
primary_writer: Arc<PrimaryWriter>,
other_writers: Arc<HashMap<String, Box<dyn LogWriter>>>,
}
impl LoggerHandle {
pub(crate) fn new(
spec: Arc<RwLock<LogSpecification>>,
primary_writer: Arc<PrimaryWriter>,
other_writers: Arc<HashMap<String, Box<dyn LogWriter>>>,
) -> Self {
Self {
spec,
spec_stack: Vec::default(),
primary_writer,
other_writers,
}
}
#[cfg(feature = "specfile_without_notification")]
pub(crate) fn current_spec(&self) -> Arc<RwLock<LogSpecification>> {
Arc::clone(&self.spec)
}
pub(crate) fn reconfigure(&self, mut max_level: log::LevelFilter) {
for w in self.other_writers.as_ref().values() {
max_level = std::cmp::max(max_level, w.max_log_level());
}
log::set_max_level(max_level);
}
pub fn set_new_spec(&mut self, new_spec: LogSpecification) {
let max_level = new_spec.max_level();
self.spec.write().unwrap().update_from(new_spec);
self.reconfigure(max_level);
}
pub fn parse_new_spec(&mut self, spec: &str) {
self.set_new_spec(LogSpecification::parse(spec).unwrap_or_else(|e| {
eprintln!(
"[flexi_logger] LoggerHandle::parse_new_spec(): failed with {}",
e
);
LogSpecification::off()
}))
}
pub fn push_temp_spec(&mut self, new_spec: LogSpecification) {
self.spec_stack
.push(self.spec.read().unwrap().clone());
self.set_new_spec(new_spec);
}
pub fn parse_and_push_temp_spec(&mut self, new_spec: &str) {
self.spec_stack
.push(self.spec.read().unwrap().clone());
self.set_new_spec(LogSpecification::parse(new_spec).unwrap_or_else(|e| {
eprintln!(
"[flexi_logger] LoggerHandle::parse_new_spec(): failed with {}, \
falling back to empty log spec",
e
);
LogSpecification::off()
}));
}
pub fn pop_temp_spec(&mut self) {
if let Some(previous_spec) = self.spec_stack.pop() {
self.set_new_spec(previous_spec);
}
}
pub fn flush(&self) {
self.primary_writer.flush().ok();
for writer in self.other_writers.values() {
writer.flush().ok();
}
}
pub fn shutdown(&self) {
self.primary_writer.flush().ok();
if let PrimaryWriter::Multi(writer) = &*self.primary_writer {
writer.shutdown();
}
for writer in self.other_writers.values() {
writer.shutdown();
}
}
#[doc(hidden)]
pub fn validate_logs(&self, expected: &[(&'static str, &'static str, &'static str)]) {
self.primary_writer.validate_logs(expected)
}
}