Skip to content

Commit

Permalink
Group arguments that are likely constant
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 26, 2023
1 parent cc2a3e0 commit 6c1a48a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
42 changes: 20 additions & 22 deletions src/__private_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ impl<'a> KVs<'a> for () {
// Log implementation.

fn log_impl(
args: Arguments,
level: Level,
&(target, module_path, file): &(&str, &'static str, &'static str),
fmt_args: Arguments,
const_args: &LikelyConstantArgs,
line: u32,
kvs: Option<&[(&str, &Value)]>,
) {
Expand All @@ -53,11 +52,11 @@ fn log_impl(
let mut builder = Record::builder();

builder
.args(args)
.level(level)
.target(target)
.module_path_static(Some(module_path))
.file_static(Some(file))
.args(fmt_args)
.level(const_args.level)
.target(const_args.target)
.module_path_static(Some(const_args.module_path))
.file_static(Some(const_args.file))
.line(Some(line));

#[cfg(feature = "kv_unstable")]
Expand All @@ -66,22 +65,21 @@ fn log_impl(
crate::logger().log(&builder.build());
}

pub fn log<'a, K>(
args: Arguments,
level: Level,
target_module_path_and_file: &(&str, &'static str, &'static str),
line: u32,
kvs: K,
) where
// Group arguments that are likely constant together so that the compiler can reuse these arguments
// between different calls.
#[derive(Debug)]
pub struct LikelyConstantArgs<'a> {
pub level: Level,
pub target: &'a str,
pub module_path: &'static str,
pub file: &'static str,
}

pub fn log<'a, K>(fmt_args: Arguments, const_args: &LikelyConstantArgs, line: u32, kvs: K)
where
K: KVs<'a>,
{
log_impl(
args,
level,
target_module_path_and_file,
line,
kvs.into_kvs(),
)
log_impl(fmt_args, const_args, line, kvs.into_kvs())
}

pub fn enabled(level: Level, target: &str) -> bool {
Expand Down
16 changes: 12 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ macro_rules! log {
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::__private_api::log::<&_>(
$crate::__private_api::format_args!($($arg)+),
lvl,
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
&$crate::__private_api::LikelyConstantArgs {
level: lvl,
target: $target,
module_path: $crate::__private_api::module_path!(),
file: $crate::__private_api::file!(),
},
$crate::__private_api::line!(),
&[$(($crate::__log_key!($key), &$value)),+]
);
Expand All @@ -49,8 +53,12 @@ macro_rules! log {
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::__private_api::log(
$crate::__private_api::format_args!($($arg)+),
lvl,
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
&$crate::__private_api::LikelyConstantArgs {
level: lvl,
target: $target,
module_path: $crate::__private_api::module_path!(),
file: $crate::__private_api::file!(),
},
$crate::__private_api::line!(),
(),
);
Expand Down

0 comments on commit 6c1a48a

Please sign in to comment.