Skip to content

Commit 9d6c77e

Browse files
authored
Merge pull request #4 from belltoy/master
Make regex an optional dependency
2 parents dca23d6 + 5240ff9 commit 9d6c77e

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ path = "src/lib.rs"
1717

1818
[dependencies]
1919
slog = "2"
20-
regex = "0.1"
20+
regex = { version = "0.2", optional = true }
2121
slog-term = "2"
2222
slog-stdlog = "2"
2323
slog-scope = "3"
24-
slog-async = "2"
2524
log = "0.3.6"
2625

26+
[dev-dependencies]
27+
slog-async = "2"
28+
2729
[[test]]
2830
name = "regexp_filter"
2931
harness = false
3032

33+
[features]
34+
default = ["regex"]

src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,25 @@
7171
html_root_url = "http://doc.rust-lang.org/env_logger/")]
7272
#![cfg_attr(test, deny(warnings))]
7373

74-
extern crate regex;
7574
#[macro_use]
7675
extern crate slog;
7776
extern crate slog_term;
7877
extern crate slog_stdlog;
7978
extern crate slog_scope;
80-
extern crate slog_async;
8179
extern crate log;
8280

83-
use regex::Regex;
8481
use std::{env, result, sync};
8582
use std::cell::RefCell;
8683
use slog::*;
8784

85+
#[cfg(feature = "regex")]
86+
#[path = "regex.rs"]
87+
mod filter;
88+
89+
#[cfg(not(feature = "regex"))]
90+
#[path = "string.rs"]
91+
mod filter;
92+
8893
thread_local! {
8994
static TL_BUF: RefCell<String> = RefCell::new(String::new())
9095
}
@@ -93,7 +98,7 @@ thread_local! {
9398
pub struct EnvLogger<T : Drain> {
9499
drain : T,
95100
directives: Vec<LogDirective>,
96-
filter: Option<Regex>,
101+
filter: Option<filter::Filter>,
97102
}
98103

99104
/// LogBuilder acts as builder for initializing the EnvLogger.
@@ -102,7 +107,7 @@ pub struct EnvLogger<T : Drain> {
102107
pub struct LogBuilder<T : Drain> {
103108
drain : T,
104109
directives: Vec<LogDirective>,
105-
filter: Option<Regex>,
110+
filter: Option<filter::Filter>,
106111
}
107112

108113
impl<T : Drain> LogBuilder<T> {
@@ -271,7 +276,7 @@ pub fn init() -> std::result::Result<slog_scope::GlobalLoggerGuard, log::SetLogg
271276

272277
/// Parse a logging specification string (e.g: "crate1,crate2::mod3,crate3::x=error/foo")
273278
/// and return a vector with log directives.
274-
fn parse_logging_spec(spec: &str) -> (Vec<LogDirective>, Option<Regex>) {
279+
fn parse_logging_spec(spec: &str) -> (Vec<LogDirective>, Option<filter::Filter>) {
275280
let mut dirs = Vec::new();
276281

277282
let mut parts = spec.split('/');
@@ -318,7 +323,7 @@ fn parse_logging_spec(spec: &str) -> (Vec<LogDirective>, Option<Regex>) {
318323
}});
319324

320325
let filter = filter.map_or(None, |filter| {
321-
match Regex::new(filter) {
326+
match filter::Filter::new(filter) {
322327
Ok(re) => Some(re),
323328
Err(e) => {
324329
println!("warning: invalid regex filter - {}", e);

src/regex.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
extern crate regex;
2+
3+
use std::fmt;
4+
5+
use self::regex::Regex;
6+
7+
pub struct Filter {
8+
inner: Regex,
9+
}
10+
11+
impl Filter {
12+
pub fn new(spec: &str) -> Result<Filter, String> {
13+
match Regex::new(spec){
14+
Ok(r) => Ok(Filter { inner: r }),
15+
Err(e) => Err(e.to_string()),
16+
}
17+
}
18+
19+
pub fn is_match(&self, s: &str) -> bool {
20+
self.inner.is_match(s)
21+
}
22+
}
23+
24+
impl fmt::Display for Filter {
25+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26+
self.inner.fmt(f)
27+
}
28+
}

src/string.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::fmt;
2+
3+
pub struct Filter {
4+
inner: String,
5+
}
6+
7+
impl Filter {
8+
pub fn new(spec: &str) -> Result<Filter, String> {
9+
Ok(Filter { inner: spec.to_string() })
10+
}
11+
12+
pub fn is_match(&self, s: &str) -> bool {
13+
s.contains(&self.inner)
14+
}
15+
}
16+
17+
impl fmt::Display for Filter {
18+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19+
self.inner.fmt(f)
20+
}
21+
}

0 commit comments

Comments
 (0)