Make regex an optional dependency

Not all uses of env-logger require a full regex engine for matching, for example
the compiler works with just simple substring matching. Additionally compiling
regex can often take quite a bit of time, so for those that would prefer the
filtering can be disabled.
This commit is contained in:
Alex Crichton 2016-09-01 16:11:24 -07:00
parent d040d3236a
commit 9bba3d5fd5
5 changed files with 67 additions and 11 deletions

View File

@ -11,6 +11,7 @@ script:
- cargo test --verbose
- ([ $TRAVIS_RUST_VERSION != nightly ] || cargo test --verbose --no-default-features)
- cargo test --verbose --manifest-path env/Cargo.toml
- cargo test --verbose --manifest-path env/Cargo.toml --no-default-features
- cargo run --verbose --manifest-path tests/max_level_features/Cargo.toml
- cargo run --verbose --manifest-path tests/max_level_features/Cargo.toml --release
- ([ $TRAVIS_RUST_VERSION != nightly ] || cargo doc --no-deps --features nightly)

10
env/Cargo.toml vendored
View File

@ -11,13 +11,13 @@ An logging implementation for `log` which is configured via an environment
variable.
"""
[dependencies.log]
version = "0.3"
path = ".."
[dependencies]
regex = "0.1"
log = { version = "0.3", path = ".." }
regex = { version = "0.1", optional = true }
[[test]]
name = "regexp_filter"
harness = false
[features]
default = ["regex"]

18
env/src/lib.rs vendored
View File

@ -130,10 +130,8 @@
html_root_url = "http://doc.rust-lang.org/env_logger/")]
#![cfg_attr(test, deny(warnings))]
extern crate regex;
extern crate log;
use regex::Regex;
use std::env;
use std::io::prelude::*;
use std::io;
@ -141,10 +139,18 @@ use std::mem;
use log::{Log, LogLevel, LogLevelFilter, LogRecord, SetLoggerError, LogMetadata};
#[cfg(feature = "regex")]
#[path = "regex.rs"]
mod filter;
#[cfg(not(feature = "regex"))]
#[path = "string.rs"]
mod filter;
/// The logger.
pub struct Logger {
directives: Vec<LogDirective>,
filter: Option<Regex>,
filter: Option<filter::Filter>,
format: Box<Fn(&LogRecord) -> String + Sync + Send>,
}
@ -183,7 +189,7 @@ pub struct Logger {
/// ```
pub struct LogBuilder {
directives: Vec<LogDirective>,
filter: Option<Regex>,
filter: Option<filter::Filter>,
format: Box<Fn(&LogRecord) -> String + Sync + Send>,
}
@ -352,7 +358,7 @@ pub fn init() -> Result<(), SetLoggerError> {
/// Parse a logging specification string (e.g: "crate1,crate2::mod3,crate3::x=error/foo")
/// and return a vector with log directives.
fn parse_logging_spec(spec: &str) -> (Vec<LogDirective>, Option<Regex>) {
fn parse_logging_spec(spec: &str) -> (Vec<LogDirective>, Option<filter::Filter>) {
let mut dirs = Vec::new();
let mut parts = spec.split('/');
@ -399,7 +405,7 @@ fn parse_logging_spec(spec: &str) -> (Vec<LogDirective>, Option<Regex>) {
}});
let filter = filter.map_or(None, |filter| {
match Regex::new(filter) {
match filter::Filter::new(filter) {
Ok(re) => Some(re),
Err(e) => {
println!("warning: invalid regex filter - {}", e);

28
env/src/regex.rs vendored Normal file
View File

@ -0,0 +1,28 @@
extern crate regex;
use std::fmt;
use self::regex::Regex;
pub struct Filter {
inner: Regex,
}
impl Filter {
pub fn new(spec: &str) -> Result<Filter, String> {
match Regex::new(spec){
Ok(r) => Ok(Filter { inner: r }),
Err(e) => Err(e.to_string()),
}
}
pub fn is_match(&self, s: &str) -> bool {
self.inner.is_match(s)
}
}
impl fmt::Display for Filter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
}

21
env/src/string.rs vendored Normal file
View File

@ -0,0 +1,21 @@
use std::fmt;
pub struct Filter {
inner: String,
}
impl Filter {
pub fn new(spec: &str) -> Result<Filter, String> {
Ok(Filter { inner: spec.to_string() })
}
pub fn is_match(&self, s: &str) -> bool {
s.contains(&self.inner)
}
}
impl fmt::Display for Filter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
}