Don't constantly log total_avg error

This commit is contained in:
elkowar 2023-06-10 20:30:10 +02:00
parent b31e397e97
commit c613275621
No known key found for this signature in database
GPG key ID: 50E76B4711E4C3E4

View file

@ -111,6 +111,8 @@ pub fn get_battery_capacity() -> Result<String> {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub fn get_battery_capacity() -> Result<String> { pub fn get_battery_capacity() -> Result<String> {
use std::sync::atomic::AtomicBool;
let mut current = 0_f64; let mut current = 0_f64;
let mut total = 0_f64; let mut total = 0_f64;
let mut json = String::from('{'); let mut json = String::from('{');
@ -143,14 +145,18 @@ pub fn get_battery_capacity() -> Result<String> {
current += c.trim_end_matches(|c| c == '\n').parse::<f64>()?; current += c.trim_end_matches(|c| c == '\n').parse::<f64>()?;
total += t.trim_end_matches(|c| c == '\n').parse::<f64>()?; total += t.trim_end_matches(|c| c == '\n').parse::<f64>()?;
} else { } else {
static WARNED: AtomicBool = AtomicBool::new(false);
if !WARNED.load(std::sync::atomic::Ordering::Relaxed) {
WARNED.store(true, std::sync::atomic::Ordering::Relaxed);
log::warn!( log::warn!(
"Failed to get/calculate uWh: the total_avg value of the battery magic var will probably be a garbage \ "Failed to get/calculate uWh: the total_avg value of the battery magic var will probably be a \
value that can not be trusted." garbage value that can not be trusted."
); );
} }
} }
} }
} }
}
if total == 0_f64 { if total == 0_f64 {
return Ok(String::from("")); return Ok(String::from(""));
} }