From 81fc1058ee058c2ca0ad83fb369ad3470e442ebc Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 28 Jul 2023 23:26:37 +0800 Subject: log with `tracing` --- Cargo.lock | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 14 ++++---- 3 files changed, 123 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 41d084d..447e8bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -220,6 +220,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.147" @@ -242,6 +248,12 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" + [[package]] name = "memchr" version = "2.5.0" @@ -268,6 +280,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -293,6 +315,12 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "parking_lot" version = "0.12.1" @@ -374,6 +402,15 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -416,6 +453,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tokio" version = "1.29.1" @@ -447,6 +494,64 @@ dependencies = [ "syn", ] +[[package]] +name = "tracing" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +dependencies = [ + "cfg-if", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + [[package]] name = "udp-obfs-rs" version = "0.1.0" @@ -454,6 +559,8 @@ dependencies = [ "clap", "hex", "tokio", + "tracing", + "tracing-subscriber", ] [[package]] @@ -468,6 +575,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 4066d3d..7e09ce1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,5 @@ edition = "2021" clap = { version = "4", features = ["derive"] } hex = "0.4.3" tokio = { version = "1", features = ["full"] } +tracing = "0.1.37" +tracing-subscriber = "0.3.17" diff --git a/src/main.rs b/src/main.rs index 06f0c05..27051f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::net::{SocketAddr, ToSocketAddrs}; use clap::{arg, Parser}; use tokio::net::UdpSocket; +use tracing::{debug, error}; use crate::Destination::{Address, SockAddr}; @@ -36,6 +37,7 @@ enum Destination<'a> { #[tokio::main] async fn main() { + tracing_subscriber::fmt::init(); let args = Args::parse(); let key = hex::decode(args.key) .expect("failed to decode key hex string"); @@ -52,7 +54,7 @@ async fn main() { loop { match sock.recv_from(&mut buf).await { Ok((len, addr)) => { - eprintln!("recv_from OK, len {}, addr {}", len, addr); + debug!("recv_from OK, len {}, addr {}", len, addr); let mut i = 0; let n = key.len(); for j in 0..len { @@ -72,7 +74,7 @@ async fn main() { Address(addr) => match resolve_addr(&addr) { Ok(addr) => SockAddr(addr), Err(why) => { - eprintln!("Failed to resolve address {}: {}", addr, why); + error!("Failed to resolve address {}: {}", addr, why); Destination::None } }, @@ -90,20 +92,20 @@ async fn main() { }; match target { None => { - eprintln!("cannot send_to: destination not determined"); + error!("cannot send_to: destination not determined"); continue; } Some(target) => { if let Err(why) = sock.send_to(&buf[..len], target).await { - eprintln!("send_to failed, dest: {}, error: {}", target, why); + debug!("send_to failed, dest: {}, error: {}", target, why); } else { - eprintln!("send_to OK, len: {}, dest: {}", len, target); + debug!("send_to OK, len: {}, dest: {}", len, target); } } } } Err(why) => { - eprintln!("recv_from failed: {}", why); + debug!("recv_from failed: {}", why); } } } -- cgit v1.2.3