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
mod multi_writer;
pub(crate) mod std_stream;
mod std_writer;
use self::{multi_writer::MultiWriter, std_stream::StdStream, std_writer::StdWriter};
use crate::{
filter::LogLineWriter,
logger::Duplicate,
writers::{FileLogWriter, LogWriter},
{DeferredNow, FormatFunction, WriteMode},
};
use log::Record;
pub(crate) enum PrimaryWriter {
Std(StdWriter),
Multi(MultiWriter),
}
impl PrimaryWriter {
pub fn multi(
duplicate_stderr: Duplicate,
duplicate_stdout: Duplicate,
format_for_stderr: FormatFunction,
format_for_stdout: FormatFunction,
o_file_writer: Option<Box<FileLogWriter>>,
o_other_writer: Option<Box<dyn LogWriter>>,
) -> Self {
Self::Multi(MultiWriter::new(
duplicate_stderr,
duplicate_stdout,
format_for_stderr,
format_for_stdout,
o_file_writer,
o_other_writer,
))
}
pub fn stderr(format: FormatFunction, write_mode: &WriteMode) -> Self {
Self::Std(StdWriter::new(
StdStream::Err(std::io::stderr()),
format,
write_mode,
))
}
pub fn stdout(format: FormatFunction, write_mode: &WriteMode) -> Self {
Self::Std(StdWriter::new(
StdStream::Out(std::io::stdout()),
format,
write_mode,
))
}
pub fn write(&self, now: &mut DeferredNow, record: &Record) -> std::io::Result<()> {
match *self {
Self::Std(ref w) => w.write(now, record),
Self::Multi(ref w) => w.write(now, record),
}
}
pub fn flush(&self) -> std::io::Result<()> {
match *self {
Self::Std(ref w) => w.flush(),
Self::Multi(ref w) => w.flush(),
}
}
pub fn validate_logs(&self, expected: &[(&'static str, &'static str, &'static str)]) {
self.shutdown();
match self {
Self::Std(writer) => {
writer.validate_logs(expected);
}
Self::Multi(writer) => {
writer.validate_logs(expected);
}
}
}
pub fn shutdown(&self) {
self.flush().ok();
match self {
Self::Std(writer) => {
writer.shutdown();
}
Self::Multi(writer) => {
writer.shutdown();
}
}
}
}
impl LogLineWriter for PrimaryWriter {
fn write(&self, now: &mut DeferredNow, record: &Record) -> std::io::Result<()> {
self.write(now, record)
}
}