Skip to content

Commit

Permalink
Merge pull request #370 from KodrAus/fix/macro-expr-context
Browse files Browse the repository at this point in the history
fix up macro use in expr context
  • Loading branch information
KodrAus authored Dec 16, 2019
2 parents 87fc152 + 1a7ffda commit 62c0975
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ macro_rules! log {
#[doc(hidden)]
macro_rules! log_impl {
// End of macro input
(target: $target:expr, $lvl:expr, ($message:expr)) => {
(target: $target:expr, $lvl:expr, ($message:expr)) => {{
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::__private_api_log_lit(
Expand All @@ -56,9 +56,8 @@ macro_rules! log_impl {
&($target, __log_module_path!(), __log_file!(), __log_line!()),
);
}
};

(target: $target:expr, $lvl:expr, ($($arg:expr),*)) => {
}};
(target: $target:expr, $lvl:expr, ($($arg:expr),*)) => {{
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::__private_api_log(
Expand All @@ -68,10 +67,10 @@ macro_rules! log_impl {
None,
);
}
};
}};

// // Trailing k-v pairs containing no trailing comma
(target: $target:expr, $lvl:expr, ($($arg:expr),*) { $($key:ident : $value:expr),* }) => {
(target: $target:expr, $lvl:expr, ($($arg:expr),*) { $($key:ident : $value:expr),* }) => {{
let lvl = log::Level::Info;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::__private_api_log(
Expand All @@ -81,7 +80,7 @@ macro_rules! log_impl {
Some(&[$((__log_stringify!($key), &$value)),*])
);
}
};
}};

// Trailing k-v pairs with trailing comma
(target: $target:expr, $lvl:expr, ($($e:expr),*) { $($key:ident : $value:expr,)* }) => {
Expand Down
22 changes: 22 additions & 0 deletions tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,39 @@ fn base() {
info!("hello",);
}

#[test]
fn base_expr_context() {
let _ = info!("hello");
}

#[test]
fn with_args() {
info!("hello {}", "cats");
info!("hello {}", "cats",);
info!("hello {}", "cats",);
}

#[test]
fn with_args_expr_context() {
match "cats" {
cats => info!("hello {}", cats),
};
}

#[test]
fn kv() {
info!("hello {}", "cats", {
cat_1: "chashu",
cat_2: "nori",
});
}

#[test]
fn kv_expr_context() {
match "chashu" {
cat_1 => info!("hello {}", "cats", {
cat_1: cat_1,
cat_2: "nori",
}),
};
}

0 comments on commit 62c0975

Please sign in to comment.