Skip to content

Commit

Permalink
support field shorthand in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Feb 18, 2024
1 parent 90a347b commit ad91711
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn shave_the_yak(yak: &mut Yak) {
break;
}
Err(err) => {
warn!(err:error = err; "Unable to locate a razor, retrying");
warn!(err:err; "Unable to locate a razor, retrying");
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
//! info!(a = 1; "Something of interest");
//! ```
//!
//! Key-values support the same shorthand identifer syntax as `format_args`:
//!
//! ```
//! # use log::info;
//! let a = 1;
//!
//! info!(a; "Something of interest");
//! ```
//!
//! Values are capturing using the [`ToValue`] trait by default. To capture a value
//! using a different trait implementation, use a modifier after its key. Here's how
//! the same example can capture `a` using its `Debug` implementation instead:
Expand All @@ -44,7 +53,7 @@
//! - `:debug` will capture the value using `Debug`.
//! - `:%` will capture the value using `Display`.
//! - `:display` will capture the value using `Display`.
//! - `:error` will capture the value using `std::error::Error` (requires the `kv_std` feature).
//! - `:err` will capture the value using `std::error::Error` (requires the `kv_std` feature).
//! - `:sval` will capture the value using `sval::Value` (requires the `kv_sval` feature).
//! - `:serde` will capture the value using `serde::Serialize` (requires the `kv_serde` feature).
//!
Expand Down
2 changes: 1 addition & 1 deletion src/kv/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ macro_rules! as_display {

/// Get a value from an error.
#[cfg(feature = "kv_unstable_std")]
#[deprecated(note = "use the `key:error = value` macro syntax instead")]
#[deprecated(note = "use the `key:err = value` macro syntax instead")]
#[macro_export]
macro_rules! as_error {
($capture:expr) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
//! break;
//! }
//! Err(err) => {
//! warn!(err:error = err; "Unable to locate a razor, retrying");
//! warn!(err:err; "Unable to locate a razor, retrying");
//! }
//! }
//! }
Expand Down
17 changes: 13 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
#[macro_export]
macro_rules! log {
// log!(target: "my_target", Level::Info, key1:? = 42, key2 = true; "a {} event", "log");
(target: $target:expr, $lvl:expr, $($key:tt $(:$capture:tt)? = $value:expr),+; $($arg:tt)+) => ({
(target: $target:expr, $lvl:expr, $($key:tt $(:$capture:tt)? $(= $value:expr)?),+; $($arg:tt)+) => ({
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::__private_api::log::<&_>(
$crate::__private_api::format_args!($($arg)+),
lvl,
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
$crate::__private_api::line!(),
&[$(($crate::__log_key!($key), $crate::__log_value!(($value)$(:$capture)*))),+]
&[$(($crate::__log_key!($key), $crate::__log_value!($key $(:$capture)* = $($value)*))),+]
);
}
});
Expand Down Expand Up @@ -256,10 +256,19 @@ macro_rules! __log_key {
#[macro_export]
#[cfg(feature = "kv")]
macro_rules! __log_value {
// Default
(($args:expr)) => {
// Entrypoint
($key:tt = $args:expr) => {
$crate::__log_value!(($args):value)
};
($key:tt :$capture:tt = $args:expr) => {
$crate::__log_value!(($args):$capture)
};
($key:ident =) => {
$crate::__log_value!(($key):value)
};
($key:ident :$capture:tt =) => {
$crate::__log_value!(($key):$capture)
};
// ToValue
(($args:expr):value) => {
$crate::__private_api::capture_to_value(&&$args)
Expand Down
11 changes: 10 additions & 1 deletion tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ fn kv_named_args() {
all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world");
}

#[test]
#[cfg(feature = "kv")]
fn kv_ident() {
let cat_1 = "chashu";
let cat_2 = "nori";

all_log_macros!(cat_1, cat_2:%, cat_count = 2; "hello {world}", world = "world");
}

#[test]
#[cfg(feature = "kv")]
fn kv_expr_context() {
Expand Down Expand Up @@ -274,7 +283,7 @@ fn kv_display() {
#[cfg(feature = "kv_std")]
fn kv_error() {
all_log_macros!(
a:error = std::io::Error::new(std::io::ErrorKind::Other, "an error");
a:err = std::io::Error::new(std::io::ErrorKind::Other, "an error");
"hello world"
);
}
Expand Down

0 comments on commit ad91711

Please sign in to comment.