From c983bc2d87f91f28d07e8696a44a36dd95aff625 Mon Sep 17 00:00:00 2001 From: Itsusinn Date: Sun, 15 Sep 2024 03:46:36 +0800 Subject: [PATCH] refactor: replace log with tracing --- Cargo.lock | 1 - tuic-server/Cargo.toml | 1 - tuic-server/src/config.rs | 3 +-- tuic-server/src/connection/handle_stream.rs | 13 ++++++------ tuic-server/src/connection/handle_task.rs | 23 +++++++++++---------- tuic-server/src/connection/mod.rs | 12 +++++------ tuic-server/src/connection/udp_session.rs | 3 ++- tuic-server/src/restful.rs | 3 ++- tuic-server/src/server.rs | 7 ++++--- 9 files changed, 34 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3e87cca1..82b09f8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1606,7 +1606,6 @@ dependencies = [ "json_comments", "lateinit", "lexopt", - "log", "quinn", "rcgen", "register-count", diff --git a/tuic-server/Cargo.toml b/tuic-server/Cargo.toml index 2aebef24..0422ac12 100644 --- a/tuic-server/Cargo.toml +++ b/tuic-server/Cargo.toml @@ -49,7 +49,6 @@ humantime-serde = "1" json_comments = "0.2" # Logging -log = { version = "0.4", default-features = false, features = ["serde", "std"] } time = { version = "0.3", features = ["macros", "local-offset"] } humantime = { version = "2", default-features = false } tracing-subscriber = { version = "0.3", default-features = false, features = ["tracing-log", "std", "local-time","fmt"] } diff --git a/tuic-server/src/config.rs b/tuic-server/src/config.rs index 428acdba..7af0cebe 100644 --- a/tuic-server/src/config.rs +++ b/tuic-server/src/config.rs @@ -7,9 +7,8 @@ use figment::{ }; use json_comments::StripComments; use lexopt::{Arg, Parser}; -use log::warn; use serde::{Deserialize, Serialize}; -use tracing::level_filters::LevelFilter; +use tracing::{level_filters::LevelFilter, warn}; use uuid::Uuid; use crate::{ diff --git a/tuic-server/src/connection/handle_stream.rs b/tuic-server/src/connection/handle_stream.rs index d8b5b3da..7ab0cec0 100644 --- a/tuic-server/src/connection/handle_stream.rs +++ b/tuic-server/src/connection/handle_stream.rs @@ -4,6 +4,7 @@ use bytes::Bytes; use quinn::{RecvStream, SendStream, VarInt}; use register_count::Register; use tokio::time; +use tracing::{debug, warn}; use tuic_quinn::Task; use super::Connection; @@ -11,7 +12,7 @@ use crate::{error::Error, utils::UdpRelayMode, CONFIG}; impl Connection { pub async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) { - log::debug!( + debug!( "[{id:#010x}] [{addr}] [{user}] incoming unidirectional stream", id = self.id(), addr = self.inner.remote_address(), @@ -60,7 +61,7 @@ impl Connection { Ok(Task::Dissociate(assoc_id)) => self.handle_dissociate(assoc_id).await, Ok(_) => unreachable!(), // already filtered in `tuic_quinn` Err(err) => { - log::warn!( + warn!( "[{id:#010x}] [{addr}] [{user}] handling incoming unidirectional stream \ error: {err}", id = self.id(), @@ -73,7 +74,7 @@ impl Connection { } pub async fn handle_bi_stream(self, (send, recv): (SendStream, RecvStream), _reg: Register) { - log::debug!( + debug!( "[{id:#010x}] [{addr}] [{user}] incoming bidirectional stream", id = self.id(), addr = self.inner.remote_address(), @@ -110,7 +111,7 @@ impl Connection { Ok(Task::Connect(conn)) => self.handle_connect(conn).await, Ok(_) => unreachable!(), // already filtered in `tuic_quinn` Err(err) => { - log::warn!( + warn!( "[{id:#010x}] [{addr}] [{user}] handling incoming bidirectional stream error: \ {err}", id = self.id(), @@ -123,7 +124,7 @@ impl Connection { } pub async fn handle_datagram(self, dg: Bytes) { - log::debug!( + debug!( "[{id:#010x}] [{addr}] [{user}] incoming datagram", id = self.id(), addr = self.inner.remote_address(), @@ -152,7 +153,7 @@ impl Connection { Ok(Task::Heartbeat) => self.handle_heartbeat().await, Ok(_) => unreachable!(), Err(err) => { - log::warn!( + warn!( "[{id:#010x}] [{addr}] [{user}] handling incoming datagram error: {err}", id = self.id(), addr = self.inner.remote_address(), diff --git a/tuic-server/src/connection/handle_task.rs b/tuic-server/src/connection/handle_task.rs index c526ee96..b35664c7 100644 --- a/tuic-server/src/connection/handle_task.rs +++ b/tuic-server/src/connection/handle_task.rs @@ -10,6 +10,7 @@ use tokio::{ net::{self, TcpStream}, }; use tokio_util::compat::FuturesAsyncReadCompatExt; +use tracing::{info, warn}; use tuic::Address; use tuic_quinn::{Authenticate, Connect, Packet}; @@ -18,7 +19,7 @@ use crate::{error::Error, utils::UdpRelayMode}; impl Connection { pub async fn handle_authenticate(&self, auth: Authenticate) { - log::info!( + info!( "[{id:#010x}] [{addr}] [{user}] [authenticate] {auth_uuid}", id = self.id(), addr = self.inner.remote_address(), @@ -30,7 +31,7 @@ impl Connection { pub async fn handle_connect(&self, conn: Connect) { let target_addr = conn.addr().to_string(); - log::info!( + info!( "[{id:#010x}] [{addr}] [{user}] [connect] {target_addr}", id = self.id(), addr = self.inner.remote_address(), @@ -73,7 +74,7 @@ impl Connection { match process.await { Ok(()) => {} - Err(err) => log::warn!( + Err(err) => warn!( "[{id:#010x}] [{addr}] [{user}] [connect] {target_addr}: {err}", id = self.id(), addr = self.inner.remote_address(), @@ -88,7 +89,7 @@ impl Connection { let frag_id = pkt.frag_id(); let frag_total = pkt.frag_total(); - log::info!( + info!( "[{id:#010x}] [{addr}] [{user}] [packet] [{assoc_id:#06x}] [from-{mode}] \ [{pkt_id:#06x}] fragment {frag_id}/{frag_total}", id = self.id(), @@ -103,7 +104,7 @@ impl Connection { Ok(None) => return, Ok(Some(res)) => res, Err(err) => { - log::warn!( + warn!( "[{id:#010x}] [{addr}] [{user}] [packet] [{assoc_id:#06x}] [from-{mode}] \ [{pkt_id:#06x}] fragment {frag_id}/{frag_total}: {err}", id = self.id(), @@ -116,7 +117,7 @@ impl Connection { }; let process = async { - log::info!( + info!( "[{id:#010x}] [{addr}] [{user}] [packet] [{assoc_id:#06x}] [from-{mode}] \ [{pkt_id:#06x}] to {src_addr}", id = self.id(), @@ -151,7 +152,7 @@ impl Connection { }; if let Err(err) = process.await { - log::warn!( + warn!( "[{id:#010x}] [{addr}] [{user}] [packet] [{assoc_id:#06x}] [from-{mode}] \ [{pkt_id:#06x}] to {src_addr}: {err}", id = self.id(), @@ -163,7 +164,7 @@ impl Connection { } pub async fn handle_dissociate(&self, assoc_id: u16) { - log::info!( + info!( "[{id:#010x}] [{addr}] [{user}] [dissociate] [{assoc_id:#06x}]", id = self.id(), addr = self.inner.remote_address(), @@ -176,7 +177,7 @@ impl Connection { } pub async fn handle_heartbeat(&self) { - log::info!( + info!( "[{id:#010x}] [{addr}] [{user}] [heartbeat]", id = self.id(), addr = self.inner.remote_address(), @@ -187,7 +188,7 @@ impl Connection { pub async fn relay_packet(self, pkt: Bytes, addr: Address, assoc_id: u16) { let addr_display = addr.to_string(); - log::info!( + info!( "[{id:#010x}] [{addr}] [{user}] [packet] [{assoc_id:#06x}] [to-{mode}] from {src_addr}", id = self.id(), addr = self.inner.remote_address(), @@ -202,7 +203,7 @@ impl Connection { }; if let Err(err) = res { - log::warn!( + warn!( "[{id:#010x}] [{addr}] [{user}] [packet] [{assoc_id:#06x}] [to-{mode}] from \ {src_addr}: {err}", id = self.id(), diff --git a/tuic-server/src/connection/mod.rs b/tuic-server/src/connection/mod.rs index d0241716..bb57759d 100644 --- a/tuic-server/src/connection/mod.rs +++ b/tuic-server/src/connection/mod.rs @@ -8,7 +8,7 @@ use arc_swap::ArcSwap; use quinn::{Connecting, Connection as QuinnConnection, VarInt}; use register_count::Counter; use tokio::{sync::RwLock as AsyncRwLock, time}; -use tracing::{info, warn}; +use tracing::{debug, info, warn}; use tuic_quinn::{side, Authenticate, Connection as Model}; use self::{authenticated::Authenticated, udp_session::UdpSession}; @@ -85,13 +85,13 @@ impl Connection { match handle_incoming.await { Ok(()) => {} Err(err) if err.is_trivial() => { - log::debug!( + debug!( "[{id:#010x}] [{addr}] [{user}] {err}", id = conn.id(), user = conn.auth, ); } - Err(err) => log::warn!( + Err(err) => warn!( "[{id:#010x}] [{addr}] [{user}] connection error: {err}", id = conn.id(), user = conn.auth, @@ -100,13 +100,13 @@ impl Connection { } } Err(err) if err.is_trivial() => { - log::debug!( + debug!( "[{id:#010x}] [{addr}] [unauthenticated] {err}", id = u32::MAX, ); } Err(err) => { - log::warn!( + warn!( "[{id:#010x}] [{addr}] [unauthenticated] {err}", id = u32::MAX, ) @@ -172,7 +172,7 @@ impl Connection { break; } - log::debug!( + debug!( "[{id:#010x}] [{addr}] [{user}] packet fragment garbage collecting event", id = self.id(), addr = self.inner.remote_address(), diff --git a/tuic-server/src/connection/udp_session.rs b/tuic-server/src/connection/udp_session.rs index e229ee09..13c2e620 100644 --- a/tuic-server/src/connection/udp_session.rs +++ b/tuic-server/src/connection/udp_session.rs @@ -13,6 +13,7 @@ use tokio::{ RwLock as AsyncRwLock, }, }; +use tracing::warn; use tuic::Address; use super::Connection; @@ -95,7 +96,7 @@ impl UdpSession { let (pkt, addr) = match session_listening.recv().await { Ok(res) => res, Err(err) => { - log::warn!( + warn!( "[{id:#010x}] [{addr}] [{user}] [packet] [{assoc_id:#06x}] outbound \ listening error: {err}", id = session_listening.0.conn.id(), diff --git a/tuic-server/src/restful.rs b/tuic-server/src/restful.rs index bed73e44..f81dadd0 100644 --- a/tuic-server/src/restful.rs +++ b/tuic-server/src/restful.rs @@ -13,6 +13,7 @@ use axum_extra::{ TypedHeader, }; use lateinit::LateInit; +use tracing::warn; use uuid::Uuid; use crate::CONFIG; @@ -31,7 +32,7 @@ pub async fn start() { .route("/kick", post(kick)) .route("/online", get(list_online)); let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); - log::warn!("RESTful server started, listening on {addr}"); + warn!("RESTful server started, listening on {addr}"); axum::serve(listener, app).await.unwrap(); } diff --git a/tuic-server/src/server.rs b/tuic-server/src/server.rs index a78fc046..85a604b0 100644 --- a/tuic-server/src/server.rs +++ b/tuic-server/src/server.rs @@ -14,6 +14,7 @@ use rustls::{ ServerConfig as RustlsServerConfig, }; use socket2::{Domain, Protocol, SockAddr, Socket, Type}; +use tracing::{debug, warn}; use crate::{ connection::{Connection, INIT_CONCURRENT_STREAMS}, @@ -131,7 +132,7 @@ impl Server { } pub async fn start(&self) { - log::warn!( + warn!( "server started, listening on {}", self.ep.local_addr().unwrap() ); @@ -146,11 +147,11 @@ impl Server { tokio::spawn(Connection::handle(conn)); } Err(e) => { - log::debug!("[Incoming] Failed to accept connection: {e}"); + debug!("[Incoming] Failed to accept connection: {e}"); } }, None => { - log::debug!("[Incoming] ep.accept() returned None."); + debug!("[Incoming] the endpoint is closed"); return; } }