melib: add timestamp to debug trace logs

This commit is contained in:
Manos Pitsidianakis 2020-05-16 12:46:01 +03:00
parent 295577f9d7
commit 68b1feb6c8
No known key found for this signature in database
GPG Key ID: 73627C2F690DF710

View File

@ -37,14 +37,13 @@
//! - A `debug` macro that works like `std::dbg` but for multiple threads. (see `dbg` module) //! - A `debug` macro that works like `std::dbg` but for multiple threads. (see `dbg` module)
#[macro_use] #[macro_use]
pub mod dbg { pub mod dbg {
#[allow(clippy::redundant_closure)]
#[macro_export] #[macro_export]
macro_rules! debug { macro_rules! log_tag {
($val:literal) => { () => {
{
if cfg!(feature="debug-tracing") {
eprint!( eprint!(
"[{:?}] {}:{}_{}: ", "[{}][{:?}] {}:{}_{}: ",
crate::datetime::timestamp_to_string(crate::datetime::now(), Some("%Y-%m-%d %T")),
std::thread::current() std::thread::current()
.name() .name()
.map(std::string::ToString::to_string) .map(std::string::ToString::to_string)
@ -53,6 +52,16 @@ pub mod dbg {
line!(), line!(),
column!() column!()
); );
};
}
#[allow(clippy::redundant_closure)]
#[macro_export]
macro_rules! debug {
($val:literal) => {
{
if cfg!(feature="debug-tracing") {
log_tag!();
eprintln!($val); eprintln!($val);
} }
$val $val
@ -65,16 +74,7 @@ pub mod dbg {
// of temporaries - https://stackoverflow.com/a/48732525/1063961 // of temporaries - https://stackoverflow.com/a/48732525/1063961
match $val { match $val {
tmp => { tmp => {
eprint!( log_tag!();
"[{:?}] {}:{}_{}: ",
std::thread::current()
.name()
.map(std::string::ToString::to_string)
.unwrap_or_else(|| format!("{:?}", std::thread::current().id())),
file!(),
line!(),
column!()
);
eprintln!("{} = {:?}", stringify, tmp); eprintln!("{} = {:?}", stringify, tmp);
tmp tmp
} }
@ -85,16 +85,7 @@ pub mod dbg {
}; };
($fmt:literal, $($arg:tt)*) => { ($fmt:literal, $($arg:tt)*) => {
if cfg!(feature="debug-tracing") { if cfg!(feature="debug-tracing") {
eprint!( log_tag!();
"[{:?}] {}:{}_{}: ",
std::thread::current()
.name()
.map(std::string::ToString::to_string)
.unwrap_or_else(|| format!("{:?}", std::thread::current().id())),
file!(),
line!(),
column!()
);
eprintln!($fmt, $($arg)*); eprintln!($fmt, $($arg)*);
} }
}; };