veilid/veilid-core/src/xx/log_thru.rs

184 lines
3.9 KiB
Rust
Raw Normal View History

2021-12-18 00:18:25 +00:00
pub use alloc::string::{String, ToString};
2021-12-17 02:57:28 +00:00
pub fn map_to_string<X: ToString>(arg: X) -> String {
arg.to_string()
}
/*
trait LogThru<T, E> {
fn log_thru<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T, F>;
}
impl<T, E> LogThru<T, E> for Result<T, E> {
fn log_thru<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T, F> {
match self {
Ok(t) => Ok(t),
Err(e) => Err(op(e)),
}
}
}
*/
#[macro_export]
macro_rules! fn_string {
($text:expr) => {
|| $text.to_string()
};
}
#[macro_export]
macro_rules! log_net {
(error $text:expr) => {error!(
target: "net",
"{}",
$text,
)};
2021-12-18 00:18:25 +00:00
(error $fmt:literal, $($arg:expr),+) => {
error!(target:"net", $fmt, $($arg),+);
2021-12-17 02:57:28 +00:00
};
($text:expr) => {trace!(
target: "net",
"{}",
$text,
)};
2021-12-18 00:18:25 +00:00
($fmt:literal, $($arg:expr),+) => {
trace!(target:"net", $fmt, $($arg),+);
2021-12-17 02:57:28 +00:00
}
}
#[macro_export]
macro_rules! log_rpc {
(error $text:expr) => { error!(
target: "rpc",
"{}",
$text,
)};
2021-12-18 00:18:25 +00:00
(error $fmt:literal, $($arg:expr),+) => {
error!(target:"rpc", $fmt, $($arg),+);
2021-12-17 02:57:28 +00:00
};
($text:expr) => {trace!(
target: "rpc",
"{}",
$text,
)};
2021-12-18 00:18:25 +00:00
($fmt:literal, $($arg:expr),+) => {
trace!(target:"rpc", $fmt, $($arg),+);
2021-12-17 02:57:28 +00:00
}
}
#[macro_export]
macro_rules! log_rtab {
(error $text:expr) => { error!(
target: "rtab",
"{}",
$text,
)};
2021-12-18 00:18:25 +00:00
(error $fmt:literal, $($arg:expr),+) => {
error!(target:"rtab", $fmt, $($arg),+);
2021-12-17 02:57:28 +00:00
};
($text:expr) => {trace!(
target: "rtab",
"{}",
$text,
)};
2021-12-18 00:18:25 +00:00
($fmt:literal, $($arg:expr),+) => {
trace!(target:"rtab", $fmt, $($arg),+);
2021-12-17 02:57:28 +00:00
}
}
#[macro_export]
macro_rules! logthru_net {
($($level:ident)?) => {
logthru!($($level)? "net")
};
($($level:ident)? $text:literal) => {
logthru!($($level)? "net", $text)
};
2021-12-18 00:18:25 +00:00
($($level:ident)? $fmt:literal, $($arg:expr),+) => {
logthru!($($level)? "net", $fmt, $($arg),+)
2021-12-17 02:57:28 +00:00
}
}
#[macro_export]
macro_rules! logthru_rpc {
($($level:ident)?) => {
logthru!($($level)? "rpc")
};
($($level:ident)? $text:literal) => {
logthru!($($level)? "rpc", $text)
};
2021-12-18 00:18:25 +00:00
($($level:ident)? $fmt:literal, $($arg:expr),+) => {
logthru!($($level)? "rpc", $fmt, $($arg),+)
2021-12-17 02:57:28 +00:00
}
}
#[macro_export]
macro_rules! logthru_rtab {
($($level:ident)?) => {
logthru!($($level)? "rtab")
};
($($level:ident)? $text:literal) => {
logthru!($($level)? "rtab", $text)
};
2021-12-18 00:18:25 +00:00
($($level:ident)? $fmt:literal, $($arg:expr),+) => {
logthru!($($level)? "rtab", $fmt, $($arg),+)
2021-12-17 02:57:28 +00:00
}
}
#[macro_export]
macro_rules! logthru {
// error
(error $target:literal) => (|e__| {
error!(
target: $target,
"[{}]",
e__,
);
e__
});
(error $target:literal, $text:literal) => (|e__| {
error!(
target: $target,
"[{}] {}",
e__,
$text
);
e__
});
2021-12-18 00:18:25 +00:00
(error $target:literal, $fmt:literal, $($arg:expr),+) => (|e__| {
2021-12-17 02:57:28 +00:00
error!(
target: $target,
concat!("[{}] ", $fmt),
e__,
2021-12-18 00:18:25 +00:00
$($arg),+
2021-12-17 02:57:28 +00:00
);
e__
});
// trace
($target:literal) => (|e__| {
trace!(
target: $target,
"[{}]",
e__,
);
e__
});
($target:literal, $text:literal) => (|e__| {
trace!(
target: $target,
"[{}] {}",
e__,
$text
);
e__
});
2021-12-18 00:18:25 +00:00
($target:literal, $fmt:literal, $($arg:expr),+) => (|e__| {
2021-12-17 02:57:28 +00:00
trace!(
target: $target,
concat!("[{}] ", $fmt),
e__,
2021-12-18 00:18:25 +00:00
$($arg),+
2021-12-17 02:57:28 +00:00
);
e__
})
}