Skip to content

Commit

Permalink
Fix the inverted RUST_LOG filter
Browse files Browse the repository at this point in the history
RUST_LOG supports regex filtering of log messages with a syntax like
`RUST_LOG=main/foo` to use the regex filter 'foo'. Unfortunately, the
filter was inverted, so `RUST_LOG=main/foo` would actually show all
messages except the ones containing 'foo'.
  • Loading branch information
lilyball committed Sep 17, 2014
1 parent ad9ed40 commit e7b2570
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/liblog/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ pub fn log(level: u32, loc: &'static LogLocation, args: &fmt::Arguments) {
// Test the literal string from args against the current filter, if there
// is one.
match unsafe { FILTER.as_ref() } {
Some(filter) if filter.is_match(args.to_string().as_slice()) => return,
Some(filter) if !filter.is_match(args.to_string().as_slice()) => return,
_ => {}
}

Expand Down Expand Up @@ -383,7 +383,7 @@ fn enabled(level: u32,

/// Initialize logging for the current process.
///
/// This is not threadsafe at all, so initialization os performed through a
/// This is not threadsafe at all, so initialization is performed through a
/// `Once` primitive (and this function is called from that primitive).
fn init() {
let (mut directives, filter) = match os::getenv("RUST_LOG") {
Expand Down
54 changes: 54 additions & 0 deletions src/test/run-pass/rust-log-filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// exec-env:RUST_LOG=rust-log-filter/f.o

#![feature(phase)]
#[phase(plugin,link)]
extern crate log;

pub struct ChannelLogger {
tx: Sender<String>
}

impl ChannelLogger {
pub fn new() -> (Box<ChannelLogger>, Receiver<String>) {
let (tx, rx) = channel();
(box ChannelLogger { tx: tx }, rx)
}
}

impl log::Logger for ChannelLogger {
fn log(&mut self, record: &log::LogRecord) {
self.tx.send(format!("{}", record.args));
}
}

pub fn main() {
let (logger, rx) = ChannelLogger::new();

spawn(proc() {
log::set_logger(logger);

// our regex is "f.o"
// ensure it is a regex, and isn't anchored
info!("foo");
info!("bar");
info!("foo bar");
info!("bar foo");
info!("f1o");
});

assert_eq!(rx.recv().as_slice(), "foo");
assert_eq!(rx.recv().as_slice(), "foo bar");
assert_eq!(rx.recv().as_slice(), "bar foo");
assert_eq!(rx.recv().as_slice(), "f1o");
assert!(rx.recv_opt().is_err());
}

5 comments on commit e7b2570

@bors
Copy link
Contributor

@bors bors commented on e7b2570 Sep 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at lilyball@e7b2570

@bors
Copy link
Contributor

@bors bors commented on e7b2570 Sep 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging kballard/rust/rust_log_pattern_inverted = e7b2570 into auto

@bors
Copy link
Contributor

@bors bors commented on e7b2570 Sep 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kballard/rust/rust_log_pattern_inverted = e7b2570 merged ok, testing candidate = 8067f44

@bors
Copy link
Contributor

@bors bors commented on e7b2570 Sep 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on e7b2570 Sep 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 8067f44

Please sign in to comment.