-
Notifications
You must be signed in to change notification settings - Fork 45
/
multi_writer.rs
220 lines (206 loc) · 7.45 KB
/
multi_writer.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
use crate::{
logger::Duplicate,
util::{eprint_err, write_buffered, ErrorCode},
writers::{FileLogWriter, FileLogWriterBuilder, FileLogWriterConfig, LogWriter},
{DeferredNow, FlexiLoggerError, FormatFunction},
};
use log::Record;
use std::{
io::Write,
path::PathBuf,
sync::atomic::{AtomicU8, Ordering},
};
// The `MultiWriter` writes logs to a FileLogWriter and/or another Writer,
// and can duplicate messages to stderr or stdout.
pub(crate) struct MultiWriter {
duplicate_stderr: AtomicU8,
duplicate_stdout: AtomicU8,
support_capture: bool,
format_for_stderr: FormatFunction,
format_for_stdout: FormatFunction,
o_file_writer: Option<Box<FileLogWriter>>,
o_other_writer: Option<Box<dyn LogWriter>>,
}
impl MultiWriter {
pub(crate) fn new(
duplicate_stderr: Duplicate,
duplicate_stdout: Duplicate,
support_capture: bool,
format_for_stderr: FormatFunction,
format_for_stdout: FormatFunction,
o_file_writer: Option<Box<FileLogWriter>>,
o_other_writer: Option<Box<dyn LogWriter>>,
) -> Self {
MultiWriter {
duplicate_stderr: AtomicU8::new(duplicate_stderr as u8),
duplicate_stdout: AtomicU8::new(duplicate_stdout as u8),
support_capture,
format_for_stderr,
format_for_stdout,
o_file_writer,
o_other_writer,
}
}
pub(crate) fn reset_file_log_writer(
&self,
flwb: &FileLogWriterBuilder,
) -> Result<(), FlexiLoggerError> {
self.o_file_writer
.as_ref()
.map_or(Err(FlexiLoggerError::NoFileLogger), |flw| flw.reset(flwb))
}
pub(crate) fn flw_config(&self) -> Result<FileLogWriterConfig, FlexiLoggerError> {
self.o_file_writer
.as_ref()
.map_or(Err(FlexiLoggerError::NoFileLogger), |flw| flw.config())
}
pub(crate) fn reopen_output(&self) -> Result<(), FlexiLoggerError> {
match (&self.o_file_writer, &self.o_other_writer) {
(None, None) => Ok(()),
(Some(ref w), None) => w.reopen_outputfile(),
(None, Some(w)) => w.reopen_output(),
(Some(w1), Some(w2)) => {
let r1 = w1.reopen_outputfile();
let r2 = w2.reopen_output();
match (r1, r2) {
(Ok(()), Ok(())) => Ok(()),
(Err(e), _) | (Ok(()), Err(e)) => Err(e),
}
}
}
}
pub(crate) fn existing_log_files(&self) -> Result<Vec<PathBuf>, FlexiLoggerError> {
if let Some(fw) = self.o_file_writer.as_ref() {
fw.existing_log_files()
} else {
Ok(Vec::new())
}
}
pub(crate) fn adapt_duplication_to_stderr(&self, dup: Duplicate) {
self.duplicate_stderr.store(dup as u8, Ordering::Relaxed);
}
pub(crate) fn adapt_duplication_to_stdout(&self, dup: Duplicate) {
self.duplicate_stdout.store(dup as u8, Ordering::Relaxed);
}
fn duplication_to_stderr(&self) -> Duplicate {
Duplicate::from(self.duplicate_stderr.load(Ordering::Relaxed))
}
fn duplication_to_stdout(&self) -> Duplicate {
Duplicate::from(self.duplicate_stdout.load(Ordering::Relaxed))
}
pub(crate) fn trigger_rotation(&self) -> Result<(), FlexiLoggerError> {
if let Some(ref w) = &self.o_file_writer {
w.trigger_rotation()?;
}
// if let Some(w) = &self.o_other_writer {
// w.trigger_rotation(); // todo is not (yet?) part of trait LogWriter
// }
Ok(())
}
}
impl LogWriter for MultiWriter {
fn validate_logs(&self, expected: &[(&'static str, &'static str, &'static str)]) {
if let Some(ref writer) = self.o_file_writer {
(*writer).validate_logs(expected);
}
if let Some(ref writer) = self.o_other_writer {
(*writer).validate_logs(expected);
}
}
fn write(&self, now: &mut DeferredNow, record: &Record) -> std::io::Result<()> {
if match self.duplication_to_stderr() {
Duplicate::Error => record.level() == log::Level::Error,
Duplicate::Warn => record.level() <= log::Level::Warn,
Duplicate::Info => record.level() <= log::Level::Info,
Duplicate::Debug => record.level() <= log::Level::Debug,
Duplicate::Trace | Duplicate::All => true,
Duplicate::None => false,
} {
if self.support_capture {
let mut tmp_buf = Vec::<u8>::with_capacity(200);
(self.format_for_stderr)(&mut tmp_buf, now, record)
.unwrap_or_else(|e| eprint_err(ErrorCode::Format, "formatting failed", &e));
eprintln!("{}", String::from_utf8_lossy(&tmp_buf));
} else {
write_buffered(
self.format_for_stderr,
now,
record,
&mut std::io::stderr(),
#[cfg(test)]
None,
)?;
}
}
if match self.duplication_to_stdout() {
Duplicate::Error => record.level() == log::Level::Error,
Duplicate::Warn => record.level() <= log::Level::Warn,
Duplicate::Info => record.level() <= log::Level::Info,
Duplicate::Debug => record.level() <= log::Level::Debug,
Duplicate::Trace | Duplicate::All => true,
Duplicate::None => false,
} {
if self.support_capture {
let mut tmp_buf = Vec::<u8>::with_capacity(200);
(self.format_for_stdout)(&mut tmp_buf, now, record)
.unwrap_or_else(|e| eprint_err(ErrorCode::Format, "formatting failed", &e));
println!("{}", String::from_utf8_lossy(&tmp_buf));
} else {
write_buffered(
self.format_for_stdout,
now,
record,
&mut std::io::stdout(),
#[cfg(test)]
None,
)?;
}
}
if let Some(ref writer) = self.o_file_writer {
writer.write(now, record)?;
}
if let Some(ref writer) = self.o_other_writer {
writer.write(now, record)?;
}
Ok(())
}
/// Provides the maximum log level that is to be written.
fn max_log_level(&self) -> log::LevelFilter {
*self
.o_file_writer
.as_ref()
.map(|w| w.max_log_level())
.iter()
.chain(
self.o_other_writer
.as_ref()
.map(|w| w.max_log_level())
.iter(),
)
.max()
.unwrap(/*ok*/)
}
fn flush(&self) -> std::io::Result<()> {
if let Some(ref writer) = self.o_file_writer {
writer.flush()?;
}
if let Some(ref writer) = self.o_other_writer {
writer.flush()?;
}
if !matches!(self.duplication_to_stderr(), Duplicate::None) {
std::io::stderr().flush()?;
}
if !matches!(self.duplication_to_stdout(), Duplicate::None) {
std::io::stdout().flush()?;
}
Ok(())
}
fn shutdown(&self) {
if let Some(ref writer) = self.o_file_writer {
writer.shutdown();
}
if let Some(ref writer) = self.o_other_writer {
writer.shutdown();
}
}
}