Skip to content

Commit

Permalink
only truncate the log file if the --logs argument isn't passed
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 5, 2024
1 parent d24dc9a commit 668ea3d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
6 changes: 2 additions & 4 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand All @@ -26,9 +26,7 @@ struct Args {
restore_default: bool,
}

pub fn parse_cli_args() -> Task<Message> {
let args = Args::parse();

pub fn handle_cli_args(args: Args) -> Task<Message> {
#[cfg(windows)]
if args.logs {
std::process::Command::new("explorer")
Expand Down
15 changes: 10 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<std::fs::File>;
#[cfg(all(windows, not(debug_assertions)))]
let _gag2: gag::Redirect<std::fs::File>;
#[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();
Expand Down
14 changes: 9 additions & 5 deletions src/utils/formatted_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,16 @@ pub fn get_logs_file_path() -> Option<String> {
#[allow(dead_code)]
#[cfg(windows)]
pub fn redirect_stdout_stderr_to_file(
logs: bool,
) -> Option<(gag::Redirect<std::fs::File>, gag::Redirect<std::fs::File>)> {
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
}
Expand Down

0 comments on commit 668ea3d

Please sign in to comment.