aboutsummaryrefslogtreecommitdiff
path: root/src/output/aggregators.rs
diff options
context:
space:
mode:
authorDaniel <kingdread@gmx.de>2020-08-28 15:49:12 +0200
committerDaniel <kingdread@gmx.de>2020-08-28 15:59:17 +0200
commit09e1f9c330ec99de06c834b2de95c378bc6d00d3 (patch)
tree387d58df01ef305245cc58eb456d3e7731dbfc16 /src/output/aggregators.rs
parentea60bea5854976e45cee5026c5bdc21d19c0dc0f (diff)
downloadraidgrep-09e1f9c330ec99de06c834b2de95c378bc6d00d3.tar.gz
raidgrep-09e1f9c330ec99de06c834b2de95c378bc6d00d3.tar.bz2
raidgrep-09e1f9c330ec99de06c834b2de95c378bc6d00d3.zip
add --count/-n flag
Diffstat (limited to 'src/output/aggregators.rs')
-rw-r--r--src/output/aggregators.rs30
1 files changed, 29 insertions, 1 deletions
diff --git a/src/output/aggregators.rs b/src/output/aggregators.rs
index 83171eb..34ecea4 100644
--- a/src/output/aggregators.rs
+++ b/src/output/aggregators.rs
@@ -8,7 +8,11 @@
//! an Aggregator must make sure that the data is protected by a mutex or similar.
use super::{super::LogResult, formats::Format, sorting::Sorting};
-use std::{io::Write, sync::Mutex};
+use std::{
+ io::Write,
+ sync::atomic::{AtomicU32, Ordering},
+ sync::Mutex,
+};
pub trait Aggregator: Sync {
fn push_item(&self, item: LogResult, format: &dyn Format, stream: &mut dyn Write);
@@ -62,3 +66,27 @@ impl Aggregator for SortedOutput {
stream.flush().unwrap();
}
}
+
+/// An aggregator that just counts how many logs have been found.
+#[derive(Debug)]
+pub struct CountingOutput {
+ counter: AtomicU32,
+}
+
+impl CountingOutput {
+ pub fn new() -> Self {
+ CountingOutput {
+ counter: AtomicU32::new(0),
+ }
+ }
+}
+
+impl Aggregator for CountingOutput {
+ fn push_item(&self, _: LogResult, _: &dyn Format, _: &mut dyn Write) {
+ self.counter.fetch_add(1, Ordering::SeqCst);
+ }
+
+ fn finish(self: Box<Self>, _: &dyn Format, stream: &mut dyn Write) {
+ writeln!(stream, "{}", self.counter.into_inner()).unwrap();
+ }
+}