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: reduce duplicated functions #20

Merged
merged 1 commit into from
Aug 1, 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
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