Replace logging function with a macro
This commit is contained in:
parent
99b9feeb95
commit
0d66b1ee7c
5 changed files with 44 additions and 44 deletions
27
src/dev.rs
27
src/dev.rs
|
|
@ -1,7 +1,22 @@
|
|||
pub fn log<F>(function: &F, message: &str) {
|
||||
eprintln!(
|
||||
"{:?} [{}] {message}",
|
||||
crate::ONSET.elapsed(),
|
||||
std::any::type_name_of_val(function).replace("en::", ""),
|
||||
);
|
||||
pub fn elog(function: &str, message: &str) {
|
||||
eprintln!("{:?} [{function}] {message}", crate::ONSET.elapsed());
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log {
|
||||
($fmt:expr $(, $($arg:tt)+ )? ) => {{
|
||||
let mut scope = std::any::type_name_of_val(&|| {})
|
||||
.to_string().replace("::{{closure}}", "");
|
||||
|
||||
if scope.matches("::").count() > 3 {
|
||||
let parts: Vec<&str> = scope.split("::").collect();
|
||||
if let (Some(module) , Some(caller)) =
|
||||
(parts.get(parts.len().saturating_sub(1)),
|
||||
parts.get(parts.len().saturating_sub(2))) {
|
||||
scope = format!("{module}::{caller}");
|
||||
}
|
||||
}
|
||||
|
||||
$crate::dev::elog(&scope, &format!($fmt $(, $($arg)+ )?));
|
||||
}};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ use axum::{
|
|||
http::{Response, StatusCode, header, HeaderValue},
|
||||
};
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::{
|
||||
formats::{Format, populate_graph, serialize_graph},
|
||||
};
|
||||
|
|
@ -24,22 +25,12 @@ 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) {
|
||||
crate::dev::log(
|
||||
&file,
|
||||
&format!(
|
||||
"Overwrote existing header {h:?} because a header for \
|
||||
the same key existed"
|
||||
),
|
||||
log!(
|
||||
"Overwrote existing header {h:?} because a header for the same key existed"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
crate::dev::log(
|
||||
&file,
|
||||
&format!(
|
||||
"Failed to create content type \
|
||||
header value from {content_type}"
|
||||
),
|
||||
);
|
||||
log!("Failed to create content type header value from {content_type}");
|
||||
}
|
||||
|
||||
response
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ use axum::{
|
|||
http::{header, HeaderValue, Response, StatusCode},
|
||||
};
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
pub(in crate::handlers) fn make_response(
|
||||
body: &str,
|
||||
status_code: u16,
|
||||
|
|
@ -18,20 +20,14 @@ pub(in crate::handlers) fn make_response(
|
|||
if let Some(overwritten) =
|
||||
response.headers_mut().insert(header.0.clone(), wrapped)
|
||||
{
|
||||
crate::dev::log(
|
||||
&make_response,
|
||||
&format!(
|
||||
"Overwrote header {overwritten:?} \
|
||||
log!(
|
||||
"Overwrote header {overwritten:?} \
|
||||
because another for key {} already existed",
|
||||
header.0
|
||||
),
|
||||
header.0
|
||||
);
|
||||
}
|
||||
} else {
|
||||
crate::dev::log(
|
||||
&make_response,
|
||||
&format!("Failed to wrap header value {}", header.1),
|
||||
);
|
||||
log!("Failed to wrap header value {}", header.1);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
use std::{sync, time};
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::log;
|
||||
}
|
||||
|
||||
pub mod formats;
|
||||
pub mod types;
|
||||
pub mod router;
|
||||
|
|
|
|||
24
src/main.rs
24
src/main.rs
|
|
@ -1,6 +1,6 @@
|
|||
use std::{backtrace, io, panic};
|
||||
|
||||
use en::{ONSET, dev, formats::populate_graph, syntax};
|
||||
use en::{prelude::*, ONSET, formats::populate_graph, syntax};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
|
|
@ -30,26 +30,20 @@ async fn main() -> io::Result<()> {
|
|||
|
||||
let listener =
|
||||
tokio::net::TcpListener::bind(&address).await.map_err(|e| {
|
||||
dev::log(
|
||||
&main,
|
||||
&format!("Failed to create listener at {address}: {e:#?}"),
|
||||
);
|
||||
log!("Failed to create listener at {address}: {e:#?}");
|
||||
e
|
||||
})?;
|
||||
|
||||
dev::log(
|
||||
&main,
|
||||
&format!(
|
||||
"Listening on {}",
|
||||
listener
|
||||
.local_addr()
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or("<unknown>".to_string())
|
||||
),
|
||||
log!(
|
||||
"Listening on {}",
|
||||
listener
|
||||
.local_addr()
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or("<unknown>".to_string())
|
||||
);
|
||||
|
||||
axum::serve(listener, app).await.map_err(|e| {
|
||||
dev::log(&main, &format!("Failed to serve application: {e:#?}"));
|
||||
log!("Failed to serve application: {e:#?}");
|
||||
io::Error::other(e)
|
||||
})?;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue