Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move impls next to types #283

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 77 additions & 77 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,6 @@ mod inner;
#[path = "string.rs"]
mod inner;

/// A log filter.
///
/// This struct can be used to determine whether or not a log record
/// should be written to the output.
/// Use the [`Builder`] type to parse and construct a `Filter`.
///
/// [`Builder`]: struct.Builder.html
pub struct Filter {
directives: Vec<Directive>,
filter: Option<inner::Filter>,
}

/// A builder for a log filter.
///
/// It can be used to parse a set of directives from a string before building
Expand Down Expand Up @@ -112,61 +100,6 @@ pub struct Builder {
built: bool,
}

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

impl Filter {
/// Returns the maximum `LevelFilter` that this filter instance is
/// configured to output.
///
/// # Example
///
/// ```rust
/// use log::LevelFilter;
/// use env_logger::filter::Builder;
///
/// let mut builder = Builder::new();
/// builder.filter(Some("module1"), LevelFilter::Info);
/// builder.filter(Some("module2"), LevelFilter::Error);
///
/// let filter = builder.build();
/// assert_eq!(filter.filter(), LevelFilter::Info);
/// ```
pub fn filter(&self) -> LevelFilter {
self.directives
.iter()
.map(|d| d.level)
.max()
.unwrap_or(LevelFilter::Off)
}

/// Checks if this record matches the configured filter.
pub fn matches(&self, record: &Record) -> bool {
if !self.enabled(record.metadata()) {
return false;
}

if let Some(filter) = self.filter.as_ref() {
if !filter.is_match(&record.args().to_string()) {
return false;
}
}

true
}

/// Determines if a log message with the specified metadata would be logged.
pub fn enabled(&self, metadata: &Metadata) -> bool {
let level = metadata.level();
let target = metadata.target();

enabled(&self.directives, level, target)
}
}

impl Builder {
/// Initializes the filter builder with defaults.
pub fn new() -> Builder {
Expand Down Expand Up @@ -265,7 +198,7 @@ impl Builder {

Filter {
directives: mem::take(&mut directives),
filter: mem::replace(&mut self.filter, None),
filter: mem::take(&mut self.filter),
}
}
}
Expand All @@ -276,15 +209,6 @@ impl Default for Builder {
}
}

impl fmt::Debug for Filter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Filter")
.field("filter", &self.filter)
.field("directives", &self.directives)
.finish()
}
}

impl fmt::Debug for Builder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.built {
Expand All @@ -298,6 +222,82 @@ 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
/// should be written to the output.
/// Use the [`Builder`] type to parse and construct a `Filter`.
///
/// [`Builder`]: struct.Builder.html
pub struct Filter {
directives: Vec<Directive>,
filter: Option<inner::Filter>,
}

impl Filter {
/// Returns the maximum `LevelFilter` that this filter instance is
/// configured to output.
///
/// # Example
///
/// ```rust
/// use log::LevelFilter;
/// use env_logger::filter::Builder;
///
/// let mut builder = Builder::new();
/// builder.filter(Some("module1"), LevelFilter::Info);
/// builder.filter(Some("module2"), LevelFilter::Error);
///
/// let filter = builder.build();
/// assert_eq!(filter.filter(), LevelFilter::Info);
/// ```
pub fn filter(&self) -> LevelFilter {
self.directives
.iter()
.map(|d| d.level)
.max()
.unwrap_or(LevelFilter::Off)
}

/// Checks if this record matches the configured filter.
pub fn matches(&self, record: &Record) -> bool {
if !self.enabled(record.metadata()) {
return false;
}

if let Some(filter) = self.filter.as_ref() {
if !filter.is_match(&record.args().to_string()) {
return false;
}
}

true
}

/// Determines if a log message with the specified metadata would be logged.
pub fn enabled(&self, metadata: &Metadata) -> bool {
let level = metadata.level();
let target = metadata.target();

enabled(&self.directives, level, target)
}
}

impl fmt::Debug for Filter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Filter")
.field("filter", &self.filter)
.field("directives", &self.directives)
.finish()
}
}

/// Parse a logging specification string (e.g: "crate1,crate2::mod3,crate3::x=error/foo")
/// and return a vector with log directives.
fn parse_spec(spec: &str) -> (Vec<Directive>, Option<inner::Filter>) {
Expand Down
4 changes: 0 additions & 4 deletions src/fmt/humantime/extern_impl.rs → src/fmt/humantime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ use humantime::{

use crate::fmt::{Formatter, TimestampPrecision};

pub(in crate::fmt) mod glob {
pub use super::*;
}

impl Formatter {
/// Get a [`Timestamp`] for the current date and time in UTC.
///
Expand Down
11 changes: 0 additions & 11 deletions src/fmt/humantime/mod.rs

This file was deleted.

5 changes: 0 additions & 5 deletions src/fmt/humantime/shim_impl.rs

This file was deleted.

34 changes: 18 additions & 16 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ use std::{fmt, io, mem};

use log::Record;

#[cfg(feature = "humantime")]
mod humantime;
pub(crate) mod writer;

pub use self::humantime::glob::*;
#[cfg(feature = "humantime")]
pub use self::humantime::Timestamp;
pub use self::writer::glob::*;

use self::writer::{Buffer, Writer};
Expand Down Expand Up @@ -149,21 +151,6 @@ pub(crate) struct Builder {
built: bool,
}

impl Default for Builder {
fn default() -> Self {
Builder {
format_timestamp: Some(Default::default()),
format_module_path: false,
format_target: true,
format_level: true,
format_indent: Some(4),
custom_format: None,
format_suffix: "\n",
built: false,
}
}
}

impl Builder {
/// Convert the format into a callable function.
///
Expand Down Expand Up @@ -202,6 +189,21 @@ impl Builder {
}
}

impl Default for Builder {
fn default() -> Self {
Builder {
format_timestamp: Some(Default::default()),
format_module_path: false,
format_target: true,
format_level: true,
format_indent: Some(4),
custom_format: None,
format_suffix: "\n",
built: false,
}
}
}

#[cfg(feature = "color")]
type SubtleStyle = StyledValue<'static, &'static str>;
#[cfg(not(feature = "color"))]
Expand Down
Loading
Loading