diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 7b02f308e..ec5aa361e 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -13,7 +13,7 @@ use iced::{window, Task}; version = APP_VERSION, about = "Application to comfortably monitor your network traffic" )] -struct Args { +pub 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, @@ -26,9 +26,7 @@ struct Args { restore_default: bool, } -pub fn parse_cli_args() -> Task { - let args = Args::parse(); - +pub fn handle_cli_args(args: Args) -> Task { #[cfg(windows)] if args.logs { std::process::Command::new("explorer") diff --git a/src/main.rs b/src/main.rs index bcf6ac10a..cd0a4525c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,13 +6,15 @@ 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::parse_cli_args; +use cli::handle_cli_args; use configs::types::config_device::ConfigDevice; use configs::types::config_settings::ConfigSettings; use gui::pages::types::running_page::RunningPage; @@ -55,19 +57,22 @@ 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() { + if let Some((gag1, gag2)) = utils::formatted_strings::redirect_stdout_stderr_to_file(args.logs) + { _gag1 = gag1; _gag2 = gag2; } - let configs = CONFIGS.clone(); - - let boot_task_chain = parse_cli_args(); + let boot_task_chain = handle_cli_args(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 0b9cff06b..f5c68c52a 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -176,12 +176,16 @@ pub fn get_logs_file_path() -> Option { #[allow(dead_code)] #[cfg(windows)] pub fn redirect_stdout_stderr_to_file( + logs: bool, ) -> Option<(gag::Redirect, gag::Redirect)> { - 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()?, - )); + // 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()?, + )); + } } None }