From b5dbbeaff2569e9838d5273e4afe9de8cf5ffa0c Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 8 Aug 2023 14:08:03 +0200 Subject: [PATCH 1/2] fix status not showing if the first is a failure --- src/networking/monitoring/uptime.rs | 30 +++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/networking/monitoring/uptime.rs b/src/networking/monitoring/uptime.rs index f2097b0..f561530 100644 --- a/src/networking/monitoring/uptime.rs +++ b/src/networking/monitoring/uptime.rs @@ -19,9 +19,7 @@ use std::{fmt, time::Duration}; //// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// -// we want the log macros in any case -#[allow(unused_imports)] -use tracing; +use crate::logger::*; use reqwest; @@ -142,10 +140,10 @@ impl UptimeStatus { return; } let ratio: f32 = (self.reachable as f32) / (self.urls.len() as f32) * 100f32; - tracing::trace!("calculated success_ratio: {}", ratio); + trace!("calculated success_ratio: {}", ratio); self.success_ratio = ratio.floor() as u8; self.success = self.success_ratio >= self.success_ratio_target; - tracing::trace!("calculated success as: {}", self.success) + trace!("calculated success as: {}", self.success) } } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -218,7 +216,7 @@ pub fn continuous_uptime_monitor( timeout: u64, ) { if urls.len() == 0 { - tracing::error!("No URLs provided. There is nothing to monitor."); + error!("No URLs provided. There is nothing to monitor."); return; } @@ -226,11 +224,15 @@ pub fn continuous_uptime_monitor( let mut last_downtime: Option = None; let mut last_uptime: Option = None; let mut status = UptimeStatus::new(success_ratio_target, urls, timeout); - let mut last_was_up: bool = false; + // we assume that the last status was up, so the binary shows the first status if its a + // failure. + let mut last_was_up: bool = true; let mut last_ratio: u8 = status.success_ratio; loop { + trace!(?status, ?last_was_up, "loop iteration for continuous uptime monitor"); if !status.success { if last_was_up { + trace!("displaying status"); display_uptime_status("fail", last_uptime, last_downtime, &status) } last_downtime = Some(SystemTime::now()); @@ -293,19 +295,19 @@ fn display_uptime_status( ) { // I know it's weird that this has two spaces too much, but somehow just the tabs is missing // two spaces. - tracing::info!("uptime check: {}", msg); - tracing::info!("last uptime: {}", match_format_time(last_uptime)); - tracing::info!("last downtime: {}", match_format_time(last_downtime)); - tracing::info!( + info!("uptime check: {}", msg); + info!("last uptime: {}", match_format_time(last_uptime)); + info!("last downtime: {}", match_format_time(last_downtime)); + info!( "since downtime: {}", match_format_duration_since(last_downtime) ); - tracing::info!( + info!( "since uptime: {}", match_format_duration_since(last_uptime) ); - tracing::debug!("\n{}", status); - tracing::info!("{}", divider!()); + debug!("\n{}", status); + info!("{}", divider!()); } //////////////////////////////////////////////////////////////////////////////////////////////////// From 33ec84075a195c5af743281a9456c723029cd847 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 8 Aug 2023 14:08:19 +0200 Subject: [PATCH 2/2] minor cosmetic changes --- src/bin/main/mod.rs | 1 + src/logger/mod.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/bin/main/mod.rs b/src/bin/main/mod.rs index 8aea29b..c9ee8cc 100644 --- a/src/bin/main/mod.rs +++ b/src/bin/main/mod.rs @@ -28,6 +28,7 @@ pub mod args; use args::*; //// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// +#[allow(dead_code)] const EXIT_SUCCESS: i32 = 0; const EXIT_FAILURE_USAGE: i32 = 1; diff --git a/src/logger/mod.rs b/src/logger/mod.rs index 2a99796..9942436 100644 --- a/src/logger/mod.rs +++ b/src/logger/mod.rs @@ -107,8 +107,12 @@ impl Logger { warn!("trying to reinitialize the logger, ignoring"); return Err(Error::Usage(format!("logging is already initialized"))); } else { - let filter = tracing_subscriber::filter::FilterFn::new(|metadata| { - true + let filter = tracing_subscriber::filter::FilterFn::new(|_metadata| { + #[allow(unused_variables)] + #[allow(unused_mut)] + let mut status = true; + + status }); let basic_subscriber = tracing_subscriber::fmt::Subscriber::builder()