Skip to content

Commit 94ada5d

Browse files
authored
Add logging infrastructure (#302)
* Add logging dependencies * Initialise logging
1 parent 5cb09cc commit 94ada5d

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed

Cargo.lock

+82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ resolv-conf = "0.7.0"
3636
pnet_macros_support = "0.34.0"
3737
anyhow = { version = "1.0.75", features = ["backtrace"] }
3838
thiserror = "1.0.49"
39+
log = "0.4.20"
40+
simplelog = "0.12.1"
41+
clap-verbosity-flag = "2.0.1"
42+
derivative = "2.2.0"
3943

4044
[target.'cfg(target_os="windows")'.dependencies]
41-
netstat2 = "0.9.1"
45+
netstat2 = "0.9.1"
4246
sysinfo = "0.29.10"
4347

4448
[target.'cfg(target_os="linux")'.dependencies]

src/cli.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
use std::net::Ipv4Addr;
1+
use std::{net::Ipv4Addr, path::PathBuf};
22

33
use clap::{Args, Parser};
4+
use clap_verbosity_flag::{InfoLevel, Verbosity};
5+
use derivative::Derivative;
46

5-
#[derive(Clone, Debug, Default, Parser)]
7+
#[derive(Clone, Debug, Derivative, Parser)]
8+
#[derivative(Default)]
69
#[command(name = "bandwhich", version)]
710
pub struct Opt {
811
#[arg(short, long)]
@@ -25,6 +28,14 @@ pub struct Opt {
2528
/// A dns server ip to use instead of the system default
2629
pub dns_server: Option<Ipv4Addr>,
2730

31+
#[arg(long)]
32+
/// Enable logging to a file
33+
pub log_to: Option<PathBuf>,
34+
35+
#[command(flatten)]
36+
#[derivative(Default(value = "Verbosity::new(0, 0)"))]
37+
pub verbosity: Verbosity<InfoLevel>,
38+
2839
#[command(flatten)]
2940
pub render_opts: RenderOpts,
3041
}

src/main.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod tests;
99

1010
use std::{
1111
collections::HashMap,
12+
fs::File,
1213
sync::{
1314
atomic::{AtomicBool, AtomicUsize, Ordering},
1415
Arc, Mutex, RwLock,
@@ -29,15 +30,26 @@ use network::{
2930
};
3031
use pnet::datalink::{DataLinkReceiver, NetworkInterface};
3132
use ratatui::backend::{Backend, CrosstermBackend};
33+
use simplelog::WriteLogger;
3234

3335
use crate::cli::Opt;
3436

3537
const DISPLAY_DELTA: Duration = Duration::from_millis(1000);
3638

3739
fn main() -> anyhow::Result<()> {
3840
let opts = Opt::parse();
39-
let os_input = os::get_input(opts.interface.as_deref(), !opts.no_resolve, opts.dns_server)?;
4041

42+
// init logging
43+
if let Some(ref log_path) = opts.log_to {
44+
let log_file = File::options().create_new(true).open(log_path)?;
45+
WriteLogger::init(
46+
opts.verbosity.log_level_filter(),
47+
Default::default(),
48+
log_file,
49+
)?;
50+
}
51+
52+
let os_input = os::get_input(opts.interface.as_deref(), !opts.no_resolve, opts.dns_server)?;
4153
if opts.raw {
4254
let terminal_backend = RawTerminalBackend {};
4355
start(terminal_backend, os_input, opts);

0 commit comments

Comments
 (0)