Skip to content
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

Adjust logging levels #32

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/handlers/ar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl super::Processor for Ar {

fn process(&self, input_path: &Path) -> Result<super::ProcessResult> {
let mut have_mod = false;
let (mut io, mut input) = InputOutputHelper::open(input_path, self.config.check)?;
let (mut io, mut input) = InputOutputHelper::open(input_path, self.config.check, true)?;

let mut buf = [0; MAGIC.len()];
input.read_exact(&mut buf)?;
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/jar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl super::Processor for Jar {

fn process(&self, input_path: &Path) -> Result<super::ProcessResult> {
let mut have_mod = false;
let (mut io, input) = InputOutputHelper::open(input_path, self.config.check)?;
let (mut io, input) = InputOutputHelper::open(input_path, self.config.check, true)?;
let mut input = zip::ZipArchive::new(input)?;

io.open_output()?;
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/javadoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl super::Processor for Javadoc {
let mut have_mod = false;
let mut after_header = false;

let (mut io, input) = InputOutputHelper::open(input_path, self.config.check)?;
let (mut io, input) = InputOutputHelper::open(input_path, self.config.check, true)?;

io.open_output()?;
let mut output = BufWriter::new(io.output.as_mut().unwrap());
Expand Down
18 changes: 13 additions & 5 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod javadoc;
pub mod pyc;

use anyhow::{bail, Context, Result};
use log::{debug, info, warn};
use log::{log, debug, info, warn, Level};
use serde::{Serialize, Deserialize};
use std::ascii::escape_default;
use std::collections::HashMap;
Expand Down Expand Up @@ -371,6 +371,7 @@ pub struct InputOutputHelper<'a> {
pub output: Option<File>,

pub check: bool,
pub verbose: bool, // include logging about each modified file
}

impl<'a> Drop for InputOutputHelper<'a> {
Expand All @@ -396,7 +397,11 @@ fn unwrap_os_string(filename: &OsStr) -> Result<&str> {
}

impl<'a> InputOutputHelper<'a> {
pub fn open(input_path: &'a Path, check: bool) -> Result<(Self, BufReader<File>)> {
pub fn open(
input_path: &'a Path,
check: bool,
verbose: bool,
) -> Result<(Self, BufReader<File>)> {

let input = File::open(input_path)
.with_context(|| format!("Cannot open {:?}", input_path))?;
Expand All @@ -410,6 +415,7 @@ impl<'a> InputOutputHelper<'a> {
output_path: None,
output: None,
check,
verbose,
};

Ok((io, input))
Expand All @@ -435,7 +441,7 @@ impl<'a> InputOutputHelper<'a> {
bail!("{}: cannot open temporary file: {}", output_path.display(), e);
}

debug!("{}: stale temporary file found, removing", output_path.display());
info!("{}: stale temporary file found, removing", output_path.display());
fs::remove_file(&output_path)?;
openopts.open(&output_path)?
}
Expand Down Expand Up @@ -474,7 +480,8 @@ impl<'a> InputOutputHelper<'a> {
// If it has multiple links, we reopen the orignal file and rewrite it.
// This way the inode number is retained and hard links are not broken.
if meta.nlink() == 1 {
info!("{}: replacing with normalized version", self.input_path.display());
log!(if self.verbose { Level::Info } else { Level::Debug },
"{}: replacing with normalized version", self.input_path.display());

if !self.check {
output.set_permissions(meta.permissions())?;
Expand All @@ -495,7 +502,8 @@ impl<'a> InputOutputHelper<'a> {
Ok(ProcessResult::Replaced)

} else {
info!("{}: rewriting with normalized contents", self.input_path.display());
log!(if self.verbose { Level::Info } else { Level::Debug },
"{}: rewriting with normalized contents", self.input_path.display());

if !self.check {
output.seek(io::SeekFrom::Start(0))?;
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/pyc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ impl super::Processor for Pyc {
}

fn process(&self, input_path: &Path) -> Result<super::ProcessResult> {
let (mut io, input) = InputOutputHelper::open(input_path, self.config.check)?;
let (mut io, input) = InputOutputHelper::open(input_path, self.config.check, true)?;

let mut parser = PycParser::from_file(input_path, input)?;
if parser.version < (3, 0) {
Expand Down Expand Up @@ -852,7 +852,7 @@ impl super::Processor for PycZeroMtime {
}

fn process(&self, input_path: &Path) -> Result<super::ProcessResult> {
let (mut io, input) = InputOutputHelper::open(input_path, self.config.check)?;
let (mut io, input) = InputOutputHelper::open(input_path, self.config.check, false)?;

let mut parser = PycParser::from_file(input_path, input)?;
let have_mod = parser.set_zero_mtime()?;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl handlers::Processor for Trivial {
fn test_input_output_helper_drop() {
let (_dir, input) = prepare_dir("tests/cases/libempty.a").unwrap();

let (mut helper, _) = handlers::InputOutputHelper::open(&*input, false).unwrap();
let (mut helper, _) = handlers::InputOutputHelper::open(&*input, false, false).unwrap();
helper.open_output().unwrap();

let output_path = helper.output_path.as_ref().unwrap().clone();
Expand Down
Loading