Skip to content

Commit

Permalink
Specialize target and level arguments that can be statically determined
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 5, 2023
1 parent b834897 commit b54a3b7
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 124 deletions.
279 changes: 190 additions & 89 deletions src/__private_api.rs
Original file line number Diff line number Diff line change
@@ -1,89 +1,190 @@
//! 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::{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 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>)]>;
}
}

// 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, &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"
)
}

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());
}

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

pub fn enabled(level: Level, target: &str) -> bool {
crate::logger().enabled(&Metadata::builder().level(level).target(target).build())
}
//! WARNING: this is not part of the crate's public API and is subject to change at any time
use self::sealed::{KVs, Level as LevelTrait, Target};
use crate::{Level, LevelFilter, Metadata, Record};
use std::cmp::Ordering;
use std::fmt::Arguments;
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 type Value<'a> = str;

mod sealed {
/// Types for the `target` argument.
pub trait Target<'a>: Copy {
fn into_target(self, module_path: &'static str) -> &'a str;
}

/// Types for the `level` argument.
pub trait Level: Copy {
fn into_level(self) -> crate::Level;
}

/// Types for the `kv` argument.
pub trait KVs<'a> {
fn into_kvs(self) -> Option<&'a [(&'a str, &'a super::Value<'a>)]>;
}
}

// Types for the `target` argument.

/// Caller specified target explicitly.
impl<'a> Target<'a> for &'a str {
#[inline]
fn into_target(self, _module_path: &'static str) -> &'a str {
self
}
}

/// Caller did not specified target.
impl<'a> Target<'a> for () {
#[inline]
fn into_target(self, module_path: &'static str) -> &'a str {
module_path
}
}

// Types for the `kvs` argument.

/// Caller specified key-value data explicitly.
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)
}
}

/// Caller did not specify key-value data.
impl<'a> KVs<'a> for () {
#[inline]
fn into_kvs(self) -> Option<&'a [(&'a str, &'a Value<'a>)]> {
None
}
}

// Types for the `level` argument.

/// The log level is dynamically determined.
impl LevelTrait for Level {
#[inline]
fn into_level(self) -> Level {
self
}
}

macro_rules! define_static_levels {
($(($name:ident, $level:ident),)*) => {$(
#[derive(Clone, Copy, Debug)]
pub struct $name;

/// The log level is statically determined.
impl LevelTrait for $name {
#[inline]
fn into_level(self) -> Level {
Level::$level
}
}

impl PartialEq<LevelFilter> for $name {
#[inline]
fn eq(&self, other: &LevelFilter) -> bool {
self.into_level().eq(other)
}
}

impl PartialOrd<LevelFilter> for $name {
#[inline]
fn partial_cmp(&self, other: &LevelFilter) -> Option<Ordering> {
self.into_level().partial_cmp(other)
}
}
)*};
}

define_static_levels![
(LevelError, Error),
(LevelWarn, Warn),
(LevelInfo, Info),
(LevelDebug, Debug),
(LevelTrace, Trace),
];

// Log implementation.

/// Group arguments that can be shared by different log calls together in order to reduce binary size.
#[derive(Debug)]
pub struct Meta<'a> {
pub level: Level,
pub target: &'a str,
pub module_path: &'static str,
pub file: &'static str,
}

fn log_impl(
args: Arguments,
&Meta {
level,
target,
module_path,
file,
}: &Meta,
line: u32,
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"
)
}

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());
}

/// Group arguments that can be shared by different log calls together in order to reduce binary size.
#[derive(Debug)]
pub struct GenericMeta<T, L> {
pub level: L,
pub target: T,
pub module_path: &'static str,
pub file: &'static str,
}

pub fn log<'a, 'b, T, L, K>(args: Arguments, meta: &GenericMeta<T, L>, line: u32, kvs: K)
where
T: Target<'a>,
L: LevelTrait,
K: KVs<'b>,
{
log_impl(
args,
&Meta {
level: meta.level.into_level(),
target: meta.target.into_target(meta.module_path),
module_path: meta.module_path,
file: meta.file,
},
line,
kvs.into_kvs(),
)
}

pub fn enabled(level: Level, target: &str) -> bool {
crate::logger().enabled(&Metadata::builder().level(level).target(target).build())
}
Loading

0 comments on commit b54a3b7

Please sign in to comment.