add basic logger

This commit is contained in:
2025-05-01 00:08:09 +02:00
parent 06b389b28f
commit 96bf9303ea
4 changed files with 253 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
chrono = "0.4.41"
http-body-util = "0.1.3"
hyper = { version = "1.6.0", features = ["full"] }
hyper-util = { version = "0.1.11", features = ["full"] }

View File

@@ -0,0 +1,20 @@
#[macro_export]
macro_rules! log {
(info $text: literal$(, $($arg: expr),*$(,)?)?) => {
let now = chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, false);
println!(concat!("[{}] INFO ", $text), now, $($($arg),*)?);
};
(debug $text: literal$(, $($arg: expr),*$(,)?)?) => {
#[cfg(debug_assertions)]
{
let now = chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, false);
println!(concat!("[{}] DEBUG ", $text), now, $($($arg),*)?);
}
};
(err $text: literal$(, $($arg: expr),*$(,)?)?) => {
let now = chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, false);
println!(concat!("[{}] ERROR ", $text), now, $($($arg),*)?);
};
}

View File

@@ -6,6 +6,7 @@ use hyper::{
use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;
mod logger;
mod args;
mod config;
@@ -61,11 +62,12 @@ async fn main() {
};
loop {
let Ok((stream, _)) = listener.accept().await else {
let Ok((stream, addr)) = listener.accept().await else {
eprintln!("unable to accept new connections");
return;
};
let io = TokioIo::new(stream);
log!(debug "new connection from {:?}", addr);
let state_clone = state.clone();
tokio::task::spawn(async move {
if let Err(_) = http1::Builder::new().serve_connection(io, service_fn(move |req| {