Skip to content

Commit a4e526c

Browse files
committed
chore: change log into tracing
1 parent d2dbbaa commit a4e526c

11 files changed

Lines changed: 111 additions & 100 deletions

File tree

Cargo.lock

Lines changed: 4 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tuic-client/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ jemallocator = ["tikv-jemallocator"]
2020
[dependencies]
2121
bytes = { version = "1", default-features = false, features = ["std"] }
2222

23-
env_logger = { version = "0.11", default-features = false, features = ["humantime"] }
24-
humantime = { version = "2", default-features = false }
2523
lexopt = { version = "0.3", default-features = false }
26-
log = { version = "0.4", default-features = false, features = ["serde", "std"] }
2724
once_cell = { version = "1", default-features = false, features = ["parking_lot", "std"] }
2825

2926
serde = { version = "1", default-features = false, features = ["derive", "std"] }
@@ -57,4 +54,11 @@ thiserror = { version = "2", default-features = false }
5754
anyhow = "1"
5855
eyre = { version = "0" }
5956

57+
# Logging
58+
time = { version = "0.3", features = ["macros", "local-offset"] }
59+
humantime = { version = "2", default-features = false }
60+
tracing-subscriber = { version = "0.3", default-features = false, features = ["tracing-log", "std", "local-time", "fmt", "ansi"] }
61+
chrono = "0.4"
62+
tracing = "0.1"
63+
6064
tikv-jemallocator = { version = "0.6", optional = true }

tuic-client/src/config.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::{
1212

1313
use humantime::Duration as HumanDuration;
1414
use lexopt::{Arg, Error as ArgumentError, Parser};
15-
use log::LevelFilter;
1615
use serde::{Deserialize, Deserializer, de::Error as DeError};
1716
use serde_json::Error as SerdeError;
1817
use thiserror::Error;
@@ -37,7 +36,7 @@ pub struct Config {
3736
pub local: Local,
3837

3938
#[serde(default = "default::log_level")]
40-
pub log_level: LevelFilter,
39+
pub log_level: String,
4140
}
4241

4342
#[derive(Deserialize)]
@@ -179,7 +178,6 @@ impl Config {
179178
}
180179

181180
mod default {
182-
use log::LevelFilter;
183181

184182
pub mod relay {
185183
use std::{path::PathBuf, time::Duration};
@@ -271,8 +269,8 @@ mod default {
271269
}
272270
}
273271

274-
pub fn log_level() -> LevelFilter {
275-
LevelFilter::Warn
272+
pub fn log_level() -> String {
273+
"info".into()
276274
}
277275
}
278276

tuic-client/src/connection/handle_stream.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::sync::atomic::Ordering;
33
use bytes::Bytes;
44
use quinn::{RecvStream, SendStream, VarInt};
55
use register_count::Register;
6+
use tracing::{debug, warn};
67
use tuic_quinn::Task;
78

89
use super::Connection;
@@ -46,7 +47,7 @@ impl Connection {
4647
}
4748

4849
pub async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) {
49-
log::debug!("[relay] incoming unidirectional stream");
50+
debug!("[relay] incoming unidirectional stream");
5051

5152
let res = match self.model.accept_uni_stream(recv).await {
5253
Err(err) => Err(Error::Model(err)),
@@ -61,25 +62,25 @@ impl Connection {
6162
};
6263

6364
if let Err(err) = res {
64-
log::warn!("[relay] incoming unidirectional stream error: {err}");
65+
warn!("[relay] incoming unidirectional stream error: {err}");
6566
}
6667
}
6768

6869
pub async fn handle_bi_stream(self, send: SendStream, recv: RecvStream, _reg: Register) {
69-
log::debug!("[relay] incoming bidirectional stream");
70+
debug!("[relay] incoming bidirectional stream");
7071

7172
let res = match self.model.accept_bi_stream(send, recv).await {
7273
Err(err) => Err::<(), _>(Error::Model(err)),
7374
_ => unreachable!(), // already filtered in `tuic_quinn`
7475
};
7576

7677
if let Err(err) = res {
77-
log::warn!("[relay] incoming bidirectional stream error: {err}");
78+
warn!("[relay] incoming bidirectional stream error: {err}");
7879
}
7980
}
8081

8182
pub async fn handle_datagram(self, dg: Bytes) {
82-
log::debug!("[relay] incoming datagram");
83+
debug!("[relay] incoming datagram");
8384

8485
let res = match self.model.accept_datagram(dg) {
8586
Err(err) => Err(Error::Model(err)),
@@ -94,7 +95,7 @@ impl Connection {
9495
};
9596

9697
if let Err(err) = res {
97-
log::warn!("[relay] incoming datagram error: {err}");
98+
warn!("[relay] incoming datagram error: {err}");
9899
}
99100
}
100101
}

tuic-client/src/connection/handle_task.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use bytes::Bytes;
44
use quinn::ZeroRttAccepted;
55
use socks5_proto::Address as Socks5Address;
66
use tokio::time;
7+
use tracing::{debug, info, warn};
78
use tuic::Address;
89
use tuic_quinn::{Connect, Packet};
910

@@ -13,30 +14,30 @@ use crate::{error::Error, socks5::UDP_SESSIONS as SOCKS5_UDP_SESSIONS, utils::Ud
1314
impl Connection {
1415
pub async fn authenticate(self, zero_rtt_accepted: Option<ZeroRttAccepted>) {
1516
if let Some(zero_rtt_accepted) = zero_rtt_accepted {
16-
log::debug!("[relay] [authenticate] waiting for connection to be fully established");
17+
debug!("[relay] [authenticate] waiting for connection to be fully established");
1718
zero_rtt_accepted.await;
1819
}
1920

20-
log::debug!("[relay] [authenticate] sending authentication");
21+
debug!("[relay] [authenticate] sending authentication");
2122

2223
match self
2324
.model
2425
.authenticate(self.uuid, self.password.clone())
2526
.await
2627
{
27-
Ok(()) => log::info!("[relay] [authenticate] {uuid}", uuid = self.uuid),
28-
Err(err) => log::warn!("[relay] [authenticate] authentication sending error: {err}"),
28+
Ok(()) => info!("[relay] [authenticate] {uuid}", uuid = self.uuid),
29+
Err(err) => warn!("[relay] [authenticate] authentication sending error: {err}"),
2930
}
3031
}
3132

3233
pub async fn connect(&self, addr: Address) -> Result<Connect, Error> {
3334
let addr_display = addr.to_string();
34-
log::info!("[relay] [connect] {addr_display}");
35+
info!("[relay] [connect] {addr_display}");
3536

3637
match self.model.connect(addr).await {
3738
Ok(conn) => Ok(conn),
3839
Err(err) => {
39-
log::warn!("[relay] [connect] failed initializing relay to {addr_display}: {err}");
40+
warn!("[relay] [connect] failed initializing relay to {addr_display}: {err}");
4041
Err(Error::Model(err))
4142
}
4243
}
@@ -47,11 +48,11 @@ impl Connection {
4748

4849
match self.udp_relay_mode {
4950
UdpRelayMode::Native => {
50-
log::info!("[relay] [packet] [{assoc_id:#06x}] [to-native] to {addr_display}");
51+
info!("[relay] [packet] [{assoc_id:#06x}] [to-native] to {addr_display}");
5152
match self.model.packet_native(pkt, addr, assoc_id) {
5253
Ok(()) => Ok(()),
5354
Err(err) => {
54-
log::warn!(
55+
warn!(
5556
"[relay] [packet] [{assoc_id:#06x}] [to-native] to {addr_display}: \
5657
{err}"
5758
);
@@ -60,11 +61,11 @@ impl Connection {
6061
}
6162
}
6263
UdpRelayMode::Quic => {
63-
log::info!("[relay] [packet] [{assoc_id:#06x}] [to-quic] {addr_display}");
64+
info!("[relay] [packet] [{assoc_id:#06x}] [to-quic] {addr_display}");
6465
match self.model.packet_quic(pkt, addr, assoc_id).await {
6566
Ok(()) => Ok(()),
6667
Err(err) => {
67-
log::warn!(
68+
warn!(
6869
"[relay] [packet] [{assoc_id:#06x}] [to-quic] to {addr_display}: {err}"
6970
);
7071
Err(err)
@@ -75,11 +76,11 @@ impl Connection {
7576
}
7677

7778
pub async fn dissociate(&self, assoc_id: u16) -> eyre::Result<()> {
78-
log::info!("[relay] [dissociate] [{assoc_id:#06x}]");
79+
info!("[relay] [dissociate] [{assoc_id:#06x}]");
7980
match self.model.dissociate(assoc_id).await {
8081
Ok(()) => Ok(()),
8182
Err(err) => {
82-
log::warn!("[relay] [dissociate] [{assoc_id:#06x}] {err}");
83+
warn!("[relay] [dissociate] [{assoc_id:#06x}] {err}");
8384
Err(err)?
8485
}
8586
}
@@ -98,8 +99,8 @@ impl Connection {
9899
}
99100

100101
match self.model.heartbeat().await {
101-
Ok(()) => log::debug!("[relay] [heartbeat]"),
102-
Err(err) => log::warn!("[relay] [heartbeat] {err}"),
102+
Ok(()) => debug!("[relay] [heartbeat]"),
103+
Err(err) => warn!("[relay] [heartbeat] {err}"),
103104
}
104105
}
105106
}
@@ -116,7 +117,7 @@ impl Connection {
116117
unreachable!()
117118
};
118119

119-
log::info!(
120+
info!(
120121
"[relay] [packet] [{assoc_id:#06x}] [from-{mode}] [{pkt_id:#06x}] fragment \
121122
{frag_id}/{frag_total}",
122123
frag_id = pkt.frag_id() + 1,
@@ -125,7 +126,7 @@ impl Connection {
125126

126127
match pkt.accept().await {
127128
Ok(Some((pkt, addr, _))) => {
128-
log::info!(
129+
info!(
129130
"[relay] [packet] [{assoc_id:#06x}] [from-{mode}] [{pkt_id:#06x}] from {addr}"
130131
);
131132

@@ -147,20 +148,20 @@ impl Connection {
147148

148149
if let Some(session) = session {
149150
if let Err(err) = session.send(pkt, addr).await {
150-
log::warn!(
151+
warn!(
151152
"[relay] [packet] [{assoc_id:#06x}] [from-native] [{pkt_id:#06x}] \
152153
failed sending packet to socks5 client: {err}",
153154
);
154155
}
155156
} else {
156-
log::warn!(
157+
warn!(
157158
"[relay] [packet] [{assoc_id:#06x}] [from-native] [{pkt_id:#06x}] unable \
158159
to find socks5 associate session"
159160
);
160161
}
161162
}
162163
Ok(None) => {}
163-
Err(err) => log::warn!(
164+
Err(err) => warn!(
164165
"[relay] [packet] [{assoc_id:#06x}] [from-native] [{pkt_id:#06x}] packet \
165166
receiving error: {err}"
166167
),

tuic-client/src/connection/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use tokio::{
2222
sync::{OnceCell as AsyncOnceCell, RwLock as AsyncRwLock},
2323
time,
2424
};
25+
use tracing::{debug, info, warn};
2526
use tuic_quinn::{Connection as Model, side};
2627
use uuid::Uuid;
2728

@@ -284,7 +285,7 @@ impl Connection {
284285
gc_interval: Duration,
285286
gc_lifetime: Duration,
286287
) {
287-
log::info!("[relay] connection established");
288+
info!("[relay] connection established");
288289

289290
tokio::spawn(self.clone().authenticate(zero_rtt_accepted));
290291
tokio::spawn(self.clone().heartbeat(heartbeat));
@@ -307,7 +308,7 @@ impl Connection {
307308
};
308309
};
309310

310-
log::warn!("[relay] connection error: {err}");
311+
warn!("[relay] connection error: {err}");
311312
}
312313

313314
fn is_closed(&self) -> bool {
@@ -322,7 +323,7 @@ impl Connection {
322323
break;
323324
}
324325

325-
log::debug!("[relay] packet fragment garbage collecting event");
326+
debug!("[relay] packet fragment garbage collecting event");
326327
self.model.collect_garbage(gc_lifetime);
327328
}
328329
}

0 commit comments

Comments
 (0)