file permissions on unix & better verbose control

This commit is contained in:
Alex Janka 2022-08-09 11:17:16 +10:00
parent ec31317928
commit b50a5e5211

View file

@ -1,4 +1,7 @@
use std::fs::OpenOptions;
use std::net::UdpSocket; use std::net::UdpSocket;
#[cfg(target_family = "unix")]
use std::os::unix::fs::OpenOptionsExt;
use std::path::Path; use std::path::Path;
use std::{fs, net}; use std::{fs, net};
@ -191,11 +194,12 @@ fn main() {
if deserialised.is_race_on == 0 { if deserialised.is_race_on == 0 {
continue 'listener; continue 'listener;
} }
if args.verbose {}
if status.position != deserialised.race_position { if status.position != deserialised.race_position {
status.position = deserialised.race_position; status.position = deserialised.race_position;
println!("now position {}", status.position); if args.verbose {
println!("now position {}", status.position);
}
} }
if status.inrace { if status.inrace {
@ -203,10 +207,12 @@ fn main() {
// coming out of race // coming out of race
status.inrace = false; status.inrace = false;
writer.flush().expect("couldnt flush to file"); writer.flush().expect("couldnt flush to file");
println!( if args.verbose {
"{}: no longer in race", println!(
&Local::now().format("%H:%M:%S").to_string() "{}: no longer in race",
); &Local::now().format("%H:%M:%S").to_string()
);
}
continue 'listener; continue 'listener;
} else { } else {
// still in race // still in race
@ -215,22 +221,30 @@ fn main() {
if deserialised.race_position > 0 { if deserialised.race_position > 0 {
// getting into race // getting into race
status.inrace = true; status.inrace = true;
println!( if args.verbose {
"{}: entering race", println!(
&Local::now().format("%H:%M:%S").to_string() "{}: entering race",
); &Local::now().format("%H:%M:%S").to_string()
println!("car class: {}", deserialised.car_performance_index); );
println!("car class: {}", deserialised.car_performance_index);
}
let mut options = OpenOptions::new().write(true).create_new(true).clone();
// open file with wide open permissions on unix systems
if cfg!(target_family = "unix") {
options.mode(0o777);
}
// open file for this race // open file for this race
// filename format: timestamp _ car class _ car ordinal // filename format: timestamp _ car class _ car ordinal
writer = csv::Writer::from_writer( writer = csv::Writer::from_writer(
fs::File::create(folder_path.join(format!( options
"{}_{}_{}{}", .open(folder_path.join(format!(
&Local::now().format("%Y-%m-%d_%H-%M").to_string(), "{}_{}_{}{}",
deserialised.car_performance_index, &Local::now().format("%Y-%m-%d_%H-%M").to_string(),
deserialised.car_ordinal, deserialised.car_performance_index,
".csv", deserialised.car_ordinal,
))) ".csv",
.expect("couldnt open file"), )))
.expect("couldnt open file"),
); );
} else { } else {
// still not in race // still not in race