Skip to content

Commit

Permalink
Merge pull request #645 from GyulyVGC/windows-debug
Browse files Browse the repository at this point in the history
Redirect `stderr` and `stdout` to file on Windows releases
  • Loading branch information
GyulyVGC authored Dec 7, 2024
2 parents 9aaf6be + 711796a commit 7faf769
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ jobs:
if: matrix.os == 'ubuntu'
run: cargo install cross --git https://github.com/cross-rs/cross

- name: Clippy (release mode)
run: cargo clippy --release -- -D warnings

- name: Build binary
if: matrix.os == 'ubuntu'
run: cross build --release --target ${{ matrix.target }}
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
All Sniffnet releases with the relative changes are documented in this file.

## [UNRELEASED]
- Added Vietnamese translation 🇻🇳 ([#577](https://github.com/GyulyVGC/sniffnet/pull/577))
- Added CLI argument `--adapter [<NAME>]` to allow immediately starting the capture from a given network interface ([#643](https://github.com/GyulyVGC/sniffnet/pull/643) — fixes [#636](https://github.com/GyulyVGC/sniffnet/issues/636))
- Added Vietnamese translation 🇻🇳 ([#577](https://github.com/GyulyVGC/sniffnet/pull/577))
- Redirect `stderr` and `stdout` to file on Windows release builds ([#645](https://github.com/GyulyVGC/sniffnet/pull/645) — fixes [#578](https://github.com/GyulyVGC/sniffnet/issues/578))
- Updated some of the existing translations to v1.3:
- Chinese ([#575](https://github.com/GyulyVGC/sniffnet/pull/575))
- Korean ([#604](https://github.com/GyulyVGC/sniffnet/pull/604))
Expand Down
24 changes: 23 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ phf_shared = "0.11.2"
splines = "4.4.0"
clap = { version = "4.5.23", features = ["derive"] }

[target.'cfg(windows)'.dependencies]
gag = "1.0.0"

[target.'cfg(not(target_arch = "powerpc64"))'.dependencies]
reqwest = { version = "0.12.9", default-features = false, features = ["json", "blocking", "rustls-tls"] }

Expand Down
31 changes: 27 additions & 4 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,43 @@ 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(all(windows, not(debug_assertions)))]
/// Show the logs (stdout and stderr) of the most recent application run
#[arg(short, long, exclusive = true)]
logs: bool,
/// Restore default settings
#[arg(short, long)]
#[arg(short, long, exclusive = true)]
restore_default: bool,
}

pub fn parse_cli_args() -> Task<Message> {
let mut boot_task_chain = window::get_latest().map(Message::WindowId);

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
let _ = std::fs::OpenOptions::new()
.write(true)
.truncate(true)
.open(logs_file);
}
}

if args.restore_default {
Configs::default().store();
std::process::exit(0);
}

let mut boot_task_chain = window::get_latest().map(Message::WindowId);
if let Some(adapter) = args.adapter {
boot_task_chain = boot_task_chain
.chain(Task::done(Message::AdapterSelection(adapter)))
Expand Down
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use iced::{application, window, Font, Pixels, Settings};

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,9 +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();
#[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() {
_gag1 = gag1;
_gag2 = gag2;
}

let boot_task_chain = parse_cli_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
33 changes: 33 additions & 0 deletions src/utils/formatted_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,31 @@ pub fn get_formatted_num_seconds(num_seconds: u128) -> String {
}
}

#[allow(dead_code)]
#[cfg(windows)]
pub fn get_logs_file_path() -> Option<String> {
let mut conf = confy::get_configuration_file_path(crate::SNIFFNET_LOWERCASE, "logs").ok()?;
conf.set_extension("txt");
Some(conf.to_str()?.to_string())
}

#[cfg(all(windows, not(debug_assertions)))]
pub fn redirect_stdout_stderr_to_file(
) -> Option<(gag::Redirect<std::fs::File>, gag::Redirect<std::fs::File>)> {
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
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -200,4 +225,12 @@ mod tests {
"94522879700260684295381835397713392:04:15"
);
}

#[cfg(windows)]
#[test]
fn test_logs_file_path() {
let file_path = std::path::PathBuf::from(get_logs_file_path().unwrap());
assert!(file_path.is_absolute());
assert_eq!(file_path.file_name().unwrap(), "logs.txt");
}
}

0 comments on commit 7faf769

Please sign in to comment.