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

chore: tidy some interfaces and naming #22

Merged
merged 1 commit into from
Aug 2, 2024
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
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> {
Copy link
Contributor Author

@tisonkun tisonkun Aug 2, 2024

Choose a reason for hiding this comment

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

Add const parameter will cause logger cannot be reused.

For example,

let mut logger = Logger::new();

// ... do something

// compile error; cannot assign `Logger<true>` to `Logger<false>`.
logger = logger.dispatch( ... ); 

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