Main: Improve logging output

This patch improves the logging output by being more specific
about what is invalid about a file when checking its content.
This commit is contained in:
networkException 2024-08-16 11:01:07 +02:00
parent 70ebd0076c
commit 791083f099
Signed by: networkException
GPG key ID: E3877443AE684391

View file

@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::fmt::{self, Formatter, Display};
use std::fs;
use std::io;
@ -29,7 +30,7 @@ impl CharacterSet {
}
}
fn non_matching_characters(&self, string: &str) -> Vec<char> {
fn non_matching_characters(&self, string: &str) -> HashSet<char> {
let charset = self.charset();
string.chars().filter(|input_character| !charset.contains(input_character)).collect()
@ -118,7 +119,7 @@ struct Arguments {
}
fn main() -> io::Result<()> {
env_logger::init_from_env(env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "debug"));
env_logger::init_from_env(env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"));
let arguments = Arguments::parse();
let path = arguments.path;
@ -144,7 +145,7 @@ fn main() -> io::Result<()> {
process::exit(0);
},
WhenFileExists::Error => {
log::error!("File exists, exiting.");
log::error!("File exists, exiting with error.");
process::exit(1);
},
WhenFileExists::CheckContent => {
@ -158,29 +159,38 @@ fn main() -> io::Result<()> {
process::exit(0);
} else {
let found_non_matching = non_matching_characters.iter().take(5).collect::<String>();
let found_non_matching = escape_string::escape(found_non_matching.as_str());
let found_non_matching = escape_string::escape(found_non_matching.as_str()).to_string();
let found_non_matching = if non_matching_characters.len() > 5 {
found_non_matching + "..."
} else {
found_non_matching
};
let error_message = match (found_non_matching.is_empty(), content_length == length) {
(false, true) => format!("invalid character set (found [{found_non_matching}] not matching {character_set})"),
(true, false) => format!("invalid length (found {content_length}, expected {length})"),
(_, _) => format!("invalid character set (found [{found_non_matching}] not matching {character_set}) and length (found {content_length}, expected {length})")
};
match arguments.when_file_content_is_invalid {
WhenFileContentIsInvalid::Exit => {
log::info!("File exists, invalid character set (found [{}]) or length (found {}), exiting.", found_non_matching, content_length);
log::info!("File exists, {error_message}, exiting.");
process::exit(0);
},
WhenFileContentIsInvalid::Error => {
log::error!("File exists, invalid character set (found [{}]) or length (found {}), exiting.", found_non_matching, content_length);
log::error!("File exists, {error_message}, exiting with error.");
process::exit(1);
},
WhenFileContentIsInvalid::Override => {
log::debug!("File exists, invalid character set (found [{}]) or length (found {}).", found_non_matching, content_length);
log::info!("File exists, {error_message}, overriding.");
},
};
}
},
WhenFileExists::Override => (),
WhenFileExists::Override => {
log::info!("File exists, overriding.");
process::exit(0);
},
};
}
@ -190,7 +200,7 @@ fn main() -> io::Result<()> {
let generated = character_set.generate(arguments.length, &mut rng);
if let Some(path) = path {
log::debug!("Writing to {}", path);
log::info!("Writing to {}", path);
fs::write(path, &generated)?;
} else {
println!("{}", generated);