Skip to content

Commit

Permalink
Merge pull request #374 from KodrAus/fix/kv_macro
Browse files Browse the repository at this point in the history
Revert the macro implementation
  • Loading branch information
KodrAus authored Dec 19, 2019
2 parents 1c73af5 + c3a9a14 commit f06a18d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 106 deletions.
30 changes: 0 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,39 +1365,10 @@ pub fn logger() -> &'static dyn Log {

// WARNING: this is not part of the crate's public API and is subject to change at any time
#[doc(hidden)]
#[cfg(not(feature = "kv_unstable"))]
pub fn __private_api_log(
args: fmt::Arguments,
level: Level,
&(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
kvs: Option<&[(&str, &str)]>,
) {
if kvs.is_some() {
panic!(
"key-value support is experimental and must be enabled using the `kv_unstable` feature"
)
}

logger().log(
&Record::builder()
.args(args)
.level(level)
.target(target)
.module_path_static(Some(module_path))
.file_static(Some(file))
.line(Some(line))
.build(),
);
}

// WARNING: this is not part of the crate's public API and is subject to change at any time
#[cfg(feature = "kv_unstable")]
#[doc(hidden)]
pub fn __private_api_log(
args: fmt::Arguments<'_>,
level: Level,
&(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
kvs: Option<&[(&str, &dyn kv::ToValue)]>,
) {
logger().log(
&Record::builder()
Expand All @@ -1407,7 +1378,6 @@ pub fn __private_api_log(
.module_path_static(Some(module_path))
.file_static(Some(file))
.line(Some(line))
.key_values(&kvs)
.build(),
);
}
Expand Down
70 changes: 8 additions & 62 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,75 +29,29 @@
/// ```
#[macro_export(local_inner_macros)]
macro_rules! log {
// log!(target: "...", "...")
(target: $target:expr, $lvl:expr, $e:expr) => (
$crate::log_impl!(target: $target, $lvl, ($e));
);

// log!(target: "...", "...", args...)
(target: $target:expr, $lvl:expr, $e:expr, $($rest:tt)*) => (
$crate::log_impl!(target: $target, $lvl, ($e) $($rest)*);
);

// log!("...", args...)
($lvl:expr, $($arg:tt)+) => (
$crate::log!(target: __log_module_path!(), $lvl, $($arg)+);
)
}

#[macro_export(local_inner_macros)]
#[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() {
// ensure that $message is a valid format string literal
let _ = __log_format_args!($message);
$crate::__private_api_log_lit(
$message,
lvl,
&($target, __log_module_path!(), __log_file!(), __log_line!()),
);
}
}};
(target: $target:expr, $lvl:expr, ($($arg:expr),*)) => {{
});
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::__private_api_log(
__log_format_args!($($arg),*),
__log_format_args!($($arg)+),
lvl,
&($target, __log_module_path!(), __log_file!(), __log_line!()),
None,
);
}
}};

// // Trailing k-v pairs containing no trailing comma
(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(
__log_format_args!($($arg),*),
lvl,
&(__log_module_path!(), __log_module_path!(), __log_file!(), __log_line!()),
Some(&[$((__log_stringify!($key), &$value)),*])
);
}
}};

// Trailing k-v pairs with trailing comma
(target: $target:expr, $lvl:expr, ($($e:expr),*) { $($key:ident : $value:expr,)* }) => (
$crate::log_impl!(target: $target, $lvl, ($($e),*) { $($key : $value),* });
);

// Last expression arg with no trailing comma
(target: $target:expr, $lvl:expr, ($($e:expr),*) $arg:expr) => (
$crate::log_impl!(target: $target, $lvl, ($($e,)* $arg));
);

// Expression arg
(target: $target:expr, $lvl:expr, ($($e:expr),*) $arg:expr, $($rest:tt)*) => (
$crate::log_impl!(target: $target, $lvl, ($($e,)* $arg) $($rest)*);
)
});
($lvl:expr, $($arg:tt)+) => (log!(target: __log_module_path!(), $lvl, $($arg)+))
}

/// Logs a message at the error level.
Expand Down Expand Up @@ -306,11 +260,3 @@ macro_rules! __log_line {
line!()
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __log_stringify {
($($args:tt)*) => {
stringify!($($args)*)
};
}
19 changes: 5 additions & 14 deletions tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,10 @@ fn with_args_expr_context() {
}

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

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

0 comments on commit f06a18d

Please sign in to comment.