Skip to content

Commit 14c70e5

Browse files
committed
Optimize binary size
1 parent cab1088 commit 14c70e5

File tree

3 files changed

+425
-93
lines changed

3 files changed

+425
-93
lines changed

src/__private_api.rs

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
use self::sealed::{LogArgs, 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+
use std::fmt::Arguments;
19+
20+
pub trait LogArgs {
21+
fn with(self, f: impl FnOnce(Arguments));
22+
}
23+
24+
pub trait LogLevel {
25+
fn into_log_level(self) -> Level;
26+
}
27+
28+
pub trait LogTarget {
29+
fn with(self, module_path: &'static str, f: impl FnOnce(&str));
30+
}
31+
32+
pub trait LogKVs {
33+
fn with(self, f: impl FnOnce(&[(&str, &super::LogKvValue)]));
34+
}
35+
}
36+
37+
// `LogArgs`.
38+
39+
impl LogArgs for &str {
40+
#[inline]
41+
fn with(self, f: impl FnOnce(Arguments)) {
42+
f(format_args!("{self}"))
43+
}
44+
}
45+
46+
impl LogArgs for Arguments<'_> {
47+
fn with(self, f: impl FnOnce(Arguments)) {
48+
f(self)
49+
}
50+
}
51+
52+
// `LogLevel`.
53+
54+
impl LogLevel for Level {
55+
#[inline]
56+
fn into_log_level(self) -> Level {
57+
self
58+
}
59+
}
60+
61+
macro_rules! define_static_levels {
62+
($($ty:ident => $lvl:ident,)*) => {
63+
$(
64+
#[derive(Debug)]
65+
pub struct $ty;
66+
67+
impl LogLevel for $ty {
68+
#[inline]
69+
fn into_log_level(self) -> Level {
70+
Level::$lvl
71+
}
72+
}
73+
74+
impl PartialEq<LevelFilter> for $ty {
75+
#[inline]
76+
fn eq(&self, other: &LevelFilter) -> bool {
77+
Level::$lvl.eq(other)
78+
}
79+
}
80+
81+
impl PartialOrd<LevelFilter> for $ty {
82+
#[inline]
83+
fn partial_cmp(&self, other: &LevelFilter) -> Option<Ordering> {
84+
Level::$lvl.partial_cmp(other)
85+
}
86+
87+
#[inline]
88+
fn lt(&self, other: &LevelFilter) -> bool {
89+
Level::$lvl.lt(other)
90+
}
91+
92+
#[inline]
93+
fn le(&self, other: &LevelFilter) -> bool {
94+
Level::$lvl.le(other)
95+
}
96+
97+
#[inline]
98+
fn gt(&self, other: &LevelFilter) -> bool {
99+
Level::$lvl.gt(other)
100+
}
101+
102+
#[inline]
103+
fn ge(&self, other: &LevelFilter) -> bool {
104+
Level::$lvl.ge(other)
105+
}
106+
}
107+
)*
108+
};
109+
}
110+
111+
define_static_levels![
112+
StaticLevelError => Error,
113+
StaticLevelWarn => Warn,
114+
StaticLevelInfo => Info,
115+
StaticLevelDebug => Debug,
116+
StaticLevelTrace => Trace,
117+
];
118+
119+
// `LogTarget`.
120+
121+
impl LogTarget for &str {
122+
#[inline]
123+
fn with(self, _module_path: &'static str, f: impl FnOnce(&str)) {
124+
f(self)
125+
}
126+
}
127+
128+
#[derive(Debug)]
129+
pub struct TargetIsModulePath;
130+
131+
impl LogTarget for TargetIsModulePath {
132+
#[inline]
133+
fn with(self, module_path: &'static str, f: impl FnOnce(&str)) {
134+
f(module_path)
135+
}
136+
}
137+
138+
// `LogKVs`.
139+
140+
impl LogKVs for &[(&str, &LogKvValue<'_>)] {
141+
#[inline]
142+
fn with(self, f: impl FnOnce(&[(&str, &LogKvValue)])) {
143+
f(self)
144+
}
145+
}
146+
147+
#[derive(Debug)]
148+
pub struct EmptyKVs;
149+
150+
impl LogKVs for EmptyKVs {
151+
#[inline]
152+
fn with(self, f: impl FnOnce(&[(&str, &LogKvValue)])) {
153+
f(&[])
154+
}
155+
}
156+
157+
// Log functions.
158+
159+
fn log_0(
160+
&(module_path, file): &'static (&'static str, &'static str),
161+
line: u32,
162+
args: Arguments,
163+
level: Level,
164+
target: &str,
165+
kvs: &[(&str, &LogKvValue)],
166+
) {
167+
#[cfg(not(feature = "kv_unstable"))]
168+
if !kvs.is_empty() {
169+
panic!(
170+
"key-value support is experimental and must be enabled using the `kv_unstable` feature"
171+
);
172+
}
173+
174+
let mut builder = Record::builder();
175+
176+
builder
177+
.args(args)
178+
.level(level)
179+
.target(target)
180+
.module_path_static(Some(module_path))
181+
.file_static(Some(file))
182+
.line(Some(line));
183+
184+
#[cfg(feature = "kv_unstable")]
185+
builder.key_values(&kvs);
186+
187+
crate::logger().log(&builder.build());
188+
}
189+
190+
fn log_1<K>(
191+
module_path_and_file: &'static (&'static str, &'static str),
192+
line: u32,
193+
args: Arguments,
194+
level: Level,
195+
target: &str,
196+
kvs: K,
197+
) where
198+
K: LogKVs,
199+
{
200+
kvs.with(|kvs| log_0(module_path_and_file, line, args, level, target, kvs));
201+
}
202+
203+
fn log_2<T, K>(
204+
module_path_and_file: &'static (&'static str, &'static str),
205+
line: u32,
206+
args: Arguments,
207+
level: Level,
208+
target: T,
209+
kvs: K,
210+
) where
211+
T: LogTarget,
212+
K: LogKVs,
213+
{
214+
target.with(module_path_and_file.0, |target| {
215+
log_1(
216+
module_path_and_file,
217+
line,
218+
args,
219+
level.into_log_level(),
220+
target,
221+
kvs,
222+
)
223+
});
224+
}
225+
226+
pub fn log_3<L, T, K>(
227+
module_path_and_file: &'static (&'static str, &'static str),
228+
line: u32,
229+
args: Arguments,
230+
level: L,
231+
target: T,
232+
kvs: K,
233+
) where
234+
L: LogLevel,
235+
T: LogTarget,
236+
K: LogKVs,
237+
{
238+
log_2(
239+
module_path_and_file,
240+
line,
241+
args,
242+
level.into_log_level(),
243+
target,
244+
kvs,
245+
)
246+
}
247+
248+
pub fn log<A, L, T, K>(
249+
module_path_and_file: &'static (&'static str, &'static str),
250+
line: u32,
251+
args: A,
252+
level: L,
253+
target: T,
254+
kvs: K,
255+
) where
256+
A: LogArgs,
257+
L: LogLevel,
258+
T: LogTarget,
259+
K: LogKVs,
260+
{
261+
args.with(|args| log_3(module_path_and_file, line, args, level, target, kvs))
262+
}
263+
264+
pub fn enabled(level: Level, target: &str) -> bool {
265+
crate::logger().enabled(&Metadata::builder().level(level).target(target).build())
266+
}
267+
268+
pub const fn is_literal(s: &str) -> bool {
269+
let s = s.as_bytes();
270+
let n = s.len();
271+
let mut i = 0;
272+
273+
while i < n {
274+
if matches!(s[i], b'{' | b'}') {
275+
return false;
276+
}
277+
278+
i += 1;
279+
}
280+
281+
true
282+
}

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)