diff --git a/src/util/logger.rs b/src/util/logger.rs index c544502..f89070d 100644 --- a/src/util/logger.rs +++ b/src/util/logger.rs @@ -1,6 +1,7 @@ // prints output to the stderr if the verbosity is // greater than or equal to the logging level of the // notification. +#[macro_export] macro_rules! log { // NOTE: this uses a variadic argument system // copied from the std::fmt eprintln! macro @@ -14,8 +15,6 @@ macro_rules! log { #[cfg(test)] mod tests { - use super::*; - #[test] fn test_name() { log!(1, 1, "this is a {}", "test"); diff --git a/src/validate.rs b/src/validate.rs index b9120c6..f6f9188 100644 --- a/src/validate.rs +++ b/src/validate.rs @@ -1,19 +1,21 @@ use std::{path::{PathBuf, Path, Components, Component}, fs, collections::{HashMap, VecDeque}, borrow::BorrowMut}; use crate::util; +use crate::log; pub fn validate(verbosity: u8, config: util::config::Config) { println!("Validating"); let in_suffix = "in"; //TODO make this configurable? let out_suffix = "out"; + log!(1, verbosity, "LOG 1: Input suffix: {} \nOutput suffix: {}", in_suffix, out_suffix); // get list of all file names in input/output + log!(1, verbosity, "LOG 1:Reading the list of input and output files"); let in_files: Vec = get_dir_files(&config.input_path, verbosity).expect("Failed to read the input directory contents"); let out_files: Vec = get_dir_files(&config.output_path, verbosity).expect("Failed to read the output directory contents"); - let mismatch: Vec<(PathBuf, PathBuf)>; - // check the files for valid suffixes + log!(1, verbosity, "LOG 1:Collecting files with invalid suffixes"); let invalid_suffix_in: Vec = collect_invalid(&in_files, in_suffix, verbosity); let invalid_suffix_out: Vec = collect_invalid(&out_files, out_suffix, verbosity); @@ -27,7 +29,9 @@ pub fn validate(verbosity: u8, config: util::config::Config) { } // make sure each has a match + log!(1, verbosity, "LOG 1:Checking if valid mappings exist between input and output files"); let in_to_out: (Vec<(PathBuf, PathBuf)>, Vec) = map_files(in_files, out_files, &config, verbosity); + log!(1, verbosity, "LOG 1:Verifying file matches"); let invalid: Vec<(PathBuf, PathBuf)> = check_mappings(in_to_out.0, in_suffix, out_suffix, verbosity); print_unmached(in_to_out.1, verbosity); @@ -52,11 +56,13 @@ fn map_files(in_files: Vec, out_files: Vec, config: &util::con for pre in config.input_path.components() { in_prefix.push(pre); } + log!(2, verbosity, "LOG 2: Input prefixes found: {:?}", in_prefix); let mut out_prefix = vec![]; for pre in config.output_path.components() { out_prefix.push(pre); } + log!(2, verbosity, "LOG 2: Output prefixes found: {:?}", out_prefix); // check if the files have the same paths relative to in/out directory for ifile in &in_files { @@ -81,16 +87,21 @@ fn map_files(in_files: Vec, out_files: Vec, config: &util::con // println!("{:?}", ofile); // println!("{:?}", out_comp); + log!(2, verbosity, "LOG 2: Checking mapping of {} to {}", + &ifile.to_str().expect("failed to parse string"), + &ofile.to_str().expect("failed to parse string")); // check if the file names are the same if ofile.file_stem() == ifile.file_stem() { // check that their paths are the same relative to the input/output directory for comp in &in_prefix { if &comp == &in_comp.front().expect("Failed to extract") { in_comp.pop_front(); } } for comp in &out_prefix { if &comp == &out_comp.front().expect("Failed to extract") { out_comp.pop_front(); } } - println!("{:?}\n{:?}\n{:?}", in_prefix, in_comp,out_comp); + // println!("{:?}\n{:?}\n{:?}", in_prefix, in_comp,out_comp); // if the files match, then we add them to the mapping if out_comp == in_comp { - println!("Added to map"); + log!(1, verbosity, "LOG 1: Adding the mapping between {} and {} to the list of file mappings.", + ifile.to_str().expect("failed to parse string"), + ofile.to_str().expect("failed to parse string")); res.0.push((ifile.clone(), ofile.clone())); found = true; } @@ -98,7 +109,9 @@ fn map_files(in_files: Vec, out_files: Vec, config: &util::con } // if we fail to find a match, we add the file to the rest - if !found { res.1.push(ifile.clone()); } + if !found { + log!(1, verbosity, "LOG 1: Adding {} to the list of unmapped files", ifile.to_str().expect("failed to parse string")); + res.1.push(ifile.clone()); } } let mut cl: Vec = Vec::from(out_files.clone()); @@ -108,6 +121,7 @@ fn map_files(in_files: Vec, out_files: Vec, config: &util::con } cl.retain(|x| !r.contains(&x)); + log!(1, verbosity, "LOG 1: Adding the following files to the list of unmapped files:\n{:?}", cl); res.1.append(&mut cl); // println!("{:?}", res); return res; @@ -126,8 +140,10 @@ fn check_mappings(mappings: Vec<(PathBuf, PathBuf)>, in_suffix: &str, out_suffix let mut res: Vec<(PathBuf, PathBuf)> = vec![]; for tup in &mappings { + log!(2, verbosity, "LOG 2: Verifying the mapping {:?}", tup); if !(tup.0.as_path().extension().unwrap() == in_suffix) || !(tup.1.as_path().extension().unwrap() == out_suffix) { res.push(tup.clone()); + log!(2, verbosity, "LOG 2: Added {:?} to the list of invalid mappings", tup); } } // println!("{:?}", res); @@ -146,12 +162,19 @@ fn collect_invalid(files: &Vec, suf: &str, verbosity: u8) -> Vec = vec![]; for file in files { + log!(2, verbosity, "LOG 2: Checking the validity of {} against the suffix {}:\n$${}$$\n$${}$$", + file.to_str().expect("failed to parse string"), + suf, + file.extension().expect("Failed to extract suffix").to_str().expect("Failed to parse string"), + suf); + let extension = file.extension().expect("Invalid file name for validation"); if extension.to_str() != Some(suf) { res.push(file.clone()); + log!(2, verbosity, "LOG 2: Added {} to invalid files", file.to_str().expect("failed to parse string")); } } - println!("{:?}", res); + // println!("{:?}", res); return res } /** @@ -164,6 +187,7 @@ fn collect_invalid(files: &Vec, suf: &str, verbosity: u8) -> Vec Result, &'static str> { let mut res: Vec = vec![]; + log!(2, verbosity, "LOG 2: Checking the directory {} for files", path.to_str().expect("failed to parse string")); // get the readout of the path for error handling let entries = match fs::read_dir(path) { Ok(x) => x, @@ -175,6 +199,7 @@ fn get_dir_files(path: &PathBuf, verbosity: u8) -> Result, &'static if p.path().is_dir() { (); } else { + log!(2, verbosity, "LOG 2: Found file {}", stringify!(p)); res.push(p.path()); } }