diff options
Diffstat (limited to 'src/output/aggregators.rs')
-rw-r--r-- | src/output/aggregators.rs | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/src/output/aggregators.rs b/src/output/aggregators.rs index 34ecea4..c3d971a 100644 --- a/src/output/aggregators.rs +++ b/src/output/aggregators.rs @@ -14,10 +14,13 @@ use std::{ sync::Mutex, }; +use anyhow::Result; + pub trait Aggregator: Sync { - fn push_item(&self, item: LogResult, format: &dyn Format, stream: &mut dyn Write); + fn push_item(&self, item: LogResult, format: &dyn Format, stream: &mut dyn Write) + -> Result<()>; // When the `unsized_locals` feature is stable, we could rewrite this to finish(self, ...). - fn finish(self: Box<Self>, format: &dyn Format, stream: &mut dyn Write); + fn finish(self: Box<Self>, format: &dyn Format, stream: &mut dyn Write) -> Result<()>; } /// An aggregator that just pushes through each item to the output stream without any sorting or @@ -25,13 +28,21 @@ pub trait Aggregator: Sync { pub struct WriteThrough; impl Aggregator for WriteThrough { - fn push_item(&self, item: LogResult, format: &dyn Format, stream: &mut dyn Write) { + fn push_item( + &self, + item: LogResult, + format: &dyn Format, + stream: &mut dyn Write, + ) -> Result<()> { let text = format.format_result(&item); - stream.write_all(text.as_bytes()).unwrap(); - stream.flush().unwrap(); + stream.write_all(text.as_bytes())?; + stream.flush()?; + Ok(()) } - fn finish(self: Box<Self>, _: &dyn Format, _: &mut dyn Write) {} + fn finish(self: Box<Self>, _: &dyn Format, _: &mut dyn Write) -> Result<()> { + Ok(()) + } } /// An aggregator that keeps all found logs in memory and sorts them before outputting them. @@ -51,19 +62,21 @@ impl SortedOutput { } impl Aggregator for SortedOutput { - fn push_item(&self, item: LogResult, _: &dyn Format, _: &mut dyn Write) { - self.items.lock().unwrap().push(item) + fn push_item(&self, item: LogResult, _: &dyn Format, _: &mut dyn Write) -> Result<()> { + self.items.lock().unwrap().push(item); + Ok(()) } - fn finish(self: Box<Self>, format: &dyn Format, stream: &mut dyn Write) { + fn finish(self: Box<Self>, format: &dyn Format, stream: &mut dyn Write) -> Result<()> { let SortedOutput { sorting, items } = *self; let mut items = items.into_inner().unwrap(); items.sort_unstable_by(|a, b| sorting.cmp(a, b)); for item in items { let text = format.format_result(&item); - stream.write_all(text.as_bytes()).unwrap(); + stream.write_all(text.as_bytes())?; } - stream.flush().unwrap(); + stream.flush()?; + Ok(()) } } @@ -82,11 +95,13 @@ impl CountingOutput { } impl Aggregator for CountingOutput { - fn push_item(&self, _: LogResult, _: &dyn Format, _: &mut dyn Write) { + fn push_item(&self, _: LogResult, _: &dyn Format, _: &mut dyn Write) -> Result<()> { self.counter.fetch_add(1, Ordering::SeqCst); + Ok(()) } - fn finish(self: Box<Self>, _: &dyn Format, stream: &mut dyn Write) { - writeln!(stream, "{}", self.counter.into_inner()).unwrap(); + fn finish(self: Box<Self>, _: &dyn Format, stream: &mut dyn Write) -> Result<()> { + writeln!(stream, "{}", self.counter.into_inner())?; + Ok(()) } } |