Skip to content

Commit

Permalink
chore: tidy some interfaces and naming (#22)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Aug 2, 2024
1 parent 1617536 commit eebc2fe
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/tisonkun/logforth"
rust-version = "1.71.0"
version = "0.5.0"
version = "0.6.0"

[package.metadata.docs.rs]
all-features = true
Expand Down
25 changes: 19 additions & 6 deletions src/filter/min_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,23 @@ use log::Metadata;
use crate::filter::Filter;
use crate::filter::FilterResult;

/// A filter that checks if the log level is at most the specified level.
///
/// From least to most verbose, the levels are:
///
/// - `Error`
/// - `Warn`
/// - `Info`
/// - `Debug`
/// - `Trace`
///
/// If MaxLevel is set to `Info`, it will allow `Error`, `Warn`, and `Info` logs.
///
/// If MaxLevel is set to `Off`, it will reject all logs.
#[derive(Debug, Clone)]
pub struct MinLevel(pub LevelFilter);
pub struct MaxLevel(pub LevelFilter);

impl MinLevel {
impl MaxLevel {
pub(crate) fn filter(&self, metadata: &Metadata) -> FilterResult {
let level = metadata.level();
if level <= self.0 {
Expand All @@ -31,14 +44,14 @@ impl MinLevel {
}
}
}
impl From<MinLevel> for Filter {
fn from(filter: MinLevel) -> Self {
Filter::MinLevel(filter)
impl From<MaxLevel> for Filter {
fn from(filter: MaxLevel) -> Self {
Filter::MaxLevel(filter)
}
}

impl From<LevelFilter> for Filter {
fn from(filter: LevelFilter) -> Self {
Filter::MinLevel(MinLevel(filter))
Filter::MaxLevel(MaxLevel(filter))
}
}
6 changes: 3 additions & 3 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

pub use self::custom::CustomFilter;
pub use self::min_level::MinLevel;
pub use self::min_level::MaxLevel;

mod custom;
mod min_level;
Expand All @@ -30,14 +30,14 @@ pub enum FilterResult {

#[derive(Debug)]
pub enum Filter {
MinLevel(MinLevel),
MaxLevel(MaxLevel),
Custom(CustomFilter),
}

impl Filter {
pub(crate) fn filter(&self, metadata: &log::Metadata) -> FilterResult {
match self {
Filter::MinLevel(filter) => filter.filter(metadata),
Filter::MaxLevel(filter) => filter.filter(metadata),
Filter::Custom(filter) => filter.filter(metadata),
}
}
Expand Down
28 changes: 11 additions & 17 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,30 @@ impl Dispatch {
/// This struct implements [log::Log] to bridge Logforth's logging implementations
/// with the [log] crate.
#[derive(Debug)]
pub struct Logger<const APPEND: bool = true> {
pub struct Logger {
dispatches: Vec<Dispatch>,
}

impl Default for Logger<false> {
impl Default for Logger {
fn default() -> Self {
Self::new()
}
}

impl Logger<false> {
impl Logger {
/// Create a new [Logger] instance.
///
/// The [Logger] instance is incomplete and must have at least one [Dispatch] added to it.
pub fn new() -> Logger<false> {
pub fn new() -> Logger {
Self { dispatches: vec![] }
}
}

impl Logger<true> {
impl Logger {
/// Add a [Dispatch] to the [Logger].
pub fn dispatch(mut self, dispatch: Dispatch) -> Logger {
self.dispatches.push(dispatch);
self
}

/// Set up the global logger with the [Logger] instance.
///
/// # Errors
Expand All @@ -156,16 +160,6 @@ impl Logger<true> {
}
}

impl<const APPEND: bool> Logger<APPEND> {
/// Add a [Dispatch] to the [Logger].
pub fn dispatch(mut self, dispatch: Dispatch) -> Logger<true> {
self.dispatches.push(dispatch);
Logger {
dispatches: self.dispatches,
}
}
}

impl log::Log for Logger {
fn enabled(&self, metadata: &Metadata) -> bool {
self.dispatches
Expand Down

0 comments on commit eebc2fe

Please sign in to comment.