Compare commits

...

2 Commits

Author SHA1 Message Date
Ayrton Chilibeck 38d03f9d55
Added logging
ci/woodpecker/push/workflow Pipeline was successful Details
ci/woodpecker/pr/workflow Pipeline was successful Details
2024-01-15 01:59:37 -07:00
Ayrton Chilibeck f86106c9fa
Added logger macro 2024-01-15 01:20:50 -07:00
2 changed files with 49 additions and 15 deletions

View File

@ -1,13 +1,22 @@
fn l1_log(verbosity: u8, message: &str) { // prints output to the stderr if the verbosity is
if verbosity > 0 { // greater than or equal to the logging level of the
println!("{}", message); // notification.
#[macro_export]
macro_rules! log {
// NOTE: this uses a variadic argument system
// copied from the std::fmt eprintln! macro
// see the appropriate documentation
($l:expr, $v:expr, $($arg:tt)*) => {{
if $v >= $l {
eprintln!($($arg)*);
} }
}};
} }
fn l2_log(verbosity: u8, message: &str) { #[cfg(test)]
if verbosity > 1 { mod tests {
println!("{}", message); #[test]
fn test_name() {
log!(1, 1, "this is a {}", "test");
} }
} }
//TODO unit test, but these are trivial

View File

@ -1,19 +1,21 @@
use std::{path::{PathBuf, Path, Components, Component}, fs, collections::{HashMap, VecDeque}, borrow::BorrowMut}; use std::{path::{PathBuf, Path, Components, Component}, fs, collections::{HashMap, VecDeque}, borrow::BorrowMut};
use crate::util; use crate::util;
use crate::log;
pub fn validate(verbosity: u8, config: util::config::Config) { pub fn validate(verbosity: u8, config: util::config::Config) {
println!("Validating"); println!("Validating");
let in_suffix = "in"; //TODO make this configurable? let in_suffix = "in"; //TODO make this configurable?
let out_suffix = "out"; 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 // 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<PathBuf> = get_dir_files(&config.input_path, verbosity).expect("Failed to read the input directory contents"); let in_files: Vec<PathBuf> = get_dir_files(&config.input_path, verbosity).expect("Failed to read the input directory contents");
let out_files: Vec<PathBuf> = get_dir_files(&config.output_path, verbosity).expect("Failed to read the output directory contents"); let out_files: Vec<PathBuf> = 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 // check the files for valid suffixes
log!(1, verbosity, "LOG 1:Collecting files with invalid suffixes");
let invalid_suffix_in: Vec<PathBuf> = collect_invalid(&in_files, in_suffix, verbosity); let invalid_suffix_in: Vec<PathBuf> = collect_invalid(&in_files, in_suffix, verbosity);
let invalid_suffix_out: Vec<PathBuf> = collect_invalid(&out_files, out_suffix, verbosity); let invalid_suffix_out: Vec<PathBuf> = 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 // 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<PathBuf>) = map_files(in_files, out_files, &config, verbosity); let in_to_out: (Vec<(PathBuf, PathBuf)>, Vec<PathBuf>) = 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); let invalid: Vec<(PathBuf, PathBuf)> = check_mappings(in_to_out.0, in_suffix, out_suffix, verbosity);
print_unmached(in_to_out.1, verbosity); print_unmached(in_to_out.1, verbosity);
@ -52,11 +56,13 @@ fn map_files(in_files: Vec<PathBuf>, out_files: Vec<PathBuf>, config: &util::con
for pre in config.input_path.components() { for pre in config.input_path.components() {
in_prefix.push(pre); in_prefix.push(pre);
} }
log!(2, verbosity, "LOG 2: Input prefixes found: {:?}", in_prefix);
let mut out_prefix = vec![]; let mut out_prefix = vec![];
for pre in config.output_path.components() { for pre in config.output_path.components() {
out_prefix.push(pre); 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 // check if the files have the same paths relative to in/out directory
for ifile in &in_files { for ifile in &in_files {
@ -81,16 +87,21 @@ fn map_files(in_files: Vec<PathBuf>, out_files: Vec<PathBuf>, config: &util::con
// println!("{:?}", ofile); // println!("{:?}", ofile);
// println!("{:?}", out_comp); // 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 // check if the file names are the same
if ofile.file_stem() == ifile.file_stem() { if ofile.file_stem() == ifile.file_stem() {
// check that their paths are the same relative to the input/output directory // 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 &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(); } } 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 the files match, then we add them to the mapping
if out_comp == in_comp { 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())); res.0.push((ifile.clone(), ofile.clone()));
found = true; found = true;
} }
@ -98,7 +109,9 @@ fn map_files(in_files: Vec<PathBuf>, out_files: Vec<PathBuf>, config: &util::con
} }
// if we fail to find a match, we add the file to the rest // 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<PathBuf> = Vec::from(out_files.clone()); let mut cl: Vec<PathBuf> = Vec::from(out_files.clone());
@ -108,6 +121,7 @@ fn map_files(in_files: Vec<PathBuf>, out_files: Vec<PathBuf>, config: &util::con
} }
cl.retain(|x| !r.contains(&x)); 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); res.1.append(&mut cl);
// println!("{:?}", res); // println!("{:?}", res);
return 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![]; let mut res: Vec<(PathBuf, PathBuf)> = vec![];
for tup in &mappings { 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) { if !(tup.0.as_path().extension().unwrap() == in_suffix) || !(tup.1.as_path().extension().unwrap() == out_suffix) {
res.push(tup.clone()); res.push(tup.clone());
log!(2, verbosity, "LOG 2: Added {:?} to the list of invalid mappings", tup);
} }
} }
// println!("{:?}", res); // println!("{:?}", res);
@ -146,12 +162,19 @@ fn collect_invalid(files: &Vec<PathBuf>, suf: &str, verbosity: u8) -> Vec<PathBu
let mut res: Vec<PathBuf> = vec![]; let mut res: Vec<PathBuf> = vec![];
for file in files { 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"); let extension = file.extension().expect("Invalid file name for validation");
if extension.to_str() != Some(suf) { if extension.to_str() != Some(suf) {
res.push(file.clone()); 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 return res
} }
/** /**
@ -164,6 +187,7 @@ fn collect_invalid(files: &Vec<PathBuf>, suf: &str, verbosity: u8) -> Vec<PathBu
fn get_dir_files(path: &PathBuf, verbosity: u8) -> Result<Vec<PathBuf>, &'static str> { fn get_dir_files(path: &PathBuf, verbosity: u8) -> Result<Vec<PathBuf>, &'static str> {
let mut res: Vec<PathBuf> = vec![]; let mut res: Vec<PathBuf> = 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 // get the readout of the path for error handling
let entries = match fs::read_dir(path) { let entries = match fs::read_dir(path) {
Ok(x) => x, Ok(x) => x,
@ -175,6 +199,7 @@ fn get_dir_files(path: &PathBuf, verbosity: u8) -> Result<Vec<PathBuf>, &'static
if p.path().is_dir() { if p.path().is_dir() {
(); ();
} else { } else {
log!(2, verbosity, "LOG 2: Found file {}", stringify!(p));
res.push(p.path()); res.push(p.path());
} }
} }