Skip to content

Commit c626f82

Browse files
committed
Support static string as key value
1 parent 21e30fc commit c626f82

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/macros.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
#[macro_export(local_inner_macros)]
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:ident = $value:expr),+; $($arg:tt)+) => ({
33+
(target: $target:expr, $lvl:expr, $($key: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
__log_format_args!($($arg)+),
3838
lvl,
3939
&($target, __log_module_path!(), __log_file!(), __log_line!()),
40-
Some(&[$((__log_stringify!($key), &$value)),+])
40+
Some(&[$((__log_key!($key), &$value)),+])
4141
);
4242
}
4343
});
@@ -268,8 +268,13 @@ macro_rules! __log_line {
268268

269269
#[doc(hidden)]
270270
#[macro_export]
271-
macro_rules! __log_stringify {
272-
($($args:tt)*) => {
271+
macro_rules! __log_key {
272+
// key1 = 42
273+
($($args:ident)*) => {
273274
stringify!($($args)*)
274275
};
276+
// "key1" = 42
277+
($($args:expr)*) => {
278+
$($args)*
279+
};
275280
}

tests/macros.rs

+10
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,13 @@ fn kv_implicit_named_args() {
205205
all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}");
206206
}
207207
}
208+
209+
#[test]
210+
#[cfg(feature = "kv_unstable")]
211+
fn kv_string_keys() {
212+
for lvl in log::Level::iter() {
213+
log!(target: "my_target", lvl, "also dogs" = "Fílos", "key/that-can't/be/an/ident" = "hi"; "hello {world}", world = "world");
214+
}
215+
216+
all_log_macros!(target: "my_target", "also dogs" = "Fílos", "key/that-can't/be/an/ident" = "hi"; "hello {world}", world = "world");
217+
}

0 commit comments

Comments
 (0)