Skip to content

Commit

Permalink
redirect before parsing CLI args; truncate only if --logs wasn't supp…
Browse files Browse the repository at this point in the history
…lied
  • Loading branch information
GyulyVGC committed Dec 7, 2024
1 parent 1e047fc commit 44ccf52
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
39 changes: 26 additions & 13 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
#[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<Message> {
#[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<Message> {
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 {
Expand Down
12 changes: 3 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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(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();
Expand Down
22 changes: 11 additions & 11 deletions src/utils/formatted_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,19 @@ pub fn get_logs_file_path() -> Option<String> {
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<std::fs::File>, gag::Redirect<std::fs::File>)> {
// 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
}
Expand Down

0 comments on commit 44ccf52

Please sign in to comment.