Add logging infrastructure (#302)

* Add logging dependencies

* Initialise logging
This commit is contained in:
cyqsimon
2023-10-11 18:16:56 +08:00
committed by GitHub
parent 5cb09cca19
commit 94ada5db8a
4 changed files with 113 additions and 4 deletions

82
Cargo.lock generated
View File

@@ -149,10 +149,13 @@ dependencies = [
"async-trait", "async-trait",
"chrono", "chrono",
"clap", "clap",
"clap-verbosity-flag",
"crossterm", "crossterm",
"derivative",
"http_req", "http_req",
"insta", "insta",
"ipnetwork", "ipnetwork",
"log",
"netstat2", "netstat2",
"packet-builder", "packet-builder",
"pnet", "pnet",
@@ -163,6 +166,7 @@ dependencies = [
"regex", "regex",
"resolv-conf", "resolv-conf",
"rstest", "rstest",
"simplelog",
"sysinfo", "sysinfo",
"thiserror", "thiserror",
"tokio", "tokio",
@@ -293,6 +297,16 @@ dependencies = [
"clap_derive", "clap_derive",
] ]
[[package]]
name = "clap-verbosity-flag"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1eef05769009513df2eb1c3b4613e7fad873a14c600ff025b08f250f59fee7de"
dependencies = [
"clap",
"log",
]
[[package]] [[package]]
name = "clap_builder" name = "clap_builder"
version = "4.4.6" version = "4.4.6"
@@ -461,6 +475,17 @@ version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2696e8a945f658fd14dc3b87242e6b80cd0f36ff04ea560fa39082368847946" checksum = "f2696e8a945f658fd14dc3b87242e6b80cd0f36ff04ea560fa39082368847946"
[[package]]
name = "derivative"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "derive-new" name = "derive-new"
version = "0.5.9" version = "0.5.9"
@@ -852,6 +877,12 @@ dependencies = [
"either", "either",
] ]
[[package]]
name = "itoa"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]] [[package]]
name = "jobserver" name = "jobserver"
version = "0.1.26" version = "0.1.26"
@@ -1044,6 +1075,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "num_threads"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "object" name = "object"
version = "0.32.1" version = "0.32.1"
@@ -1646,6 +1686,17 @@ version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "420acb44afdae038210c99e69aae24109f32f15500aa708e81d46c9f29d55fcf" checksum = "420acb44afdae038210c99e69aae24109f32f15500aa708e81d46c9f29d55fcf"
[[package]]
name = "simplelog"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acee08041c5de3d5048c8b3f6f13fafb3026b24ba43c6a695a0c76179b844369"
dependencies = [
"log",
"termcolor",
"time",
]
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.9" version = "0.4.9"
@@ -1755,6 +1806,15 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.49" version = "1.0.49"
@@ -1782,8 +1842,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "426f806f4089c493dcac0d24c29c01e2c38baf8e30f1b716ee37e83d200b18fe" checksum = "426f806f4089c493dcac0d24c29c01e2c38baf8e30f1b716ee37e83d200b18fe"
dependencies = [ dependencies = [
"deranged", "deranged",
"itoa",
"libc",
"num_threads",
"serde", "serde",
"time-core", "time-core",
"time-macros",
] ]
[[package]] [[package]]
@@ -1792,6 +1856,15 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
[[package]]
name = "time-macros"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ad70d68dba9e1f8aceda7aa6711965dfec1cac869f311a51bd08b3a2ccbce20"
dependencies = [
"time-core",
]
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"
@@ -2060,6 +2133,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

View File

@@ -36,9 +36,13 @@ resolv-conf = "0.7.0"
pnet_macros_support = "0.34.0" pnet_macros_support = "0.34.0"
anyhow = { version = "1.0.75", features = ["backtrace"] } anyhow = { version = "1.0.75", features = ["backtrace"] }
thiserror = "1.0.49" thiserror = "1.0.49"
log = "0.4.20"
simplelog = "0.12.1"
clap-verbosity-flag = "2.0.1"
derivative = "2.2.0"
[target.'cfg(target_os="windows")'.dependencies] [target.'cfg(target_os="windows")'.dependencies]
netstat2 = "0.9.1" netstat2 = "0.9.1"
sysinfo = "0.29.10" sysinfo = "0.29.10"
[target.'cfg(target_os="linux")'.dependencies] [target.'cfg(target_os="linux")'.dependencies]

View File

@@ -1,8 +1,11 @@
use std::net::Ipv4Addr; use std::{net::Ipv4Addr, path::PathBuf};
use clap::{Args, Parser}; use clap::{Args, Parser};
use clap_verbosity_flag::{InfoLevel, Verbosity};
use derivative::Derivative;
#[derive(Clone, Debug, Default, Parser)] #[derive(Clone, Debug, Derivative, Parser)]
#[derivative(Default)]
#[command(name = "bandwhich", version)] #[command(name = "bandwhich", version)]
pub struct Opt { pub struct Opt {
#[arg(short, long)] #[arg(short, long)]
@@ -25,6 +28,14 @@ pub struct Opt {
/// A dns server ip to use instead of the system default /// A dns server ip to use instead of the system default
pub dns_server: Option<Ipv4Addr>, pub dns_server: Option<Ipv4Addr>,
#[arg(long)]
/// Enable logging to a file
pub log_to: Option<PathBuf>,
#[command(flatten)]
#[derivative(Default(value = "Verbosity::new(0, 0)"))]
pub verbosity: Verbosity<InfoLevel>,
#[command(flatten)] #[command(flatten)]
pub render_opts: RenderOpts, pub render_opts: RenderOpts,
} }

View File

@@ -9,6 +9,7 @@ mod tests;
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::File,
sync::{ sync::{
atomic::{AtomicBool, AtomicUsize, Ordering}, atomic::{AtomicBool, AtomicUsize, Ordering},
Arc, Mutex, RwLock, Arc, Mutex, RwLock,
@@ -29,6 +30,7 @@ use network::{
}; };
use pnet::datalink::{DataLinkReceiver, NetworkInterface}; use pnet::datalink::{DataLinkReceiver, NetworkInterface};
use ratatui::backend::{Backend, CrosstermBackend}; use ratatui::backend::{Backend, CrosstermBackend};
use simplelog::WriteLogger;
use crate::cli::Opt; use crate::cli::Opt;
@@ -36,8 +38,18 @@ const DISPLAY_DELTA: Duration = Duration::from_millis(1000);
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let opts = Opt::parse(); let opts = Opt::parse();
let os_input = os::get_input(opts.interface.as_deref(), !opts.no_resolve, opts.dns_server)?;
// init logging
if let Some(ref log_path) = opts.log_to {
let log_file = File::options().create_new(true).open(log_path)?;
WriteLogger::init(
opts.verbosity.log_level_filter(),
Default::default(),
log_file,
)?;
}
let os_input = os::get_input(opts.interface.as_deref(), !opts.no_resolve, opts.dns_server)?;
if opts.raw { if opts.raw {
let terminal_backend = RawTerminalBackend {}; let terminal_backend = RawTerminalBackend {};
start(terminal_backend, os_input, opts); start(terminal_backend, os_input, opts);