Skip to content

Commit

Permalink
refactor: replace log with tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Sep 14, 2024
1 parent 75a5c60 commit c983bc2
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 32 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tuic-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
3 changes: 1 addition & 2 deletions tuic-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
13 changes: 7 additions & 6 deletions tuic-server/src/connection/handle_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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;
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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
23 changes: 12 additions & 11 deletions tuic-server/src/connection/handle_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
12 changes: 6 additions & 6 deletions tuic-server/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion tuic-server/src/connection/udp_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tokio::{
RwLock as AsyncRwLock,
},
};
use tracing::warn;
use tuic::Address;

use super::Connection;
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion tuic-server/src/restful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use axum_extra::{
TypedHeader,
};
use lateinit::LateInit;
use tracing::warn;
use uuid::Uuid;

use crate::CONFIG;
Expand All @@ -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();
}

Expand Down
7 changes: 4 additions & 3 deletions tuic-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -131,7 +132,7 @@ impl Server {
}

pub async fn start(&self) {
log::warn!(
warn!(
"server started, listening on {}",
self.ep.local_addr().unwrap()
);
Expand All @@ -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;
}
}
Expand Down

0 comments on commit c983bc2

Please sign in to comment.