Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 61 additions & 29 deletions src/__private_api.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,87 @@
//! WARNING: this is not part of the crate's public API and is subject to change at any time

use self::sealed::KVs;
use crate::{Level, Metadata, Record};
use std::fmt::Arguments;
pub use std::option::Option;
pub use std::{file, format_args, line, module_path, stringify};

#[cfg(feature = "kv_unstable")]
pub type Value<'a> = dyn crate::kv::value::ToValue + 'a;

#[cfg(not(feature = "kv_unstable"))]
pub fn log(
pub type Value<'a> = str;

mod sealed {
/// Types for the `kv` argument.
pub trait KVs<'a> {
fn into_kvs(self) -> Option<&'a [(&'a str, &'a super::Value<'a>)]>;
Comment thread
EFanZh marked this conversation as resolved.
}
}

// Types for the `kv` argument.

impl<'a> KVs<'a> for &'a [(&'a str, &'a Value<'a>)] {
#[inline]
fn into_kvs(self) -> Option<&'a [(&'a str, &'a Value<'a>)]> {
Some(self)
}
}

impl<'a> KVs<'a> for () {
#[inline]
fn into_kvs(self) -> Option<&'a [(&'a str, &'a Value<'a>)]> {
None
}
}

// Log implementation.

fn log_impl(
args: Arguments,
level: Level,
&(target, module_path, file): &(&str, &'static str, &'static str),
line: u32,
kvs: Option<&[(&str, &str)]>,
kvs: Option<&[(&str, &Value)]>,
) {
#[cfg(not(feature = "kv_unstable"))]
if kvs.is_some() {
panic!(
"key-value support is experimental and must be enabled using the `kv_unstable` feature"
)
}

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

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

#[cfg(feature = "kv_unstable")]
builder.key_values(&kvs);

crate::logger().log(&builder.build());
}

#[cfg(feature = "kv_unstable")]
pub fn log(
pub fn log<'a, K>(
args: Arguments,
level: Level,
&(target, module_path, file): &(&str, &'static str, &'static str),
target_module_path_and_file: &(&str, &'static str, &'static str),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides, does this change affect significantly? Or just some identical refactor.

line: u32,
kvs: Option<&[(&str, &dyn crate::kv::ToValue)]>,
) {
crate::logger().log(
&Record::builder()
.args(args)
.level(level)
.target(target)
.module_path_static(Some(module_path))
.file_static(Some(file))
.line(Some(line))
.key_values(&kvs)
.build(),
);
kvs: K,
) where
K: KVs<'a>,
{
log_impl(
args,
level,
target_module_path_and_file,
line,
kvs.into_kvs(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EFanZh we still pass None::<&[(&str, &Value)]> here.

Could you elaborate a bit on how moving the type convert one level down can help?

I'm trying to understand how the extra abstract works (and avoid breaking it in any following patch).

Copy link
Copy Markdown
Contributor Author

@EFanZh EFanZh Mar 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes the log function is not inlined, which allows callers to pass ZST to the log function as kvs argument, which does not generate machine instructions at the caller side. There might be a lot of callers in a program, but only one callee function, so I would like to reduce the machine instructions at caller side as much as possible in order to reduce the binary size.

)
}

pub fn enabled(level: Level, target: &str) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ macro_rules! log {
(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(
$crate::__private_api::log::<&_>(
$crate::__private_api::format_args!($($arg)+),
lvl,
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
$crate::__private_api::line!(),
$crate::__private_api::Option::Some(&[$(($crate::__log_key!($key), &$value)),+])
&[$(($crate::__log_key!($key), &$value)),+]
);
}
});
Expand All @@ -52,7 +52,7 @@ macro_rules! log {
lvl,
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
$crate::__private_api::line!(),
$crate::__private_api::Option::None,
(),
);
}
});
Expand Down