Add a custom log function

This commit is contained in:
Juno Takano 2025-12-13 16:28:13 -03:00
commit 4db4c703d1
5 changed files with 32 additions and 14 deletions

8
src/dev.rs Normal file
View file

@ -0,0 +1,8 @@
pub fn log<F>(function: &F, message: &str) {
eprintln!(
"{:?} [{}] {message}",
crate::ONSET.elapsed(),
std::any::type_name_of_val(function).replace("en::", ""),
);
}

View file

@ -6,6 +6,7 @@ use axum::{
use crate::formats::{Format, populate_graph, serialize_graph};
use crate::handlers;
#[expect(clippy::unused_async)]
pub async fn file(file_path: &str, content_type: &str) -> Response<Body> {
let content = match std::fs::read(file_path) {
Ok(s) => s,
@ -20,21 +21,24 @@ pub async fn file(file_path: &str, content_type: &str) -> Response<Body> {
if let Ok(header_value) = HeaderValue::from_str(content_type) {
if let Some(h) = response.headers_mut().insert(header, header_value) {
eprintln!(
"[file_handler] Overwrote existing header {h:?} \
because a header for the same key existed"
crate::dev::log(
&file,
&format!("Overwrote existing header {h:?} because a header for \
the same key existed")
);
}
} else {
eprintln!(
"[file_handler] Failed to create content type \
header value from {content_type}"
crate::dev::log(
&file,
&format!("[file_handler] Failed to create content type \
header value from {content_type}")
);
}
response
}
#[expect(clippy::unused_async)]
pub async fn serial(format: &Format) -> Response<Body> {
let graph = populate_graph();
let body = serialize_graph(format, &graph);

View file

@ -7,6 +7,7 @@ use axum::{
use crate::{formats::populate_graph, types::Node, handlers};
#[expect(clippy::unused_async)]
pub async fn nexus(template: &str) -> Response<Body> {
let mut context = tera::Context::new();
let graph = populate_graph();

View file

@ -18,16 +18,17 @@ pub fn make_response(
if let Some(overwritten) =
response.headers_mut().insert(header.0.clone(), wrapped)
{
eprintln!(
"[make_response] Overwrote header {overwritten:?} \
crate::dev::log(
&make_response,
&format!("Overwrote header {overwritten:?} \
because another for key {} already existed",
header.0
header.0)
);
}
} else {
eprintln!(
"[make_response] Failed to wrap header value {}",
header.1
crate::dev::log(
&make_response,
&format!("Failed to wrap header value {}", header.1)
);
}
}

View file

@ -5,6 +5,7 @@ use formats::Format;
mod formats;
mod types;
mod handlers;
mod dev;
static ONSET: std::sync::LazyLock<std::time::Instant> =
std::sync::LazyLock::new(std::time::Instant::now);
@ -60,8 +61,11 @@ async fn main() {
match axum::serve(listener, app).await {
Ok(()) => (),
Err(e) => {
eprintln!(
"Failed to serve application with axum::serve: {e:#?}"
dev::log(
&main,
&format!(
"Failed to serve application with axum::serve: {e:#?}"
)
);
std::process::exit(1);
},