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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#[cfg(feature = "async")]
use {
crate::util::{eprint_err, ASYNC_FLUSH, ASYNC_SHUTDOWN, ERRCODE},
crossbeam_channel::{self, SendError, Sender},
crossbeam_queue::ArrayQueue,
};
use {
super::std_stream::StdStream,
crate::{
util::{io_err, write_buffered},
writers::LogWriter,
DeferredNow, EffectiveWriteMode, FormatFunction, WriteMode,
},
log::Record,
std::io::{BufWriter, Write},
};
#[cfg(test)]
use std::io::Cursor;
#[cfg(any(feature = "async", test))]
use std::sync::Arc;
use std::sync::Mutex;
#[cfg(feature = "async")]
use std::thread::JoinHandle;
pub(crate) struct StdWriter {
format: FormatFunction,
writer: InnerStdWriter,
#[cfg(test)]
validation_buffer: Arc<Mutex<Cursor<Vec<u8>>>>,
}
enum InnerStdWriter {
Unbuffered(StdStream),
Buffered(Mutex<BufWriter<StdStream>>),
#[cfg(feature = "async")]
Async(AsyncHandle),
}
#[cfg(feature = "async")]
#[derive(Debug)]
struct AsyncHandle {
sender: Sender<Vec<u8>>,
mo_thread_handle: Mutex<Option<JoinHandle<()>>>,
a_pool: Arc<ArrayQueue<Vec<u8>>>,
msg_capa: usize,
}
#[cfg(feature = "async")]
impl AsyncHandle {
fn new(
stdstream: StdStream,
_bufsize: usize,
pool_capa: usize,
msg_capa: usize,
#[cfg(test)] validation_buffer: &Arc<Mutex<Cursor<Vec<u8>>>>,
) -> Self {
let (sender, receiver) = crossbeam_channel::unbounded::<Vec<u8>>();
let a_pool = Arc::new(ArrayQueue::new(pool_capa));
let mo_thread_handle = crate::threads::start_async_stdwriter(
stdstream,
receiver,
Arc::clone(&a_pool),
msg_capa,
#[cfg(test)]
Arc::clone(validation_buffer),
);
AsyncHandle {
sender,
mo_thread_handle,
a_pool,
msg_capa,
}
}
fn pop_buffer(&self) -> Vec<u8> {
self.a_pool
.pop()
.unwrap_or_else(|| Vec::with_capacity(self.msg_capa))
}
fn send(&self, buffer: Vec<u8>) -> Result<(), SendError<Vec<u8>>> {
self.sender.send(buffer)
}
}
impl StdWriter {
pub(crate) fn new(
stdstream: StdStream,
format: FormatFunction,
write_mode: &WriteMode,
) -> Self {
#[cfg(test)]
let validation_buffer = Arc::new(Mutex::new(Cursor::new(Vec::<u8>::new())));
let writer = match write_mode.inner() {
EffectiveWriteMode::Direct => InnerStdWriter::Unbuffered(stdstream),
EffectiveWriteMode::BufferDontFlushWith(capacity) => {
InnerStdWriter::Buffered(Mutex::new(BufWriter::with_capacity(capacity, stdstream)))
}
EffectiveWriteMode::BufferAndFlushWith(_, _) => {
unreachable!("Sync InnerStdWriter with own flushing is not implemented")
}
#[cfg(feature = "async")]
EffectiveWriteMode::AsyncWith {
bufsize,
pool_capa,
message_capa,
flush_interval,
} => {
assert_eq!(
flush_interval,
std::time::Duration::from_secs(0),
"Async InnerStdWriter with own flushing is not implemented"
);
InnerStdWriter::Async(AsyncHandle::new(
stdstream,
bufsize,
pool_capa,
message_capa,
#[cfg(test)]
&validation_buffer,
))
}
};
Self {
format,
writer,
#[cfg(test)]
validation_buffer,
}
}
}
impl LogWriter for StdWriter {
#[inline]
fn write(&self, now: &mut DeferredNow, record: &Record) -> std::io::Result<()> {
match &self.writer {
InnerStdWriter::Unbuffered(stdstream) => {
let mut w = stdstream.lock();
write_buffered(
self.format,
now,
record,
&mut w,
#[cfg(test)]
Some(&self.validation_buffer),
)
}
InnerStdWriter::Buffered(mbuf_w) => {
let mut w = mbuf_w.lock().map_err(|_e| io_err("Poison"))?;
write_buffered(
self.format,
now,
record,
&mut *w,
#[cfg(test)]
Some(&self.validation_buffer),
)
}
#[cfg(feature = "async")]
InnerStdWriter::Async(handle) => {
let mut buffer = handle.pop_buffer();
(self.format)(&mut buffer, now, record)
.unwrap_or_else(|e| eprint_err(ERRCODE::Format, "formatting failed", &e));
buffer
.write_all(b"\n")
.unwrap_or_else(|e| eprint_err(ERRCODE::Write, "writing failed", &e));
handle.send(buffer).map_err(|_e| io_err("Send"))?;
Ok(())
}
}
}
#[inline]
fn flush(&self) -> std::io::Result<()> {
match &self.writer {
InnerStdWriter::Unbuffered(stdstream) => {
let mut w = stdstream.lock();
w.flush()
}
InnerStdWriter::Buffered(mbuf_w) => {
let mut w = mbuf_w.lock().map_err(|_e| io_err("Poison"))?;
w.flush()
}
#[cfg(feature = "async")]
InnerStdWriter::Async(handle) => {
let mut buffer = handle.pop_buffer();
buffer.extend(ASYNC_FLUSH);
handle.send(buffer).ok();
Ok(())
}
}
}
fn shutdown(&self) {
#[cfg(feature = "async")]
if let InnerStdWriter::Async(handle) = &self.writer {
let mut buffer = handle.pop_buffer();
buffer.extend(ASYNC_SHUTDOWN);
handle.send(buffer).ok();
if let Ok(ref mut o_th) = handle.mo_thread_handle.lock() {
o_th.take().and_then(|th| th.join().ok());
}
}
}
#[cfg(not(test))]
fn validate_logs(&self, _expected: &[(&'static str, &'static str, &'static str)]) {}
#[cfg(test)]
fn validate_logs(&self, expected: &[(&'static str, &'static str, &'static str)]) {
{
use std::io::BufRead;
let write_cursor = self.validation_buffer.lock().unwrap();
let mut reader = std::io::BufReader::new(Cursor::new(write_cursor.get_ref()));
let mut buf = String::new();
for tuple in expected {
buf.clear();
reader.read_line(&mut buf).unwrap();
assert!(buf.contains(tuple.0), "Did not find tuple.0 = {}", tuple.0);
assert!(buf.contains(tuple.1), "Did not find tuple.1 = {}", tuple.1);
assert!(buf.contains(tuple.2), "Did not find tuple.2 = {}", tuple.2);
}
buf.clear();
reader.read_line(&mut buf).unwrap();
assert!(
buf.is_empty(),
"Found more log lines than expected: {} ",
buf
);
}
}
}
#[cfg(test)]
mod test {
use super::{StdStream, StdWriter};
use crate::{opt_format, writers::LogWriter, DeferredNow, WriteMode};
use log::Level::{Error, Info, Warn};
#[test]
fn test_with_validation() {
let writer = StdWriter::new(
StdStream::Err(std::io::stderr()),
opt_format,
&WriteMode::Direct,
);
let mut rb = log::Record::builder();
rb.target("myApp")
.file(Some("std_writer.rs"))
.line(Some(222))
.module_path(Some("std_writer::test::test_with_validation"));
rb.level(Error)
.args(format_args!("This is an error message"));
writer.write(&mut DeferredNow::new(), &rb.build()).unwrap();
rb.level(Warn).args(format_args!("This is a warning"));
writer.write(&mut DeferredNow::new(), &rb.build()).unwrap();
rb.level(Info).args(format_args!("This is an info message"));
writer.write(&mut DeferredNow::new(), &rb.build()).unwrap();
writer.validate_logs(&[
("ERROR", "std_writer.rs:222", "error"),
("WARN", "std_writer.rs:222", "warning"),
("INFO", "std_writer.rs:222", "info"),
]);
}
}