Skip to content

Commit

Permalink
refactor(filter): Pull out directive mod
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 19, 2024
1 parent c769e03 commit 98c450f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
20 changes: 20 additions & 0 deletions crates/env_filter/src/directive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use log::Level;
use log::LevelFilter;

#[derive(Debug)]
pub(crate) struct Directive {
pub(crate) name: Option<String>,
pub(crate) level: LevelFilter,
}

// Check whether a level and target are enabled by the set of directives.
pub(crate) fn enabled(directives: &[Directive], level: Level, target: &str) -> bool {
// Search for the longest match, the vector is assumed to be pre-sorted.
for directive in directives.iter().rev() {
match directive.name {
Some(ref name) if !target.starts_with(&**name) => {}
Some(..) | None => return level <= directive.level,
}
}
false
}
23 changes: 4 additions & 19 deletions crates/env_filter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@
//! }
//! ```

mod directive;
mod op;
mod parser;

use std::env;
use std::fmt;
use std::mem;

use log::{Level, LevelFilter, Metadata, Record};
use log::{LevelFilter, Metadata, Record};

use directive::enabled;
use directive::Directive;
use op::FilterOp;
use parser::parse_spec;

Expand Down Expand Up @@ -210,12 +213,6 @@ impl fmt::Debug for Builder {
}
}

#[derive(Debug)]
struct Directive {
name: Option<String>,
level: LevelFilter,
}

/// A log filter.
///
/// This struct can be used to determine whether or not a log record
Expand Down Expand Up @@ -286,18 +283,6 @@ impl fmt::Debug for Filter {
}
}

// Check whether a level and target are enabled by the set of directives.
fn enabled(directives: &[Directive], level: Level, target: &str) -> bool {
// Search for the longest match, the vector is assumed to be pre-sorted.
for directive in directives.iter().rev() {
match directive.name {
Some(ref name) if !target.starts_with(&**name) => {}
Some(..) | None => return level <= directive.level,
}
}
false
}

#[cfg(test)]
mod tests {
use log::{Level, LevelFilter};
Expand Down

0 comments on commit 98c450f

Please sign in to comment.