From ec13759704cb02c81dbb8cd9a7305060f90b875a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 12 Mar 2025 16:31:35 +0100 Subject: [PATCH] Better logging --- src/main.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index c5dc4bc..80a1ebb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,8 +66,10 @@ async fn main() -> Result<(), Box> { let (mut client_socket, _) = listener.accept().await?; tokio::spawn(async move { + let conn_id = rand_str(10); + log::info!( - "Start new connection from {}", + "[{conn_id}] Start new connection from {}", client_socket.peer_addr().unwrap() ); @@ -77,7 +79,7 @@ async fn main() -> Result<(), Box> { "{}-{}-{}", client_socket.peer_addr().unwrap().ip(), time(), - rand_str(10) + conn_id ); let mut req_file = OpenOptions::new() @@ -120,13 +122,13 @@ async fn main() -> Result<(), Box> { tokio::select! { count = client_read.read(&mut buf_client) => { let count = match count{ Ok(count) => count, Err(e) => { - log::error!("Failed to read data from client, closing connection! {e}"); + log::error!("[{conn_id}] Failed to read data from client, closing connection! {e}"); return; }}; - log::info!("Got a new client read {count} - {base_file_name}"); + log::info!("[{conn_id}] Got a new client read {count} - {base_file_name}"); if count == 0 { - log::warn!("infinite loop (client)"); + log::warn!("[{conn_id}] infinite loop (client), closing connection"); drop(upstream); return; } @@ -150,26 +152,26 @@ async fn main() -> Result<(), Box> { buf_client[..count].to_vec() }; - upstream.write_all(&buff).await.expect("Failed to write to upstream"); - req_file.write_all(&buff).expect("Failed to write to req"); + upstream.write_all(&buff).await.unwrap_or_else(|_| panic!("[{conn_id}] Failed to write to upstream")); + req_file.write_all(&buff).unwrap_or_else(|_| panic!("[{conn_id}] Failed to write to req")); } count = upstream.read(&mut buf_server) => { let count = match count { Ok(count) => count, Err(e) => { - log::error!("Failed to read from upstream! {e}"); + log::error!("[{conn_id}] Failed to read from upstream! {e}"); return; } }; if count == 0 { - log::warn!("infinite loop (upstream)"); + log::warn!("[{conn_id}] infinite loop (upstream), closing connection"); drop(upstream); return; } - log::info!("Got a new upstream read {count} - {base_file_name}"); + log::info!("[{conn_id}] Got a new upstream read {count} - {base_file_name}"); client_write.write_all(&buf_server[..count]).await.expect("Failed to write to client"); res_file.write_all(&buf_server[..count]).expect("Failed to write to res"); }