#[macro_export]
macro_rules! function {
() => {{
fn f() {}
fn type_name_of<T>(_: T) -> &'static str {
std::any::type_name::<T>()
}
let name = type_name_of(f); &name[..name.len() - 3] }};
}
#[macro_export]
macro_rules! loc {
() => {
$crate::LogLocation {
file_path: file!().to_string(),
func_path: function!().to_string(),
lineno: line!(),
thread_id: std::thread::current().id(),
}
};
}
#[macro_export]
macro_rules! q {
() => {
$crate::LOGGER.write().unwrap().q(loc!());
};
($x:literal) => {{
let val = $x;
$crate::LOGGER.write().unwrap().q_literal(&val, loc!());
val
}};
($x:expr) => {{
let val = $x;
$crate::LOGGER
.write()
.unwrap()
.q_expr(&val, stringify!($x), loc!());
val
}};
}
#[cfg(test)]
mod tests {
#[test]
fn test_function() {
assert_eq!(function!(), "q_debug::macros::tests::test_function");
struct Foo {
bar: String,
}
impl Foo {
pub fn new() -> Self {
Foo {
bar: function!().to_string(),
}
}
}
assert_eq!(
Foo::new().bar,
"q_debug::macros::tests::test_function::Foo::new"
);
}
}