From 791083f099e0549db333977c83ce26fa1e93a0b2 Mon Sep 17 00:00:00 2001 From: networkException Date: Fri, 16 Aug 2024 11:01:07 +0200 Subject: [PATCH] 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. --- src/main.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index ce7e7d7..1080e9f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { + fn non_matching_characters(&self, string: &str) -> HashSet { 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::(); - 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);