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
use std::{cell::RefCell, io};
use crate::{Log, Writer};
pub struct JSONWriter<W: io::Write + Sync + Send>(RefCell<Box<W>>);
impl<W: io::Write + Sync + Send> Writer for JSONWriter<W> {
fn write_log(&self, value: &Log) -> Result<(), io::Error> {
serde_json::to_writer(self.0.borrow_mut().as_mut(), value).map_err(io::Error::from)?;
self.0
.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(RefCell::new(Box::new(w))))
}