Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize logging codegen #276

Merged
merged 1 commit into from
Jun 5, 2018
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
48 changes: 39 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,11 @@
//! [log4rs]: https://docs.rs/log4rs/*/log4rs/
//! [fern]: https://docs.rs/fern/*/fern/

#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/log/0.4.1")]
#![doc(
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/log/0.4.0-rc.1"
)]
#![warn(missing_docs)]
#![deny(missing_debug_implementations)]
#![cfg_attr(not(feature = "std"), no_std)]
Expand Down Expand Up @@ -298,10 +300,10 @@ static MAX_LOG_LEVEL_FILTER: AtomicUsize = ATOMIC_USIZE_INIT;

static LOG_LEVEL_NAMES: [&'static str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];

static SET_LOGGER_ERROR: &'static str = "attempted to set a logger after the logging system was \
already initialized";
static LEVEL_PARSE_ERROR: &'static str = "attempted to convert a string that doesn't match an \
existing log level";
static SET_LOGGER_ERROR: &'static str = "attempted to set a logger after the logging system \
was already initialized";
static LEVEL_PARSE_ERROR: &'static str =
"attempted to convert a string that doesn't match an existing log level";

/// An enum representing the available verbosity levels of the logger.
///
Expand Down Expand Up @@ -1121,6 +1123,34 @@ pub fn logger() -> &'static Log {
}
}

// WARNING: this is not part of the crate's public API and is subject to change at any time
#[doc(hidden)]
pub fn __private_api_log(
args: fmt::Arguments,
level: Level,
target: &str,
module_path: &str,
file: &str,
line: u32,
) {
logger().log(
&Record::builder()
.args(args)
.level(level)
.target(target)
.module_path(Some(module_path))
.file(Some(file))
.line(Some(line))
.build(),
);
}

// WARNING: this is not part of the crate's public API and is subject to change at any time
#[doc(hidden)]
pub fn __private_api_enabled(level: Level, target: &str) -> bool {
logger().enabled(&Metadata::builder().level(level).target(target).build())
}

/// The statically resolved maximum log level.
///
/// See the crate level documentation for information on how to configure this.
Expand Down Expand Up @@ -1163,8 +1193,8 @@ cfg_if! {
#[cfg(test)]
mod tests {
extern crate std;
use tests::std::string::ToString;
use super::{Level, LevelFilter, ParseLevelError};
use tests::std::string::ToString;

#[test]
fn test_levelfilter_from_str() {
Expand Down Expand Up @@ -1251,8 +1281,8 @@ mod tests {
#[test]
#[cfg(feature = "std")]
fn test_error_trait() {
use std::error::Error;
use super::SetLoggerError;
use std::error::Error;
let e = SetLoggerError(());
assert_eq!(
e.description(),
Expand Down
38 changes: 16 additions & 22 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,14 @@ macro_rules! log {
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::Log::log(
$crate::logger(),
&$crate::RecordBuilder::new()
.args(format_args!($($arg)+))
.level(lvl)
.target($target)
.module_path(Some(module_path!()))
.file(Some(file!()))
.line(Some(line!()))
.build()
)
$crate::__private_api_log(
format_args!($($arg)+),
lvl,
$target,
module_path!(),
file!(),
line!(),
);
}
});
($lvl:expr, $($arg:tt)+) => (log!(target: module_path!(), $lvl, $($arg)+))
Expand Down Expand Up @@ -205,16 +202,13 @@ macro_rules! trace {
/// ```
#[macro_export]
macro_rules! log_enabled {
(target: $target:expr, $lvl:expr) => ({
(target: $target:expr, $lvl:expr) => {{
let lvl = $lvl;
lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() &&
$crate::Log::enabled(
$crate::logger(),
&$crate::MetadataBuilder::new()
.level(lvl)
.target($target)
.build(),
)
});
($lvl:expr) => (log_enabled!(target: module_path!(), $lvl))
lvl <= $crate::STATIC_MAX_LEVEL
&& lvl <= $crate::max_level()
&& $crate::__private_api_enabled(lvl, $target)
}};
($lvl:expr) => {
log_enabled!(target: module_path!(), $lvl)
};
}