From 76f3599d24f3375bdb16988177f2d9dc059a3ff5 Mon Sep 17 00:00:00 2001 From: Blizzard Finnegan Date: Fri, 23 Jun 2023 11:12:48 -0400 Subject: [PATCH] Improve logging verbosity the -i flag now overrides debug cycle count --- src/device.rs | 22 +++++++++------------- src/main.rs | 36 +++++++++++++++++++----------------- src/tty.rs | 20 ++------------------ 3 files changed, 30 insertions(+), 48 deletions(-) diff --git a/src/device.rs b/src/device.rs index c016f88..b453e96 100755 --- a/src/device.rs +++ b/src/device.rs @@ -40,7 +40,7 @@ impl Device{ if ! Path::new(&OUTPUT_FOLDER).is_dir(){ _ = fs::create_dir(&OUTPUT_FOLDER); }; - log::debug!("{:?}",&self.serial); + //log::debug!("{:?}",&self.serial); let output_path:String = OUTPUT_FOLDER.to_owned() + &self.serial + ".txt"; if ! Path::new(&output_path).exists(){ log::debug!("Creating file {}",&output_path); @@ -63,28 +63,28 @@ impl Device{ log::trace!("{:?}",file_contents); for line in file_lines { if line.len() > 0{ - log::trace!("{:?}",line); + //log::trace!("{:?}",line); let section_and_data:Vec<&str> = line.split(SECTION_SEPARATOR).collect(); let section:&str = section_and_data[0]; let possible_value:Result = section_and_data[1].trim().parse::(); match possible_value{ Ok(value) => { - log::trace!("{:?} value: [{:?}]",section,value); + //log::trace!("{:?} value: [{:?}]",section,value); match section { REBOOTS_SECTION => { self.reboots = value; - log::trace!("Reboots set to {:?}",self.reboots); + //log::trace!("Reboots set to {:?}",self.reboots); }, BP_SECTION => { self.bps = value.clone(); - log::trace!("BPS set to {:?}",self.bps); + //log::trace!("BPS set to {:?}",self.bps); }, TEMP_SECTION => { self.temp_offset = value; - log::trace!("Temp offset set to {:?}",self.temp_offset); + //log::trace!("Temp offset set to {:?}",self.temp_offset); }, _ => { - log::info!("Invalid import value: [{:?}]. Please ensure that the output directory is clean.",section_and_data); + log::warn!("Invalid import value: [{:?}]. Please ensure that the output directory is clean.",section_and_data); } }; } @@ -324,9 +324,7 @@ impl Device{ return false } } - log::trace!("Writing to file: {:?}",self.output_file); if let Some(ref mut file_name) = self.output_file{ - log::debug!("Writing to file!"); let mut output_data = REBOOTS_SECTION.to_string(); output_data.push_str(SECTION_SEPARATOR); output_data.push_str(&self.reboots.to_string()); @@ -337,12 +335,10 @@ impl Device{ output_data.push_str("\n"); output_data.push_str(TEMP_SECTION); output_data.push_str(SECTION_SEPARATOR); - log::trace!("Current temps: [{}]",self.temps); - log::trace!("Initial temps: [{}]",self.init_temps); let saved_temps = (self.temps - self.init_temps) + self.temp_offset; output_data.push_str(&saved_temps.to_string()); output_data.push_str("\n"); - log::trace!("final data to write: [{:?}]",output_data); + log::trace!("final data to write to '{:?}': [{:?}]",file_name,output_data); let temp = file_name.write_all(output_data.as_bytes()); match temp{ Err(error) => { @@ -582,7 +578,7 @@ impl Device{ log::trace!("Has bp ended on device {}? : {:?}",self.serial,bp_end); if bp_start != bp_end { self.bps +=1; - log::debug!("Increasing bp count for device {} to {}",self.serial,self.bps); + log::trace!("Increasing bp count for device {} to {}",self.serial,self.bps); self.save_values(); } } diff --git a/src/main.rs b/src/main.rs index 135f258..c307afc 100755 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use clap::Parser; #[derive(Parser,Debug)] #[command(author,version,about)] struct Args{ - /// Print all logs to screen. Sets iteration count to 50000 + /// Print all logs to screen, improves log verbosity. Sets iteration count to 50000 #[arg(short,long,action)] debug:bool, @@ -19,7 +19,7 @@ struct Args{ #[arg(short,long,action)] manual:bool, - /// Set iteration count from command line + /// Set iteration count from command line. Overrides debug iteration count. #[arg(short,long)] iterations:Option @@ -63,17 +63,15 @@ fn main(){ let args = Args::parse(); setup_logs(&args.debug); log::info!("Seymour Life Testing version: {}",VERSION); - if args.debug{ - log::debug!("Debug enabled!"); - } + log::trace!("Debug enabled!"); loop{ let mut iteration_count:u64 = 0; - if args.debug { - iteration_count = DEBUG_ITERATION_COUNT; - } - else if let Some(value) = args.iterations{ + if let Some(value) = args.iterations{ iteration_count = value; } + else if args.debug { + iteration_count = DEBUG_ITERATION_COUNT; + } else { while iteration_count < 1{ iteration_count = int_input_filtering(Some("Enter the number of iterations to complete: ")); @@ -219,14 +217,18 @@ pub fn setup_logs(debug:&bool){ message )) }) - .chain( - fern::Dispatch::new() - .level(log::LevelFilter::Trace) - .chain(fern::log_file( - format!("logs/{0}.log", - chrono_now.format("%Y-%m-%d_%H.%M").to_string() - )).unwrap()), - ) + .chain({ + let mut file_logger = fern::Dispatch::new(); + let date_format = chrono_now.format("%Y-%m-%d_%H.%M").to_string(); + let local_log_file = fern::log_file(format!("logs/{}.log",date_format)).unwrap(); + if *debug{ + file_logger = file_logger.level(log::LevelFilter::Trace); + } + else { + file_logger = file_logger.level(log::LevelFilter::Debug); + } + file_logger.chain(local_log_file) + }) .chain({ let mut stdout_logger = fern::Dispatch::new(); if *debug { diff --git a/src/tty.rs b/src/tty.rs index 42a680c..4e298f6 100755 --- a/src/tty.rs +++ b/src/tty.rs @@ -87,8 +87,7 @@ const RESPONSES:[(&str,Response);13] = [ ]; pub struct TTY{ - tty: Box, - failed_read_count: u8 + tty: Box } impl std::fmt::Debug for TTY{ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result{ @@ -114,10 +113,7 @@ impl TTY{ pub fn new(serial_location:&str) -> Option{ let possible_tty = serialport::new(serial_location,BAUD_RATE).timeout(SERIAL_READ_TIMEOUT).open(); if let Ok(tty) = possible_tty{ - Some(TTY { - tty, - failed_read_count: 0 - }) + Some(TTY{tty,}) } else{ None } @@ -150,7 +146,6 @@ impl TTY{ else{ log::trace!("Successful read of {:?} from tty {}, which matches pattern {:?}",read_line,self.tty.name().unwrap_or("unknown shell".to_string()),enum_value); }; - self.failed_read_count = 0; if enum_value == Response::TempCount(None){ let mut lines = read_line.lines(); while let Some(single_line) = lines.next(){ @@ -165,7 +160,6 @@ impl TTY{ return Response::TempCount(None) }, Ok(parsed_temp_count) => { - //log::trace!("Header: {}",header); log::trace!("parsed temp count for device {}: {}",self.tty.name().unwrap_or("unknown shell".to_string()),temp_count); return Response::TempCount(Some(parsed_temp_count)) } @@ -193,16 +187,6 @@ impl TTY{ } else { log::debug!("Read an empty string from device {:?}. Possible read error.", self); - //Due to a linux kernel power-saving setting that is overly complicated to fix, - //Serial connections will drop for a moment before re-opening, at seemingly-random - //intervals. The below is an attempt to catch and recover from this behaviour. - self.failed_read_count += 1; - if self.failed_read_count >= 15{ - self.failed_read_count = 0; - let tty_location = self.tty.name().expect("Unable to read tty name!"); - self.tty = serialport::new(tty_location,BAUD_RATE).timeout(SERIAL_READ_TIMEOUT).open().expect("Unable to open serial connection!"); - return self.read_from_device(_break_char); - } return Response::Empty; }; }