Skip to content

Commit

Permalink
Merge pull request #486 from Thomasdezeeuw/issue#484
Browse files Browse the repository at this point in the history
Support static string as key value
  • Loading branch information
KodrAus committed Mar 2, 2022
2 parents 21e30fc + c626f82 commit 863c461
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/macros.rs
Expand Up @@ -30,14 +30,14 @@
#[macro_export(local_inner_macros)]
macro_rules! log {
// log!(target: "my_target", Level::Info; key1 = 42, key2 = true; "a {} event", "log");
(target: $target:expr, $lvl:expr, $($key:ident = $value:expr),+; $($arg:tt)+) => ({
(target: $target:expr, $lvl:expr, $($key:tt = $value:expr),+; $($arg:tt)+) => ({
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::__private_api_log(
__log_format_args!($($arg)+),
lvl,
&($target, __log_module_path!(), __log_file!(), __log_line!()),
Some(&[$((__log_stringify!($key), &$value)),+])
Some(&[$((__log_key!($key), &$value)),+])
);
}
});
Expand Down Expand Up @@ -268,8 +268,13 @@ macro_rules! __log_line {

#[doc(hidden)]
#[macro_export]
macro_rules! __log_stringify {
($($args:tt)*) => {
macro_rules! __log_key {
// key1 = 42
($($args:ident)*) => {
stringify!($($args)*)
};
// "key1" = 42
($($args:expr)*) => {
$($args)*
};
}
10 changes: 10 additions & 0 deletions tests/macros.rs
Expand Up @@ -205,3 +205,13 @@ fn kv_implicit_named_args() {
all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}");
}
}

#[test]
#[cfg(feature = "kv_unstable")]
fn kv_string_keys() {
for lvl in log::Level::iter() {
log!(target: "my_target", lvl, "also dogs" = "Fílos", "key/that-can't/be/an/ident" = "hi"; "hello {world}", world = "world");
}

all_log_macros!(target: "my_target", "also dogs" = "Fílos", "key/that-can't/be/an/ident" = "hi"; "hello {world}", world = "world");
}

0 comments on commit 863c461

Please sign in to comment.