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
use std::{cell::RefCell, io};
use crate::{Log, Writer};
use parking_lot::Mutex;
pub struct JSONWriter<W: io::Write + Sync + Send>(Mutex<RefCell<Box<W>>>);
impl<W: io::Write + Sync + Send> Writer for JSONWriter<W> {
fn write_log(&self, value: &Log) -> Result<(), io::Error> {
let w = self.0.lock();
serde_json::to_writer(w.borrow_mut().as_mut(), value).map_err(io::Error::from)?;
w.borrow_mut()
.as_mut()
.write_all(b"\n")
.map_err(io::Error::from)?;
Ok(())
}
}
pub fn new_json_writer<W: io::Write + Sync + Send + 'static>(w: W) -> Box<dyn Writer> {
Box::new(JSONWriter(Mutex::new(RefCell::new(Box::new(w)))))
}