Skip certain paths from logging

This commit is contained in:
Juno Takano 2026-01-02 15:30:17 -03:00
commit 0ddc03f6c1
2 changed files with 17 additions and 2 deletions

View file

@ -1,8 +1,14 @@
#[allow(clippy::print_stderr)]
pub fn elog(function: &str, message: &str) {
eprintln!("{:?} [{function}] {message}", crate::ONSET.elapsed());
}
// Paths in this slice suppress logging if found in the stack trace
pub const SKIP_PATHS: &[&str] = &[
"en::types::Config::parse_text"
];
#[macro_export]
macro_rules! log {
($fmt:expr $(, $($arg:tt)+ )? ) => {{
@ -10,6 +16,8 @@ macro_rules! log {
let mut path = std::any::type_name_of_val(&|| {})
.to_string().replace("::{{closure}}", "");
let trace = format!("{:?}", std::backtrace::Backtrace::capture());
let level: u8 = std::env::var("DEBUG")
.unwrap_or("0".to_string()).trim().parse().unwrap_or(0);
@ -34,6 +42,8 @@ macro_rules! log {
path_vec.get(path_vec.len().saturating_sub(3)),
) {
display_path = if level > 3 {
format!("{} -> {}", trace, path.clone())
} else if level > 2 {
path.clone()
} else if level > 0 {
format!("{third_to_last}::{second_to_last}::{last}")
@ -47,7 +57,8 @@ macro_rules! log {
let filter = std::env::var("DEBUG_FILTER").unwrap_or("any".to_string());
if filter == "any" || filter.is_empty() || path.contains(&filter) {
if $crate::dev::SKIP_PATHS.iter().all(|&s| !trace.contains(s)) &&
(filter == "any" || filter.is_empty() || path.contains(&filter)) {
$crate::dev::elog(&display_path, &format!($fmt $(, $($arg)+ )?));
};

View file

@ -20,10 +20,14 @@ async fn main() -> io::Result<()> {
|s| format!("{}:{}:{}", s.file(), s.line(), s.column()),
);
let level: u8 = std::env::var("RUST_BACKTRACE")
.unwrap_or("0".to_string()).trim().parse().unwrap_or(0);
eprintln!(" P! [{:?}] {location}: {payload}", ONSET.elapsed());
let trace = backtrace::Backtrace::capture();
if trace.status() == backtrace::BacktraceStatus::Captured {
if trace.status() == backtrace::BacktraceStatus::Captured && level > 1
{
eprintln!("\n Stack trace:\n{trace:#?}");
}
}));