Write all panic messages to logs
This commit is contained in:
parent
139f7485e9
commit
d905b47456
1 changed files with 26 additions and 17 deletions
43
src/main.rs
43
src/main.rs
|
|
@ -13,7 +13,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
|||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{fmt, slice};
|
||||
use std::{fmt, panic, slice};
|
||||
use textplots::{Chart, Plot, Shape};
|
||||
|
||||
const MAX_DATA_LEN: usize = 1024 * 2 + 4 + 2;
|
||||
|
|
@ -238,6 +238,17 @@ fn init_logger(
|
|||
None => root_dispatch,
|
||||
};
|
||||
root_dispatch.apply()?;
|
||||
|
||||
// Set panic hook to log panic messages
|
||||
panic::set_hook(Box::new(|panic_info| {
|
||||
let panic_str = "Hailsens logger stopped with panic";
|
||||
if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
|
||||
error!("{panic_str}: {:?}", s);
|
||||
} else {
|
||||
error!("{panic_str}");
|
||||
}
|
||||
}));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -272,14 +283,17 @@ fn main() {
|
|||
}
|
||||
};
|
||||
port.set_timeout(calculate_timeout(args.baud_rate, 30))
|
||||
.unwrap();
|
||||
.expect("Couldn't set serial port timeout!");
|
||||
let command = format!("@{} name\n", args.device_id);
|
||||
let response = process_command(&mut port, command).unwrap_or_default();
|
||||
debug!("We got some response: {}", response.trim());
|
||||
if response.contains("'HAILSENS'") {
|
||||
info!("Detected hailsens.")
|
||||
} else {
|
||||
panic!("Couldn't detect hailsens!")
|
||||
match process_command(&mut port, command) {
|
||||
Some(response) => {
|
||||
if response.contains("'HAILSENS'") {
|
||||
info!("Detected hailsens.");
|
||||
} else {
|
||||
panic!("Couldn't detect hailsens, incorrect response: {}", response);
|
||||
}
|
||||
}
|
||||
None => panic!("No response from hailsens!"),
|
||||
}
|
||||
|
||||
get_set_param(
|
||||
|
|
@ -490,7 +504,6 @@ fn get_stats(port: &mut Box<dyn SerialPort>, id: u32) -> StatsOptions {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
fn get_set_param(
|
||||
port: &mut Box<dyn SerialPort>,
|
||||
id: u32,
|
||||
|
|
@ -737,12 +750,8 @@ fn sync_datetime(port: &mut Box<dyn SerialPort>, id: u32) {
|
|||
info!("Setting correct date/time on hailsens...");
|
||||
let formatted_date_time = Local::now().format("%Y-%m-%dT%H:%M:%S").to_string();
|
||||
let request_str = format!("@{} date {}\n", id, formatted_date_time);
|
||||
let response = process_command(port, request_str).expect("Couldn't set date/time!");
|
||||
info!(
|
||||
"Hailsens date/time: {}",
|
||||
response
|
||||
.split_whitespace()
|
||||
.nth(1)
|
||||
.expect("Couldn't get date/time!")
|
||||
);
|
||||
match process_command(port, request_str) {
|
||||
Some(response) => info!("Hailsens date/time: {response}"),
|
||||
None => error!("Couldn't set date/time!"),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue