Better logging

This commit is contained in:
Pierre HUBERT 2025-03-12 16:31:35 +01:00
parent a33561eeb6
commit ec13759704

View File

@ -66,8 +66,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
let (mut client_socket, _) = listener.accept().await?; let (mut client_socket, _) = listener.accept().await?;
tokio::spawn(async move { tokio::spawn(async move {
let conn_id = rand_str(10);
log::info!( log::info!(
"Start new connection from {}", "[{conn_id}] Start new connection from {}",
client_socket.peer_addr().unwrap() client_socket.peer_addr().unwrap()
); );
@ -77,7 +79,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
"{}-{}-{}", "{}-{}-{}",
client_socket.peer_addr().unwrap().ip(), client_socket.peer_addr().unwrap().ip(),
time(), time(),
rand_str(10) conn_id
); );
let mut req_file = OpenOptions::new() let mut req_file = OpenOptions::new()
@ -120,13 +122,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
tokio::select! { tokio::select! {
count = client_read.read(&mut buf_client) => { count = client_read.read(&mut buf_client) => {
let count = match count{ Ok(count) => count, Err(e) => { 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; 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 { if count == 0 {
log::warn!("infinite loop (client)"); log::warn!("[{conn_id}] infinite loop (client), closing connection");
drop(upstream); drop(upstream);
return; return;
} }
@ -150,26 +152,26 @@ async fn main() -> Result<(), Box<dyn Error>> {
buf_client[..count].to_vec() buf_client[..count].to_vec()
}; };
upstream.write_all(&buff).await.expect("Failed to write to upstream"); upstream.write_all(&buff).await.unwrap_or_else(|_| panic!("[{conn_id}] Failed to write to upstream"));
req_file.write_all(&buff).expect("Failed to write to req"); req_file.write_all(&buff).unwrap_or_else(|_| panic!("[{conn_id}] Failed to write to req"));
} }
count = upstream.read(&mut buf_server) => { count = upstream.read(&mut buf_server) => {
let count = match count { let count = match count {
Ok(count) => count, Ok(count) => count,
Err(e) => { Err(e) => {
log::error!("Failed to read from upstream! {e}"); log::error!("[{conn_id}] Failed to read from upstream! {e}");
return; return;
} }
}; };
if count == 0 { if count == 0 {
log::warn!("infinite loop (upstream)"); log::warn!("[{conn_id}] infinite loop (upstream), closing connection");
drop(upstream); drop(upstream);
return; 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"); 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"); res_file.write_all(&buf_server[..count]).expect("Failed to write to res");
} }