From 9aca6aba067a7985eb29252a29bd6f5cbf8117a2 Mon Sep 17 00:00:00 2001 From: Antoine Tenart Date: Fri, 5 Jul 2024 10:39:17 +0200 Subject: [PATCH] process: display: s/PrintSingle/PrintEvent/ This is makes this display logic more self described and more aligned with "PrintSeries". Suggested-by: Adrian Moreno Signed-off-by: Antoine Tenart --- retis/src/benchmark/events_output.rs | 20 ++++++++++---------- retis/src/collect/collector.rs | 8 ++++---- retis/src/process/cli/print.rs | 2 +- retis/src/process/cli/sort.rs | 4 ++-- retis/src/process/display.rs | 24 ++++++++++++------------ 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/retis/src/benchmark/events_output.rs b/retis/src/benchmark/events_output.rs index 8a2d889fe..0c39c3b60 100644 --- a/retis/src/benchmark/events_output.rs +++ b/retis/src/benchmark/events_output.rs @@ -14,7 +14,7 @@ pub(super) fn bench(ci: bool) -> Result<()> { true => 1, }; - // PrintSingle benchmark + // PrintEvent benchmark let mut factory = FileEventsFactory::new("retis/test_data/test_events_bench.json")?; let event = match factory.next_event(None)? { @@ -22,9 +22,9 @@ pub(super) fn bench(ci: bool) -> Result<()> { _ => bail!("Could not get event from test file"), }; - let mut p = PrintSingle::new( + let mut p = PrintEvent::new( Box::new(OpenOptions::new().write(true).open("/dev/null")?), - PrintSingleFormat::Text(DisplayFormat::new(DisplayFormatFlavor::SingleLine)), + PrintEventFormat::Text(DisplayFormat::new(DisplayFormatFlavor::SingleLine)), ); let now = Instant::now(); for _ in 0..iters { @@ -35,9 +35,9 @@ pub(super) fn bench(ci: bool) -> Result<()> { now.elapsed().as_micros() ); - let mut p = PrintSingle::new( + let mut p = PrintEvent::new( Box::new(OpenOptions::new().write(true).open("/dev/null")?), - PrintSingleFormat::Text(DisplayFormat::new(DisplayFormatFlavor::MultiLine)), + PrintEventFormat::Text(DisplayFormat::new(DisplayFormatFlavor::MultiLine)), ); let now = Instant::now(); for _ in 0..iters { @@ -45,9 +45,9 @@ pub(super) fn bench(ci: bool) -> Result<()> { } println!("1M_print_single_multiline_us {}", now.elapsed().as_micros()); - let mut p = PrintSingle::new( + let mut p = PrintEvent::new( Box::new(OpenOptions::new().write(true).open("/dev/null")?), - PrintSingleFormat::Json, + PrintEventFormat::Json, ); let now = Instant::now(); for _ in 0..iters { @@ -69,7 +69,7 @@ pub(super) fn bench(ci: bool) -> Result<()> { let mut p = PrintSeries::new( Box::new(OpenOptions::new().write(true).open("/dev/null")?), - PrintSingleFormat::Text(DisplayFormat::new(DisplayFormatFlavor::SingleLine)), + PrintEventFormat::Text(DisplayFormat::new(DisplayFormatFlavor::SingleLine)), ); let now = Instant::now(); for _ in 0..iters { @@ -82,7 +82,7 @@ pub(super) fn bench(ci: bool) -> Result<()> { let mut p = PrintSeries::new( Box::new(OpenOptions::new().write(true).open("/dev/null")?), - PrintSingleFormat::Text(DisplayFormat::new(DisplayFormatFlavor::MultiLine)), + PrintEventFormat::Text(DisplayFormat::new(DisplayFormatFlavor::MultiLine)), ); let now = Instant::now(); for _ in 0..iters { @@ -92,7 +92,7 @@ pub(super) fn bench(ci: bool) -> Result<()> { let mut p = PrintSeries::new( Box::new(OpenOptions::new().write(true).open("/dev/null")?), - PrintSingleFormat::Json, + PrintEventFormat::Json, ); let now = Instant::now(); for _ in 0..iters { diff --git a/retis/src/collect/collector.rs b/retis/src/collect/collector.rs index 68065c5c6..bc1ddf246 100644 --- a/retis/src/collect/collector.rs +++ b/retis/src/collect/collector.rs @@ -425,15 +425,15 @@ impl Collectors { format.set_time_format(collect.time_format.into()); format.set_monotonic_offset(monotonic_clock_offset()?); - printers.push(PrintSingle::new( + printers.push(PrintEvent::new( Box::new(io::stdout()), - PrintSingleFormat::Text(format), + PrintEventFormat::Text(format), )); } // Write the events to a file if asked to. if let Some(out) = collect.out.as_ref() { - let mut printer = PrintSingle::new( + let mut printer = PrintEvent::new( Box::new(BufWriter::new( OpenOptions::new() .create(true) @@ -442,7 +442,7 @@ impl Collectors { .open(out) .or_else(|_| bail!("Could not create or open '{}'", out.display()))?, )), - PrintSingleFormat::Json, + PrintEventFormat::Json, ); // Store "metadata events" first. diff --git a/retis/src/process/cli/print.rs b/retis/src/process/cli/print.rs index 81ed1ee5b..a4a44a7eb 100644 --- a/retis/src/process/cli/print.rs +++ b/retis/src/process/cli/print.rs @@ -45,7 +45,7 @@ impl SubCommandParserRunner for Print { format.set_time_format(self.time_format.into()); // Formatter & printer for events. - let mut output = PrintSingle::new(Box::new(stdout()), PrintSingleFormat::Text(format)); + let mut output = PrintEvent::new(Box::new(stdout()), PrintEventFormat::Text(format)); use EventResult::*; while run.running() { diff --git a/retis/src/process/cli/sort.rs b/retis/src/process/cli/sort.rs index 64049d1c1..eb1fe602b 100644 --- a/retis/src/process/cli/sort.rs +++ b/retis/src/process/cli/sort.rs @@ -99,7 +99,7 @@ impl SubCommandParserRunner for Sort { .open(&out) .or_else(|_| bail!("Could not create or open '{}'", out.display()))?, )), - PrintSingleFormat::Json, + PrintEventFormat::Json, )); } @@ -109,7 +109,7 @@ impl SubCommandParserRunner for Sort { printers.push(PrintSeries::new( Box::new(stdout()), - PrintSingleFormat::Text(format), + PrintEventFormat::Text(format), )); } diff --git a/retis/src/process/display.rs b/retis/src/process/display.rs index d56f53abd..bd0bb56c1 100644 --- a/retis/src/process/display.rs +++ b/retis/src/process/display.rs @@ -5,30 +5,30 @@ use anyhow::Result; use super::series::EventSeries; use crate::events::*; -/// Select the format in follow when printing events with `PrintSingle`. +/// Select the format in follow when printing events with `PrintEvent`. /// - Text(format): display the events in a text representation following the /// rules defined in `format` (see `DisplayFormat`). /// - Json: display the event as JSON. -pub(crate) enum PrintSingleFormat { +pub(crate) enum PrintEventFormat { Text(DisplayFormat), Json, } /// Handles event individually and write to a `Write`. -pub(crate) struct PrintSingle { +pub(crate) struct PrintEvent { writer: Box, - format: PrintSingleFormat, + format: PrintEventFormat, } -impl PrintSingle { - pub(crate) fn new(writer: Box, format: PrintSingleFormat) -> Self { +impl PrintEvent { + pub(crate) fn new(writer: Box, format: PrintEventFormat) -> Self { Self { writer, format } } /// Process events one by one (format & print). pub(crate) fn process_one(&mut self, e: &Event) -> Result<()> { match self.format { - PrintSingleFormat::Text(ref mut format) => { + PrintEventFormat::Text(ref mut format) => { if let Some(common) = e.get_section::(SectionId::MdCommon) { format.set_monotonic_offset(common.clock_monotonic_offset); } @@ -42,7 +42,7 @@ impl PrintSingle { } } } - PrintSingleFormat::Json => { + PrintEventFormat::Json => { let mut event = serde_json::to_vec(&e.to_json())?; event.push(b'\n'); self.writer.write_all(&event)?; @@ -61,11 +61,11 @@ impl PrintSingle { /// Handles event series formatting and writing to a `Write`. pub(crate) struct PrintSeries { writer: Box, - format: PrintSingleFormat, + format: PrintEventFormat, } impl PrintSeries { - pub(crate) fn new(writer: Box, format: PrintSingleFormat) -> Self { + pub(crate) fn new(writer: Box, format: PrintEventFormat) -> Self { Self { writer, format } } @@ -88,7 +88,7 @@ impl PrintSeries { pub(crate) fn process_one(&mut self, series: &EventSeries) -> Result<()> { let mut content = String::new(); match self.format { - PrintSingleFormat::Text(ref mut format) => { + PrintEventFormat::Text(ref mut format) => { let mut indent = 0; for event in series.events.iter() { if let Some(common) = event.get_section::(SectionId::MdCommon) { @@ -107,7 +107,7 @@ impl PrintSeries { self.writer.write_all(content.as_bytes())?; } } - PrintSingleFormat::Json => { + PrintEventFormat::Json => { let mut event = serde_json::to_vec(&series.to_json())?; event.push(b'\n'); self.writer.write_all(&event)?;