Skip to content

Commit 93eed72

Browse files
committed
Optimize binary size
1 parent e9123d6 commit 93eed72

File tree

3 files changed

+377
-132
lines changed

3 files changed

+377
-132
lines changed

src/lib.rs

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ use std::str::FromStr;
341341

342342
#[macro_use]
343343
mod macros;
344+
#[doc(hidden)]
345+
pub mod private_api;
344346
mod serde;
345347

346348
#[cfg(feature = "kv_unstable")]
@@ -1464,55 +1466,6 @@ pub fn logger() -> &'static dyn Log {
14641466
}
14651467
}
14661468

1467-
// WARNING: this is not part of the crate's public API and is subject to change at any time
1468-
#[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-
15161469
// WARNING: this is not part of the crate's public API and is subject to change at any time
15171470
#[doc(hidden)]
15181471
pub fn __private_api_enabled(level: Level, target: &str) -> bool {

src/macros.rs

Lines changed: 110 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,66 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[doc(hidden)]
12+
#[macro_export]
13+
macro_rules! __internal_log {
14+
(@helper $target:expr, $lvl:expr, $kvs:expr, $($arg:tt)+) => ({
15+
let lvl = $lvl;
16+
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
17+
match (::std::format_args!($($arg)+), $target, $kvs) {
18+
(args, target, kvs) => if let ::std::option::Option::Some(literal) = ::std::fmt::Arguments::as_str(&args) {
19+
$crate::private_api::log(
20+
::std::module_path!(),
21+
::std::file!(),
22+
::std::line!(),
23+
$lvl,
24+
literal,
25+
target,
26+
kvs,
27+
);
28+
} else {
29+
$crate::private_api::log(
30+
::std::module_path!(),
31+
::std::file!(),
32+
::std::line!(),
33+
$lvl,
34+
args,
35+
target,
36+
kvs,
37+
);
38+
}
39+
}
40+
}
41+
});
42+
43+
// log!(target: "my_target", Level::Info, key1 = 42, key2 = true; "a {} event", "log");
44+
(target: $target:expr, $lvl:expr, $($key:tt = $value:expr),+; $($arg:tt)+) => ($crate::__internal_log!(
45+
@helper
46+
$target,
47+
$lvl,
48+
&[
49+
$(($crate::__log_key!($key), &$value as &$crate::private_api::LogKvValue)),+
50+
] as &[_],
51+
$($arg)+
52+
));
53+
54+
// log!(target: "my_target", Level::Info, "a {} event", "log");
55+
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ($crate::__internal_log!(
56+
@helper
57+
$target,
58+
$lvl,
59+
$crate::private_api::EmptyKVs,
60+
$($arg)+
61+
));
62+
63+
// log!(Level::Info, "a log event")
64+
($lvl:expr, $($arg:tt)+) => ($crate::__internal_log!(
65+
target: $crate::private_api::TargetIsModulePath,
66+
$lvl,
67+
$($arg)+
68+
));
69+
}
70+
1171
/// The standard logging macro.
1272
///
1373
/// This macro will generically log with the specified `Level` and `format!`
@@ -27,36 +87,25 @@
2787
/// data.0, data.1, private_data);
2888
/// # }
2989
/// ```
30-
#[macro_export(local_inner_macros)]
90+
#[macro_export]
3191
macro_rules! log {
3292
// log!(target: "my_target", Level::Info, key1 = 42, key2 = true; "a {} event", "log");
33-
(target: $target:expr, $lvl:expr, $($key:tt = $value:expr),+; $($arg:tt)+) => ({
34-
let lvl = $lvl;
35-
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
36-
$crate::__private_api_log(
37-
__log_format_args!($($arg)+),
38-
lvl,
39-
&($target, __log_module_path!(), __log_file!(), __log_line!()),
40-
$crate::__private_api::Option::Some(&[$((__log_key!($key), &$value)),+])
41-
);
42-
}
43-
});
93+
(target: $target:expr, $lvl:expr, $($key:tt = $value:expr),+; $($arg:tt)+) => ($crate::__internal_log!(
94+
target: ::std::convert::identity::<&::std::primitive::str>($target),
95+
::std::convert::identity::<$crate::Level>($lvl),
96+
$($key = $value),+;
97+
$($arg)+
98+
));
4499

45100
// log!(target: "my_target", Level::Info, "a {} event", "log");
46-
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
47-
let lvl = $lvl;
48-
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
49-
$crate::__private_api_log(
50-
__log_format_args!($($arg)+),
51-
lvl,
52-
&($target, __log_module_path!(), __log_file!(), __log_line!()),
53-
$crate::__private_api::Option::None,
54-
);
55-
}
56-
});
101+
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ($crate::__internal_log!(
102+
target: ::std::convert::identity::<&::std::primitive::str>($target),
103+
::std::convert::identity::<$crate::Level>($lvl),
104+
$($arg)+
105+
));
57106

58107
// log!(Level::Info, "a log event")
59-
($lvl:expr, $($arg:tt)+) => (log!(target: __log_module_path!(), $lvl, $($arg)+));
108+
($lvl:expr, $($arg:tt)+) => ($crate::__internal_log!(::std::convert::identity::<$crate::Level>($lvl), $($arg)+));
60109
}
61110

62111
/// Logs a message at the error level.
@@ -73,14 +122,18 @@ macro_rules! log {
73122
/// error!(target: "app_events", "App Error: {}, Port: {}", err_info, 22);
74123
/// # }
75124
/// ```
76-
#[macro_export(local_inner_macros)]
125+
#[macro_export]
77126
macro_rules! error {
78127
// error!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
79128
// error!(target: "my_target", "a {} event", "log")
80-
(target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Error, $($arg)+));
129+
(target: $target:expr, $($arg:tt)+) => ($crate::__internal_log!(
130+
target: ::std::convert::identity::<&::std::primitive::str>($target),
131+
$crate::private_api::StaticLevelError,
132+
$($arg)+
133+
));
81134

82135
// error!("a {} event", "log")
83-
($($arg:tt)+) => (log!($crate::Level::Error, $($arg)+))
136+
($($arg:tt)+) => ($crate::__internal_log!($crate::private_api::StaticLevelError, $($arg)+))
84137
}
85138

86139
/// Logs a message at the warn level.
@@ -97,14 +150,18 @@ macro_rules! error {
97150
/// warn!(target: "input_events", "App received warning: {}", warn_description);
98151
/// # }
99152
/// ```
100-
#[macro_export(local_inner_macros)]
153+
#[macro_export]
101154
macro_rules! warn {
102155
// warn!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
103156
// warn!(target: "my_target", "a {} event", "log")
104-
(target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Warn, $($arg)+));
157+
(target: $target:expr, $($arg:tt)+) => ($crate::__internal_log!(
158+
target: ::std::convert::identity::<&::std::primitive::str>($target),
159+
$crate::private_api::StaticLevelWarn,
160+
$($arg)+
161+
));
105162

106163
// warn!("a {} event", "log")
107-
($($arg:tt)+) => (log!($crate::Level::Warn, $($arg)+))
164+
($($arg:tt)+) => ($crate::__internal_log!($crate::private_api::StaticLevelWarn, $($arg)+))
108165
}
109166

110167
/// Logs a message at the info level.
@@ -123,14 +180,18 @@ macro_rules! warn {
123180
/// conn_info.port, conn_info.speed);
124181
/// # }
125182
/// ```
126-
#[macro_export(local_inner_macros)]
183+
#[macro_export]
127184
macro_rules! info {
128185
// info!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
129186
// info!(target: "my_target", "a {} event", "log")
130-
(target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Info, $($arg)+));
187+
(target: $target:expr, $($arg:tt)+) => ($crate::__internal_log!(
188+
target: ::std::convert::identity::<&::std::primitive::str>($target),
189+
$crate::private_api::StaticLevelInfo,
190+
$($arg)+
191+
));
131192

132193
// info!("a {} event", "log")
133-
($($arg:tt)+) => (log!($crate::Level::Info, $($arg)+))
194+
($($arg:tt)+) => ($crate::__internal_log!($crate::private_api::StaticLevelInfo, $($arg)+))
134195
}
135196

136197
/// Logs a message at the debug level.
@@ -148,14 +209,18 @@ macro_rules! info {
148209
/// debug!(target: "app_events", "New position: x: {}, y: {}", pos.x, pos.y);
149210
/// # }
150211
/// ```
151-
#[macro_export(local_inner_macros)]
212+
#[macro_export]
152213
macro_rules! debug {
153214
// debug!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
154215
// debug!(target: "my_target", "a {} event", "log")
155-
(target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Debug, $($arg)+));
216+
(target: $target:expr, $($arg:tt)+) => ($crate::__internal_log!(
217+
target: ::std::convert::identity::<&::std::primitive::str>($target),
218+
$crate::private_api::StaticLevelDebug,
219+
$($arg)+
220+
));
156221

157222
// debug!("a {} event", "log")
158-
($($arg:tt)+) => (log!($crate::Level::Debug, $($arg)+))
223+
($($arg:tt)+) => ($crate::__internal_log!($crate::private_api::StaticLevelDebug, $($arg)+))
159224
}
160225

161226
/// Logs a message at the trace level.
@@ -175,14 +240,18 @@ macro_rules! debug {
175240
/// if pos.y >= 0.0 { "positive" } else { "negative" });
176241
/// # }
177242
/// ```
178-
#[macro_export(local_inner_macros)]
243+
#[macro_export]
179244
macro_rules! trace {
180245
// trace!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
181246
// trace!(target: "my_target", "a {} event", "log")
182-
(target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Trace, $($arg)+));
247+
(target: $target:expr, $($arg:tt)+) => ($crate::__internal_log!(
248+
target: ::std::convert::identity::<&::std::primitive::str>($target),
249+
$crate::private_api::StaticLevelTrace,
250+
$($arg)+
251+
));
183252

184253
// trace!("a {} event", "log")
185-
($($arg:tt)+) => (log!($crate::Level::Trace, $($arg)+))
254+
($($arg:tt)+) => ($crate::__internal_log!($crate::private_api::StaticLevelTrace, $($arg)+))
186255
}
187256

188257
/// Determines if a message logged at the specified level in that module will
@@ -211,7 +280,7 @@ macro_rules! trace {
211280
/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
212281
/// # fn main() {}
213282
/// ```
214-
#[macro_export(local_inner_macros)]
283+
#[macro_export]
215284
macro_rules! log_enabled {
216285
(target: $target:expr, $lvl:expr) => {{
217286
let lvl = $lvl;
@@ -220,49 +289,7 @@ macro_rules! log_enabled {
220289
&& $crate::__private_api_enabled(lvl, $target)
221290
}};
222291
($lvl:expr) => {
223-
log_enabled!(target: __log_module_path!(), $lvl)
224-
};
225-
}
226-
227-
// The log macro above cannot invoke format_args directly because it uses
228-
// local_inner_macros. A format_args invocation there would resolve to
229-
// $crate::format_args which does not exist. Instead invoke format_args here
230-
// outside of local_inner_macros so that it resolves (probably) to
231-
// core::format_args or std::format_args. Same for the several macros that
232-
// follow.
233-
//
234-
// This is a workaround until we drop support for pre-1.30 compilers. At that
235-
// point we can remove use of local_inner_macros, use $crate:: when invoking
236-
// local macros, and invoke format_args directly.
237-
#[doc(hidden)]
238-
#[macro_export]
239-
macro_rules! __log_format_args {
240-
($($args:tt)*) => {
241-
format_args!($($args)*)
242-
};
243-
}
244-
245-
#[doc(hidden)]
246-
#[macro_export]
247-
macro_rules! __log_module_path {
248-
() => {
249-
module_path!()
250-
};
251-
}
252-
253-
#[doc(hidden)]
254-
#[macro_export]
255-
macro_rules! __log_file {
256-
() => {
257-
file!()
258-
};
259-
}
260-
261-
#[doc(hidden)]
262-
#[macro_export]
263-
macro_rules! __log_line {
264-
() => {
265-
line!()
292+
log_enabled!(target: ::std::module_path!(), $lvl)
266293
};
267294
}
268295

0 commit comments

Comments
 (0)