Skip to content

Commit

Permalink
chore: reduce duplicated functions (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
andylokandy authored Aug 1, 2024
1 parent d9e779b commit ae6faf5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 36 deletions.
7 changes: 0 additions & 7 deletions src/append/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub use fastrace::*;
pub use opentelemetry::*;
pub use stdio::*;

use crate::filter::Filter;
use crate::layout::IdenticalLayout;
use crate::layout::Layout;

Expand All @@ -44,10 +43,4 @@ pub trait Append: fmt::Debug + Send + Sync + 'static {
fn default_layout(&self) -> Layout {
Layout::Identical(IdenticalLayout)
}

/// Default filters associated to this append. [log::Log] is mixed with
/// [Filter] and [Append].
fn default_filters(&self) -> Vec<Filter> {
vec![]
}
}
32 changes: 3 additions & 29 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,9 @@ impl Dispatch<false, false> {
layout: Some(layout.into()),
}
}

pub fn append(self, append: impl Append) -> Dispatch<true, true> {
Dispatch {
filters: self.filters,
appends: vec![Box::new(append)],
layout: self.layout,
}
}
}

impl Dispatch<true, false> {
pub fn append(self, append: impl Append) -> Dispatch<true, true> {
Dispatch {
filters: self.filters,
appends: vec![Box::new(append)],
layout: self.layout,
}
}
}

impl Dispatch<true, true> {
impl<const LAYOUT: bool, const APPEND: bool> Dispatch<LAYOUT, APPEND> {
pub fn append(self, append: impl Append) -> Dispatch<true, true> {
Dispatch {
filters: self.filters,
Expand All @@ -99,18 +81,10 @@ impl Dispatch {
true
}

fn do_append(&self, record: &Record) -> anyhow::Result<()> {
fn log(&self, record: &Record) -> anyhow::Result<()> {
let layout = self.layout.as_ref();

for append in &self.appends {
for filter in append.default_filters() {
match filter.filter(record.metadata()) {
FilterResult::Reject => return Ok(()),
FilterResult::Accept => break,
FilterResult::Neutral => {}
}
}

match layout {
Some(layout) => layout.format(record, &|record| append.append(record))?,
None => append
Expand Down Expand Up @@ -165,7 +139,7 @@ impl log::Log for Logger {
fn log(&self, record: &Record) {
for dispatch in &self.dispatches {
if dispatch.enabled(record.metadata()) {
if let Err(err) = dispatch.do_append(record) {
if let Err(err) = dispatch.log(record) {
handle_error(record, err);
}
}
Expand Down

0 comments on commit ae6faf5

Please sign in to comment.