Skip to content

summarize: Don't panic on broken pipe #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions summarize/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use analyzeme::AnalysisResults;
use analyzeme::ProfilingData;
use std::error::Error;
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::io::{BufReader, BufWriter, Write};
use std::{path::PathBuf, time::Duration};

use clap::Parser;
Expand Down Expand Up @@ -94,6 +94,17 @@ fn aggregate(opt: AggregateOpt) -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}

fn write_stdout(fmt_args: std::fmt::Arguments) -> Result<(), Box<dyn Error + Send + Sync>> {
std::io::stdout().write_fmt(fmt_args).map_err(|e| {
if matches!(e.kind(), std::io::ErrorKind::BrokenPipe) {
// Broken pipes are somewhat expected, exit without writing anything else.
std::process::exit(0);
}

format!("failed writing to stdout: {e}").into()
})
}

fn diff(opt: DiffOpt) -> Result<(), Box<dyn Error + Send + Sync>> {
let base = process_results(&opt.base)?;
let change = process_results(&opt.change)?;
Expand Down Expand Up @@ -142,7 +153,7 @@ fn diff(opt: DiffOpt) -> Result<(), Box<dyn Error + Send + Sync>> {

table.printstd();

println!("Total cpu time: {:?}", results.total_time);
write_stdout(format_args!("Total cpu time: {:?}\n", results.total_time))?;

let mut table = Table::new();

Expand Down Expand Up @@ -286,13 +297,13 @@ fn summarize(opt: SummarizeOpt) -> Result<(), Box<dyn Error + Send + Sync>> {

table.printstd();

println!("Total cpu time: {:?}", results.total_time);
write_stdout(format_args!("Total cpu time: {:?}\n", results.total_time))?;

if percent_above != 0.0 {
println!(
"Filtered results account for {:.3}% of total time.",
write_stdout(format_args!(
"Filtered results account for {:.3}% of total time.\n",
percent_total_time
);
))?;
}

let mut table = Table::new();
Expand Down