Skip to content

Commit 18d5de2

Browse files
SAY-5Alonely0
authored andcommitted
address review: replace print macros with structured logging
Signed-off-by: SAY-5 <saiasish.cnp@gmail.com>
1 parent 4a5d83e commit 18d5de2

3 files changed

Lines changed: 7 additions & 8 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ unused_qualifications = "warn"
9393
all = { level = "warn", priority = -1 }
9494
cargo = { level = "warn", priority = -1 }
9595
pedantic = { level = "warn", priority = -1 }
96+
print_stdout = "warn" # restriction: forbid print/println macros
97+
print_stderr = "warn" # restriction: forbid eprint/eprintln macros
9698
use_self = "warn" # nursery lint
9799
cargo_common_metadata = "allow" # 3240
98100
multiple_crate_versions = "allow" # 2882

src/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,9 @@ fn uu_main() -> Result<()> {
4646
return Ok(());
4747
}
4848
};
49-
// Avoid `println!`, which panics on broken pipe / ENOSPC (e.g. `>/dev/full`).
50-
// Match the uutils convention: silently exit on BrokenPipe, report other I/O
51-
// errors to stderr and exit with failure.
52-
if let Err(e) = writeln!(io::stdout(), "---\n{ast}") {
53-
if e.kind() == io::ErrorKind::BrokenPipe {
54-
return Ok(());
55-
}
49+
if let Err(e) = writeln!(io::stdout(), "---\n{ast}")
50+
&& e.kind() != io::ErrorKind::BrokenPipe
51+
{
5652
exit_err(Some(format!("awk: error writing to standard output: {e}")));
5753
}
5854
dbg!(arena.chunk_capacity());

src/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// files that was distributed with this source code.
55

66
use std::fmt::{Debug, Display};
7+
use std::io::{self, Write};
78
use std::panic::{UnwindSafe, catch_unwind, set_hook, take_hook};
89
use std::process::exit;
910

@@ -73,7 +74,7 @@ pub fn exit_with(res: Result<Option<impl Into<ExitCode>>, impl Display + Debug>)
7374

7475
pub fn exit_err(err: Option<impl Display + Debug>) -> ! {
7576
if let Some(err) = err {
76-
eprintln!("{err}");
77+
let _ = writeln!(io::stderr(), "{err}");
7778
}
7879
exit(EXIT_FAILURE)
7980
}

0 commit comments

Comments
 (0)