diff --git a/.travis.yml b/.travis.yml index 89e7ae0..2291fe9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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) diff --git a/env/Cargo.toml b/env/Cargo.toml index bd2de3c..f6b391a 100644 --- a/env/Cargo.toml +++ b/env/Cargo.toml @@ -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"] diff --git a/env/src/lib.rs b/env/src/lib.rs index 797680a..9105c19 100644 --- a/env/src/lib.rs +++ b/env/src/lib.rs @@ -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, - filter: Option, + filter: Option, format: Box String + Sync + Send>, } @@ -183,7 +189,7 @@ pub struct Logger { /// ``` pub struct LogBuilder { directives: Vec, - filter: Option, + filter: Option, format: Box 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, Option) { +fn parse_logging_spec(spec: &str) -> (Vec, Option) { let mut dirs = Vec::new(); let mut parts = spec.split('/'); @@ -399,7 +405,7 @@ fn parse_logging_spec(spec: &str) -> (Vec, Option) { }}); 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); diff --git a/env/src/regex.rs b/env/src/regex.rs new file mode 100644 index 0000000..0df03e6 --- /dev/null +++ b/env/src/regex.rs @@ -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 { + 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) + } +} diff --git a/env/src/string.rs b/env/src/string.rs new file mode 100644 index 0000000..74d0e04 --- /dev/null +++ b/env/src/string.rs @@ -0,0 +1,21 @@ +use std::fmt; + +pub struct Filter { + inner: String, +} + +impl Filter { + pub fn new(spec: &str) -> Result { + 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) + } +}