Skip to content

Commit

Permalink
refactor: simplify warn! and error! macros (#2503)
Browse files Browse the repository at this point in the history
* refactor: simplify the error! and warn! macros

Signed-off-by: Ruihang Xia <[email protected]>

* support display format

Signed-off-by: Ruihang Xia <[email protected]>

* err.msg to err

Signed-off-by: Ruihang Xia <[email protected]>

---------

Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia authored Sep 27, 2023
1 parent db6ceda commit fbe2f2d
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/common/telemetry/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,38 @@ macro_rules! error {

// error!(e; target: "my_target", "a {} event", "log")
($e:expr; target: $target:expr, $($arg:tt)+) => ({
use $crate::common_error::ext::ErrorExt;
use $crate::common_error::ext::StackError;

let mut stacks = vec![];
$e.debug_fmt(0, &mut stacks);
let stacks = stacks.join("\n");
$crate::log!(
target: $target,
$crate::logging::Level::ERROR,
err = ?$e,
$($arg)+
)
});

// error!(%e; target: "my_target", "a {} event", "log")
(%$e:expr; target: $target:expr, $($arg:tt)+) => ({
$crate::log!(
target: $target,
$crate::logging::Level::ERROR,
err.msg = %stacks,
err.code = %$e.status_code(),
err = %$e,
$($arg)+
)
});

// error!(e; "a {} event", "log")
($e:expr; $($arg:tt)+) => ({
use $crate::common_error::ext::ErrorExt;
use $crate::common_error::ext::StackError;

let mut stacks = vec![];
$e.debug_fmt(0, &mut stacks);
let stacks = stacks.join("\n");
$crate::log!(
$crate::logging::Level::ERROR,
err = ?$e,
$($arg)+
)
});

// error!(%e; "a {} event", "log")
(%$e:expr; $($arg:tt)+) => ({
$crate::log!(
$crate::logging::Level::ERROR,
err.msg = %stacks,
err.code = %$e.status_code(),
err = %$e,
$($arg)+
)
});
Expand All @@ -87,17 +90,18 @@ macro_rules! warn {

// warn!(e; "a {} event", "log")
($e:expr; $($arg:tt)+) => ({
use $crate::common_error::ext::ErrorExt;
use $crate::common_error::ext::StackError;

let mut stacks = vec![];
$e.debug_fmt(0, &mut stacks);
let stacks = stacks.join("\n");
$crate::log!(
$crate::logging::Level::WARN,
err = ?$e,
$($arg)+
)
});

// warn!(%e; "a {} event", "log")
(%$e:expr; $($arg:tt)+) => ({
$crate::log!(
$crate::logging::Level::WARN,
err.msg = %$e,
err.code = %$e.status_code(),
err = %$e,
$($arg)+
)
});
Expand Down Expand Up @@ -221,8 +225,10 @@ mod tests {
error!(target: "my_target", "hello {}", "world");
// Supports both owned and reference type.
error!(err; target: "my_target", "hello {}", "world");
error!(%err; target: "my_target", "hello {}", "world");
error!(err_ref; target: "my_target", "hello {}", "world");
error!(err_ref2; "hello {}", "world");
error!(%err_ref2; "hello {}", "world");
error!("hello {}", "world");

let root_err = MockError::with_source(err);
Expand Down

0 comments on commit fbe2f2d

Please sign in to comment.