Skip to content

Commit ad91711

Browse files
committed
support field shorthand in macros
1 parent 90a347b commit ad91711

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn shave_the_yak(yak: &mut Yak) {
116116
break;
117117
}
118118
Err(err) => {
119-
warn!(err:error = err; "Unable to locate a razor, retrying");
119+
warn!(err:err; "Unable to locate a razor, retrying");
120120
}
121121
}
122122
}

src/kv/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
//! info!(a = 1; "Something of interest");
3030
//! ```
3131
//!
32+
//! Key-values support the same shorthand identifer syntax as `format_args`:
33+
//!
34+
//! ```
35+
//! # use log::info;
36+
//! let a = 1;
37+
//!
38+
//! info!(a; "Something of interest");
39+
//! ```
40+
//!
3241
//! Values are capturing using the [`ToValue`] trait by default. To capture a value
3342
//! using a different trait implementation, use a modifier after its key. Here's how
3443
//! the same example can capture `a` using its `Debug` implementation instead:
@@ -44,7 +53,7 @@
4453
//! - `:debug` will capture the value using `Debug`.
4554
//! - `:%` will capture the value using `Display`.
4655
//! - `:display` will capture the value using `Display`.
47-
//! - `:error` will capture the value using `std::error::Error` (requires the `kv_std` feature).
56+
//! - `:err` will capture the value using `std::error::Error` (requires the `kv_std` feature).
4857
//! - `:sval` will capture the value using `sval::Value` (requires the `kv_sval` feature).
4958
//! - `:serde` will capture the value using `serde::Serialize` (requires the `kv_serde` feature).
5059
//!

src/kv/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ macro_rules! as_display {
11421142

11431143
/// Get a value from an error.
11441144
#[cfg(feature = "kv_unstable_std")]
1145-
#[deprecated(note = "use the `key:error = value` macro syntax instead")]
1145+
#[deprecated(note = "use the `key:err = value` macro syntax instead")]
11461146
#[macro_export]
11471147
macro_rules! as_error {
11481148
($capture:expr) => {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
//! break;
113113
//! }
114114
//! Err(err) => {
115-
//! warn!(err:error = err; "Unable to locate a razor, retrying");
115+
//! warn!(err:err; "Unable to locate a razor, retrying");
116116
//! }
117117
//! }
118118
//! }

src/macros.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
#[macro_export]
3131
macro_rules! log {
3232
// log!(target: "my_target", Level::Info, key1:? = 42, key2 = true; "a {} event", "log");
33-
(target: $target:expr, $lvl:expr, $($key:tt $(:$capture:tt)? = $value:expr),+; $($arg:tt)+) => ({
33+
(target: $target:expr, $lvl:expr, $($key:tt $(:$capture:tt)? $(= $value:expr)?),+; $($arg:tt)+) => ({
3434
let lvl = $lvl;
3535
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
3636
$crate::__private_api::log::<&_>(
3737
$crate::__private_api::format_args!($($arg)+),
3838
lvl,
3939
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
4040
$crate::__private_api::line!(),
41-
&[$(($crate::__log_key!($key), $crate::__log_value!(($value)$(:$capture)*))),+]
41+
&[$(($crate::__log_key!($key), $crate::__log_value!($key $(:$capture)* = $($value)*))),+]
4242
);
4343
}
4444
});
@@ -256,10 +256,19 @@ macro_rules! __log_key {
256256
#[macro_export]
257257
#[cfg(feature = "kv")]
258258
macro_rules! __log_value {
259-
// Default
260-
(($args:expr)) => {
259+
// Entrypoint
260+
($key:tt = $args:expr) => {
261261
$crate::__log_value!(($args):value)
262262
};
263+
($key:tt :$capture:tt = $args:expr) => {
264+
$crate::__log_value!(($args):$capture)
265+
};
266+
($key:ident =) => {
267+
$crate::__log_value!(($key):value)
268+
};
269+
($key:ident :$capture:tt =) => {
270+
$crate::__log_value!(($key):$capture)
271+
};
263272
// ToValue
264273
(($args:expr):value) => {
265274
$crate::__private_api::capture_to_value(&&$args)

tests/macros.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ fn kv_named_args() {
165165
all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world");
166166
}
167167

168+
#[test]
169+
#[cfg(feature = "kv")]
170+
fn kv_ident() {
171+
let cat_1 = "chashu";
172+
let cat_2 = "nori";
173+
174+
all_log_macros!(cat_1, cat_2:%, cat_count = 2; "hello {world}", world = "world");
175+
}
176+
168177
#[test]
169178
#[cfg(feature = "kv")]
170179
fn kv_expr_context() {
@@ -274,7 +283,7 @@ fn kv_display() {
274283
#[cfg(feature = "kv_std")]
275284
fn kv_error() {
276285
all_log_macros!(
277-
a:error = std::io::Error::new(std::io::ErrorKind::Other, "an error");
286+
a:err = std::io::Error::new(std::io::ErrorKind::Other, "an error");
278287
"hello world"
279288
);
280289
}

0 commit comments

Comments
 (0)