Skip to content

Commit ac32187

Browse files
committed
Optimize binary size
1 parent cab1088 commit ac32187

File tree

3 files changed

+340
-93
lines changed

3 files changed

+340
-93
lines changed

src/__private_api.rs

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
use self::sealed::{LogKVs, LogLevel, LogTarget};
2+
use crate::{Level, LevelFilter, Metadata, Record};
3+
use std::cmp::Ordering;
4+
pub use std::convert::identity;
5+
use std::fmt::Arguments;
6+
use std::option::Option;
7+
pub use std::primitive::str;
8+
pub use std::{file, format_args, line, module_path, stringify};
9+
10+
#[cfg(feature = "kv_unstable")]
11+
pub type LogKvValue<'a> = dyn crate::kv::value::ToValue + 'a;
12+
13+
#[cfg(not(feature = "kv_unstable"))]
14+
pub type LogKvValue<'a> = str;
15+
16+
mod sealed {
17+
use crate::Level;
18+
19+
pub trait LogLevel {
20+
fn into_log_level(self) -> Level;
21+
}
22+
23+
pub trait LogTarget {
24+
fn with<R>(self, module_path: &'static str, f: impl FnOnce(&str) -> R) -> R;
25+
}
26+
27+
pub trait LogKVs {
28+
fn with<R>(self, f: impl FnOnce(&[(&str, &super::LogKvValue)]) -> R) -> R;
29+
}
30+
}
31+
32+
// `LogLevel`.
33+
34+
impl LogLevel for Level {
35+
#[inline]
36+
fn into_log_level(self) -> Level {
37+
self
38+
}
39+
}
40+
41+
macro_rules! define_static_levels {
42+
($($ty:ident => $lvl:ident,)*) => {
43+
$(
44+
#[derive(Debug)]
45+
pub struct $ty;
46+
47+
impl LogLevel for $ty {
48+
#[inline]
49+
fn into_log_level(self) -> Level {
50+
Level::$lvl
51+
}
52+
}
53+
54+
impl PartialEq<LevelFilter> for $ty {
55+
#[inline]
56+
fn eq(&self, other: &LevelFilter) -> bool {
57+
Level::$lvl.eq(other)
58+
}
59+
}
60+
61+
impl PartialOrd<LevelFilter> for $ty {
62+
#[inline]
63+
fn partial_cmp(&self, other: &LevelFilter) -> Option<Ordering> {
64+
Level::$lvl.partial_cmp(other)
65+
}
66+
67+
#[inline]
68+
fn lt(&self, other: &LevelFilter) -> bool {
69+
Level::$lvl.lt(other)
70+
}
71+
72+
#[inline]
73+
fn le(&self, other: &LevelFilter) -> bool {
74+
Level::$lvl.le(other)
75+
}
76+
77+
#[inline]
78+
fn gt(&self, other: &LevelFilter) -> bool {
79+
Level::$lvl.gt(other)
80+
}
81+
82+
#[inline]
83+
fn ge(&self, other: &LevelFilter) -> bool {
84+
Level::$lvl.ge(other)
85+
}
86+
}
87+
)*
88+
};
89+
}
90+
91+
define_static_levels![
92+
StaticLevelError => Error,
93+
StaticLevelWarn => Warn,
94+
StaticLevelInfo => Info,
95+
StaticLevelDebug => Debug,
96+
StaticLevelTrace => Trace,
97+
];
98+
99+
// `LogTarget`.
100+
101+
impl LogTarget for &str {
102+
#[inline]
103+
fn with<R>(self, _module_path: &'static str, f: impl FnOnce(&str) -> R) -> R {
104+
f(self)
105+
}
106+
}
107+
108+
#[derive(Debug)]
109+
pub struct TargetIsModulePath;
110+
111+
impl LogTarget for TargetIsModulePath {
112+
#[inline]
113+
fn with<R>(self, module_path: &'static str, f: impl FnOnce(&str) -> R) -> R {
114+
f(module_path)
115+
}
116+
}
117+
118+
// `LogKVs`.
119+
120+
impl LogKVs for &[(&str, &LogKvValue<'_>)] {
121+
#[inline]
122+
fn with<R>(self, f: impl FnOnce(&[(&str, &LogKvValue)]) -> R) -> R {
123+
f(self)
124+
}
125+
}
126+
127+
#[derive(Debug)]
128+
pub struct EmptyKVs;
129+
130+
impl LogKVs for EmptyKVs {
131+
#[inline]
132+
fn with<R>(self, f: impl FnOnce(&[(&str, &LogKvValue)]) -> R) -> R {
133+
f(&[])
134+
}
135+
}
136+
137+
// Log functions.
138+
139+
fn log_0(
140+
&(module_path, file): &'static (&'static str, &'static str),
141+
line: u32,
142+
args: Arguments,
143+
level: Level,
144+
target: &str,
145+
kvs: &[(&str, &LogKvValue)],
146+
) {
147+
#[cfg(not(feature = "kv_unstable"))]
148+
if !kvs.is_empty() {
149+
panic!(
150+
"key-value support is experimental and must be enabled using the `kv_unstable` feature"
151+
);
152+
}
153+
154+
let mut builder = Record::builder();
155+
156+
builder
157+
.args(args)
158+
.level(level)
159+
.target(target)
160+
.module_path_static(Some(module_path))
161+
.file_static(Some(file))
162+
.line(Some(line));
163+
164+
#[cfg(feature = "kv_unstable")]
165+
builder.key_values(&kvs);
166+
167+
crate::logger().log(&builder.build());
168+
}
169+
170+
fn log_1<K>(
171+
module_path_and_file: &'static (&'static str, &'static str),
172+
line: u32,
173+
args: Arguments,
174+
level: Level,
175+
target: &str,
176+
kvs: K,
177+
) where
178+
K: LogKVs,
179+
{
180+
kvs.with(|kvs| log_0(module_path_and_file, line, args, level, target, kvs));
181+
}
182+
183+
fn log_2<T, K>(
184+
module_path_and_file: &'static (&'static str, &'static str),
185+
line: u32,
186+
args: Arguments,
187+
level: Level,
188+
target: T,
189+
kvs: K,
190+
) where
191+
T: LogTarget,
192+
K: LogKVs,
193+
{
194+
target.with(module_path_and_file.0, |target| {
195+
log_1(
196+
module_path_and_file,
197+
line,
198+
args,
199+
level.into_log_level(),
200+
target,
201+
kvs,
202+
)
203+
});
204+
}
205+
206+
pub fn log<L, T, K>(
207+
module_path_and_file: &'static (&'static str, &'static str),
208+
line: u32,
209+
args: Arguments,
210+
level: L,
211+
target: T,
212+
kvs: K,
213+
) where
214+
L: LogLevel,
215+
T: LogTarget,
216+
K: LogKVs,
217+
{
218+
log_2(
219+
module_path_and_file,
220+
line,
221+
args,
222+
level.into_log_level(),
223+
target,
224+
kvs,
225+
)
226+
}
227+
228+
pub fn enabled(level: Level, target: &str) -> bool {
229+
crate::logger().enabled(&Metadata::builder().level(level).target(target).build())
230+
}

src/lib.rs

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,65 +1466,7 @@ pub fn logger() -> &'static dyn Log {
14661466

14671467
// WARNING: this is not part of the crate's public API and is subject to change at any time
14681468
#[doc(hidden)]
1469-
#[cfg(not(feature = "kv_unstable"))]
1470-
pub fn __private_api_log(
1471-
args: fmt::Arguments,
1472-
level: Level,
1473-
&(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
1474-
kvs: Option<&[(&str, &str)]>,
1475-
) {
1476-
if kvs.is_some() {
1477-
panic!(
1478-
"key-value support is experimental and must be enabled using the `kv_unstable` feature"
1479-
)
1480-
}
1481-
1482-
logger().log(
1483-
&Record::builder()
1484-
.args(args)
1485-
.level(level)
1486-
.target(target)
1487-
.module_path_static(Some(module_path))
1488-
.file_static(Some(file))
1489-
.line(Some(line))
1490-
.build(),
1491-
);
1492-
}
1493-
1494-
// WARNING: this is not part of the crate's public API and is subject to change at any time
1495-
#[doc(hidden)]
1496-
#[cfg(feature = "kv_unstable")]
1497-
pub fn __private_api_log(
1498-
args: fmt::Arguments,
1499-
level: Level,
1500-
&(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
1501-
kvs: Option<&[(&str, &dyn kv::ToValue)]>,
1502-
) {
1503-
logger().log(
1504-
&Record::builder()
1505-
.args(args)
1506-
.level(level)
1507-
.target(target)
1508-
.module_path_static(Some(module_path))
1509-
.file_static(Some(file))
1510-
.line(Some(line))
1511-
.key_values(&kvs)
1512-
.build(),
1513-
);
1514-
}
1515-
1516-
// WARNING: this is not part of the crate's public API and is subject to change at any time
1517-
#[doc(hidden)]
1518-
pub fn __private_api_enabled(level: Level, target: &str) -> bool {
1519-
logger().enabled(&Metadata::builder().level(level).target(target).build())
1520-
}
1521-
1522-
// WARNING: this is not part of the crate's public API and is subject to change at any time
1523-
#[doc(hidden)]
1524-
pub mod __private_api {
1525-
pub use std::option::Option;
1526-
pub use std::{file, format_args, line, module_path, stringify};
1527-
}
1469+
pub mod __private_api;
15281470

15291471
/// The statically resolved maximum log level.
15301472
///

0 commit comments

Comments
 (0)