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) {
|
pub fn elog(function: &str, message: &str) {
|
||||||
eprintln!(
|
eprintln!("{:?} [{function}] {message}", crate::ONSET.elapsed());
|
||||||
"{:?} [{}] {message}",
|
}
|
||||||
crate::ONSET.elapsed(),
|
|
||||||
std::any::type_name_of_val(function).replace("en::", ""),
|
#[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},
|
http::{Response, StatusCode, header, HeaderValue},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::prelude::*;
|
||||||
use crate::{
|
use crate::{
|
||||||
formats::{Format, populate_graph, serialize_graph},
|
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 Ok(header_value) = HeaderValue::from_str(content_type) {
|
||||||
if let Some(h) = response.headers_mut().insert(header, header_value) {
|
if let Some(h) = response.headers_mut().insert(header, header_value) {
|
||||||
crate::dev::log(
|
log!(
|
||||||
&file,
|
"Overwrote existing header {h:?} because a header for the same key existed"
|
||||||
&format!(
|
|
||||||
"Overwrote existing header {h:?} because a header for \
|
|
||||||
the same key existed"
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
crate::dev::log(
|
log!("Failed to create content type header value from {content_type}");
|
||||||
&file,
|
|
||||||
&format!(
|
|
||||||
"Failed to create content type \
|
|
||||||
header value from {content_type}"
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
response
|
response
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ use axum::{
|
||||||
http::{header, HeaderValue, Response, StatusCode},
|
http::{header, HeaderValue, Response, StatusCode},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
pub(in crate::handlers) fn make_response(
|
pub(in crate::handlers) fn make_response(
|
||||||
body: &str,
|
body: &str,
|
||||||
status_code: u16,
|
status_code: u16,
|
||||||
|
|
@ -18,20 +20,14 @@ pub(in crate::handlers) fn make_response(
|
||||||
if let Some(overwritten) =
|
if let Some(overwritten) =
|
||||||
response.headers_mut().insert(header.0.clone(), wrapped)
|
response.headers_mut().insert(header.0.clone(), wrapped)
|
||||||
{
|
{
|
||||||
crate::dev::log(
|
log!(
|
||||||
&make_response,
|
"Overwrote header {overwritten:?} \
|
||||||
&format!(
|
|
||||||
"Overwrote header {overwritten:?} \
|
|
||||||
because another for key {} already existed",
|
because another for key {} already existed",
|
||||||
header.0
|
header.0
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
crate::dev::log(
|
log!("Failed to wrap header value {}", header.1);
|
||||||
&make_response,
|
|
||||||
&format!("Failed to wrap header value {}", header.1),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,9 @@
|
||||||
use std::{sync, time};
|
use std::{sync, time};
|
||||||
|
|
||||||
|
pub mod prelude {
|
||||||
|
pub use crate::log;
|
||||||
|
}
|
||||||
|
|
||||||
pub mod formats;
|
pub mod formats;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod router;
|
pub mod router;
|
||||||
|
|
|
||||||
24
src/main.rs
24
src/main.rs
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{backtrace, io, panic};
|
use std::{backtrace, io, panic};
|
||||||
|
|
||||||
use en::{ONSET, dev, formats::populate_graph, syntax};
|
use en::{prelude::*, ONSET, formats::populate_graph, syntax};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> io::Result<()> {
|
async fn main() -> io::Result<()> {
|
||||||
|
|
@ -30,26 +30,20 @@ async fn main() -> io::Result<()> {
|
||||||
|
|
||||||
let listener =
|
let listener =
|
||||||
tokio::net::TcpListener::bind(&address).await.map_err(|e| {
|
tokio::net::TcpListener::bind(&address).await.map_err(|e| {
|
||||||
dev::log(
|
log!("Failed to create listener at {address}: {e:#?}");
|
||||||
&main,
|
|
||||||
&format!("Failed to create listener at {address}: {e:#?}"),
|
|
||||||
);
|
|
||||||
e
|
e
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
dev::log(
|
log!(
|
||||||
&main,
|
"Listening on {}",
|
||||||
&format!(
|
listener
|
||||||
"Listening on {}",
|
.local_addr()
|
||||||
listener
|
.map(|s| s.to_string())
|
||||||
.local_addr()
|
.unwrap_or("<unknown>".to_string())
|
||||||
.map(|s| s.to_string())
|
|
||||||
.unwrap_or("<unknown>".to_string())
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
axum::serve(listener, app).await.map_err(|e| {
|
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)
|
io::Error::other(e)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue