Support static string as key value
This commit is contained in:
Ashley Mannix 2022-03-03 08:03:56 +10:00 committed by GitHub
commit 863c4612c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View File

@ -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)),+])
);
}
});
@ -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)*
};
}

View File

@ -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");
}