From 44ccf528543f81a833f3d2f75b006f7c5289deb0 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sat, 7 Dec 2024 10:58:25 +0100 Subject: [PATCH] redirect before parsing CLI args; truncate only if --logs wasn't supplied --- src/cli/mod.rs | 39 ++++++++++++++++++++++------------ src/main.rs | 12 +++-------- src/utils/formatted_strings.rs | 22 +++++++++---------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index a898322e..7dfe9e0c 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -13,29 +13,42 @@ use iced::{window, Task}; version = APP_VERSION, about = "Application to comfortably monitor your network traffic" )] -pub struct Args { +struct Args { /// Start sniffing packets from the supplied network adapter #[arg(short, long, value_name = "NAME", default_missing_value = CONFIGS.device.device_name.as_str(), num_args = 0..=1)] adapter: Option, - #[cfg(windows)] + #[cfg(all(windows, not(debug_assertions)))] /// Show the logs (stdout and stderr) of the most recent application run #[arg(short, long, exclusive = true)] - pub logs: bool, + logs: bool, /// Restore default settings #[arg(short, long, exclusive = true)] restore_default: bool, } -pub fn handle_cli_args(args: Args) -> Task { - #[cfg(windows)] - if args.logs { - std::process::Command::new("explorer") - .arg(crate::utils::formatted_strings::get_logs_file_path().unwrap()) - .spawn() - .unwrap() - .wait() - .unwrap_or_default(); - std::process::exit(0); +pub fn handle_cli_args() -> Task { + let args = Args::parse(); + + #[cfg(all(windows, not(debug_assertions)))] + if let Some(logs_file) = crate::utils::formatted_strings::get_logs_file_path() { + if args.logs { + std::process::Command::new("explorer") + .arg(logs_file) + .spawn() + .unwrap() + .wait() + .unwrap_or_default(); + std::process::exit(0); + } else { + // truncate logs file + unsafe { + std::fs::OpenOptions::new() + .write(true) + .truncate(true) + .open(logs_file) + .unwrap_unchecked(); + } + } } if args.restore_default { diff --git a/src/main.rs b/src/main.rs index cd0a4525..4171dd12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,10 @@ use std::borrow::Cow; use std::sync::{Arc, Mutex}; use std::{panic, process, thread}; -use clap::Parser; #[cfg(target_os = "linux")] use iced::window::settings::PlatformSpecific; use iced::{application, window, Font, Pixels, Settings}; -use crate::cli::Args; use chart::types::chart_type::ChartType; use chart::types::traffic_chart::TrafficChart; use cli::handle_cli_args; @@ -57,22 +55,18 @@ pub const SNIFFNET_TITLECASE: &str = "Sniffnet"; /// /// It initializes shared variables and loads configuration parameters pub fn main() -> iced::Result { - let configs = CONFIGS.clone(); - - let args = Args::parse(); - #[cfg(all(windows, not(debug_assertions)))] let _gag1: gag::Redirect; #[cfg(all(windows, not(debug_assertions)))] let _gag2: gag::Redirect; #[cfg(all(windows, not(debug_assertions)))] - if let Some((gag1, gag2)) = utils::formatted_strings::redirect_stdout_stderr_to_file(args.logs) - { + if let Some((gag1, gag2)) = utils::formatted_strings::redirect_stdout_stderr_to_file() { _gag1 = gag1; _gag2 = gag2; } - let boot_task_chain = handle_cli_args(args); + let configs = CONFIGS.clone(); + let boot_task_chain = handle_cli_args(); let configs1 = Arc::new(Mutex::new(configs)); let configs2 = configs1.clone(); diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index f5c68c52..3af31d96 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -173,19 +173,19 @@ pub fn get_logs_file_path() -> Option { Some(conf.to_str()?.to_string()) } -#[allow(dead_code)] -#[cfg(windows)] +#[cfg(all(windows, not(debug_assertions)))] pub fn redirect_stdout_stderr_to_file( - logs: bool, ) -> Option<(gag::Redirect, gag::Redirect)> { - // only truncate the log file if the --logs argument isn't passed - if !logs { - if let Ok(logs_file) = std::fs::File::create(get_logs_file_path()?) { - return Some(( - gag::Redirect::stdout(logs_file.try_clone().ok()?).ok()?, - gag::Redirect::stderr(logs_file).ok()?, - )); - } + if let Ok(logs_file) = std::fs::OpenOptions::new() + .write(true) + .create(true) + .append(true) + .open(get_logs_file_path()?) + { + return Some(( + gag::Redirect::stdout(logs_file.try_clone().ok()?).ok()?, + gag::Redirect::stderr(logs_file).ok()?, + )); } None }