Skip to content

Commit

Permalink
process: display: s/PrintSingle/PrintEvent/
Browse files Browse the repository at this point in the history
This is makes this display logic more self described and more aligned
with "PrintSeries".

Suggested-by: Adrian Moreno <[email protected]>
Signed-off-by: Antoine Tenart <[email protected]>
  • Loading branch information
atenart committed Jul 5, 2024
1 parent dc849b1 commit 9aca6ab
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
20 changes: 10 additions & 10 deletions retis/src/benchmark/events_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ 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)? {
EventResult::Event(event) => event,
_ => 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 {
Expand All @@ -35,19 +35,19 @@ 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 {
p.process_one(&event)?;
}
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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions retis/src/collect/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion retis/src/process/cli/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions retis/src/process/cli/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl SubCommandParserRunner for Sort {
.open(&out)
.or_else(|_| bail!("Could not create or open '{}'", out.display()))?,
)),
PrintSingleFormat::Json,
PrintEventFormat::Json,
));
}

Expand All @@ -109,7 +109,7 @@ impl SubCommandParserRunner for Sort {

printers.push(PrintSeries::new(
Box::new(stdout()),
PrintSingleFormat::Text(format),
PrintEventFormat::Text(format),
));
}

Expand Down
24 changes: 12 additions & 12 deletions retis/src/process/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Write>,
format: PrintSingleFormat,
format: PrintEventFormat,
}

impl PrintSingle {
pub(crate) fn new(writer: Box<dyn Write>, format: PrintSingleFormat) -> Self {
impl PrintEvent {
pub(crate) fn new(writer: Box<dyn Write>, 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::<CommonEventMd>(SectionId::MdCommon) {
format.set_monotonic_offset(common.clock_monotonic_offset);
}
Expand All @@ -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)?;
Expand All @@ -61,11 +61,11 @@ impl PrintSingle {
/// Handles event series formatting and writing to a `Write`.
pub(crate) struct PrintSeries {
writer: Box<dyn Write>,
format: PrintSingleFormat,
format: PrintEventFormat,
}

impl PrintSeries {
pub(crate) fn new(writer: Box<dyn Write>, format: PrintSingleFormat) -> Self {
pub(crate) fn new(writer: Box<dyn Write>, format: PrintEventFormat) -> Self {
Self { writer, format }
}

Expand All @@ -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::<CommonEventMd>(SectionId::MdCommon) {
Expand All @@ -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)?;
Expand Down

0 comments on commit 9aca6ab

Please sign in to comment.