Improve logging verbosity
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

the -i flag now overrides debug cycle count
This commit is contained in:
Blizzard Finnegan 2023-06-23 11:12:48 -04:00
parent c7b99764e1
commit 76f3599d24
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
3 changed files with 30 additions and 48 deletions

View file

@ -40,7 +40,7 @@ impl Device{
if ! Path::new(&OUTPUT_FOLDER).is_dir(){ if ! Path::new(&OUTPUT_FOLDER).is_dir(){
_ = fs::create_dir(&OUTPUT_FOLDER); _ = fs::create_dir(&OUTPUT_FOLDER);
}; };
log::debug!("{:?}",&self.serial); //log::debug!("{:?}",&self.serial);
let output_path:String = OUTPUT_FOLDER.to_owned() + &self.serial + ".txt"; let output_path:String = OUTPUT_FOLDER.to_owned() + &self.serial + ".txt";
if ! Path::new(&output_path).exists(){ if ! Path::new(&output_path).exists(){
log::debug!("Creating file {}",&output_path); log::debug!("Creating file {}",&output_path);
@ -63,28 +63,28 @@ impl Device{
log::trace!("{:?}",file_contents); log::trace!("{:?}",file_contents);
for line in file_lines { for line in file_lines {
if line.len() > 0{ if line.len() > 0{
log::trace!("{:?}",line); //log::trace!("{:?}",line);
let section_and_data:Vec<&str> = line.split(SECTION_SEPARATOR).collect(); let section_and_data:Vec<&str> = line.split(SECTION_SEPARATOR).collect();
let section:&str = section_and_data[0]; let section:&str = section_and_data[0];
let possible_value:Result<u64, std::num::ParseIntError> = section_and_data[1].trim().parse::<u64>(); let possible_value:Result<u64, std::num::ParseIntError> = section_and_data[1].trim().parse::<u64>();
match possible_value{ match possible_value{
Ok(value) => { Ok(value) => {
log::trace!("{:?} value: [{:?}]",section,value); //log::trace!("{:?} value: [{:?}]",section,value);
match section { match section {
REBOOTS_SECTION => { REBOOTS_SECTION => {
self.reboots = value; self.reboots = value;
log::trace!("Reboots set to {:?}",self.reboots); //log::trace!("Reboots set to {:?}",self.reboots);
}, },
BP_SECTION => { BP_SECTION => {
self.bps = value.clone(); self.bps = value.clone();
log::trace!("BPS set to {:?}",self.bps); //log::trace!("BPS set to {:?}",self.bps);
}, },
TEMP_SECTION => { TEMP_SECTION => {
self.temp_offset = value; 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 return false
} }
} }
log::trace!("Writing to file: {:?}",self.output_file);
if let Some(ref mut file_name) = 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(); let mut output_data = REBOOTS_SECTION.to_string();
output_data.push_str(SECTION_SEPARATOR); output_data.push_str(SECTION_SEPARATOR);
output_data.push_str(&self.reboots.to_string()); output_data.push_str(&self.reboots.to_string());
@ -337,12 +335,10 @@ impl Device{
output_data.push_str("\n"); output_data.push_str("\n");
output_data.push_str(TEMP_SECTION); output_data.push_str(TEMP_SECTION);
output_data.push_str(SECTION_SEPARATOR); 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; let saved_temps = (self.temps - self.init_temps) + self.temp_offset;
output_data.push_str(&saved_temps.to_string()); output_data.push_str(&saved_temps.to_string());
output_data.push_str("\n"); 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()); let temp = file_name.write_all(output_data.as_bytes());
match temp{ match temp{
Err(error) => { Err(error) => {
@ -582,7 +578,7 @@ impl Device{
log::trace!("Has bp ended on device {}? : {:?}",self.serial,bp_end); log::trace!("Has bp ended on device {}? : {:?}",self.serial,bp_end);
if bp_start != bp_end { if bp_start != bp_end {
self.bps +=1; 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(); self.save_values();
} }
} }

View file

@ -11,7 +11,7 @@ use clap::Parser;
#[derive(Parser,Debug)] #[derive(Parser,Debug)]
#[command(author,version,about)] #[command(author,version,about)]
struct Args{ 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)] #[arg(short,long,action)]
debug:bool, debug:bool,
@ -19,7 +19,7 @@ struct Args{
#[arg(short,long,action)] #[arg(short,long,action)]
manual:bool, manual:bool,
/// Set iteration count from command line /// Set iteration count from command line. Overrides debug iteration count.
#[arg(short,long)] #[arg(short,long)]
iterations:Option<u64> iterations:Option<u64>
@ -63,17 +63,15 @@ fn main(){
let args = Args::parse(); let args = Args::parse();
setup_logs(&args.debug); setup_logs(&args.debug);
log::info!("Seymour Life Testing version: {}",VERSION); log::info!("Seymour Life Testing version: {}",VERSION);
if args.debug{ log::trace!("Debug enabled!");
log::debug!("Debug enabled!");
}
loop{ loop{
let mut iteration_count:u64 = 0; let mut iteration_count:u64 = 0;
if args.debug { if let Some(value) = args.iterations{
iteration_count = DEBUG_ITERATION_COUNT;
}
else if let Some(value) = args.iterations{
iteration_count = value; iteration_count = value;
} }
else if args.debug {
iteration_count = DEBUG_ITERATION_COUNT;
}
else { else {
while iteration_count < 1{ while iteration_count < 1{
iteration_count = int_input_filtering(Some("Enter the number of iterations to complete: ")); iteration_count = int_input_filtering(Some("Enter the number of iterations to complete: "));
@ -219,14 +217,18 @@ pub fn setup_logs(debug:&bool){
message message
)) ))
}) })
.chain( .chain({
fern::Dispatch::new() let mut file_logger = fern::Dispatch::new();
.level(log::LevelFilter::Trace) let date_format = chrono_now.format("%Y-%m-%d_%H.%M").to_string();
.chain(fern::log_file( let local_log_file = fern::log_file(format!("logs/{}.log",date_format)).unwrap();
format!("logs/{0}.log", if *debug{
chrono_now.format("%Y-%m-%d_%H.%M").to_string() file_logger = file_logger.level(log::LevelFilter::Trace);
)).unwrap()), }
) else {
file_logger = file_logger.level(log::LevelFilter::Debug);
}
file_logger.chain(local_log_file)
})
.chain({ .chain({
let mut stdout_logger = fern::Dispatch::new(); let mut stdout_logger = fern::Dispatch::new();
if *debug { if *debug {

View file

@ -87,8 +87,7 @@ const RESPONSES:[(&str,Response);13] = [
]; ];
pub struct TTY{ pub struct TTY{
tty: Box<dyn SerialPort>, tty: Box<dyn SerialPort>
failed_read_count: u8
} }
impl std::fmt::Debug for TTY{ impl std::fmt::Debug for TTY{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result{ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result{
@ -114,10 +113,7 @@ impl TTY{
pub fn new(serial_location:&str) -> Option<Self>{ pub fn new(serial_location:&str) -> Option<Self>{
let possible_tty = serialport::new(serial_location,BAUD_RATE).timeout(SERIAL_READ_TIMEOUT).open(); let possible_tty = serialport::new(serial_location,BAUD_RATE).timeout(SERIAL_READ_TIMEOUT).open();
if let Ok(tty) = possible_tty{ if let Ok(tty) = possible_tty{
Some(TTY { Some(TTY{tty,})
tty,
failed_read_count: 0
})
} else{ } else{
None None
} }
@ -150,7 +146,6 @@ impl TTY{
else{ else{
log::trace!("Successful read of {:?} from tty {}, which matches pattern {:?}",read_line,self.tty.name().unwrap_or("unknown shell".to_string()),enum_value); 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){ if enum_value == Response::TempCount(None){
let mut lines = read_line.lines(); let mut lines = read_line.lines();
while let Some(single_line) = lines.next(){ while let Some(single_line) = lines.next(){
@ -165,7 +160,6 @@ impl TTY{
return Response::TempCount(None) return Response::TempCount(None)
}, },
Ok(parsed_temp_count) => { 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); 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)) return Response::TempCount(Some(parsed_temp_count))
} }
@ -193,16 +187,6 @@ impl TTY{
} }
else { else {
log::debug!("Read an empty string from device {:?}. Possible read error.", self); 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; return Response::Empty;
}; };
} }