Skip to content

Commit

Permalink
dispatch
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Aug 1, 2024
1 parent 527c5b9 commit d0432e0
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 19 deletions.
6 changes: 1 addition & 5 deletions src/append/fastrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::time::SystemTime;
use log::Record;

use crate::append::{Append, AppendImpl};
use crate::layout::{IdenticalLayout, KvDisplay, LayoutImpl};
use crate::layout::KvDisplay;

#[derive(Default, Debug, Clone)]
pub struct FastraceAppend;
Expand All @@ -34,10 +34,6 @@ impl Append for FastraceAppend {
fastrace::Event::add_to_local_parent(message, || []);
Ok(())
}

fn preferred_layout(&self) -> Option<LayoutImpl> {
Some(LayoutImpl::Identical(IdenticalLayout))
}
}

impl From<FastraceAppend> for AppendImpl {
Expand Down
9 changes: 5 additions & 4 deletions src/append/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use fastrace::*;
#[cfg(feature = "file")]
pub use file::*;
pub use stdio::*;
use crate::layout::LayoutImpl;
use crate::layout::{IdenticalLayout, LayoutImpl};

mod boxdyn;
mod boxlog;
Expand All @@ -41,9 +41,10 @@ pub trait Append {
/// Flushes any buffered records.
fn flush(&self) {}

/// Preferred layout for this append. If set, override the context layout.
fn preferred_layout(&self) -> Option<LayoutImpl> {
None
/// Default layout to use when [Dispatch][crate::logger::Dispatch] does not configure a
/// preferred layout.
fn default_layout(&self) -> LayoutImpl {
LayoutImpl::Identical(IdenticalLayout)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Layout for LayoutImpl {
fn format_record(&self, record: &log::Record) -> anyhow::Result<log::Record> {
match self {
LayoutImpl::BoxDyn(layout) => layout.format_record(record),
LayoutImpl::Identical(layout) => record,
LayoutImpl::Identical(layout) => layout.format_record(record),
LayoutImpl::SimpleText(layout) => layout.format_record(record),
#[cfg(feature = "json")]
LayoutImpl::SimpleJson(layout) => layout.format_record(record),
Expand Down
99 changes: 90 additions & 9 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,89 @@ use log::Record;

use crate::append::Append;
use crate::append::AppendImpl;
use crate::filter::{Filter, FilterImpl, FilterResult};
use crate::layout::{Layout, LayoutImpl};

#[derive(Debug)]
pub struct Logger {
pub struct Dispatch {
filters: Vec<FilterImpl>,
appends: Vec<AppendImpl>,
preferred_layout: Option<LayoutImpl>,
}

impl Dispatch {
pub fn new() -> Self {
Self {
filters: vec![],
appends: vec![],
preferred_layout: None,
}
}

pub fn filter(mut self, filter: FilterImpl) -> Self {
self.filters.push(filter);
self
}

pub fn append(mut self, append: AppendImpl) -> Self {
self.appends.push(append);
self
}

pub fn layout(mut self, layout: LayoutImpl) -> Self {
self.preferred_layout = Some(layout);
self
}

fn enabled(&self, metadata: &Metadata) -> bool {
for filter in &self.filters {
match filter.filter_metadata(metadata) {
FilterResult::Reject => return false,
FilterResult::Accept => return true,
FilterResult::Neutral => {}
}
}

true
}

fn try_append(&self, record: &Record) -> anyhow::Result<()> {
// TODO(tisonkun): perhaps too heavy to check filters twice.
for filter in &self.filters {
match filter.filter_metadata(record.metadata()) {
FilterResult::Reject => return Ok(()),
FilterResult::Accept => break,
FilterResult::Neutral => match filter.filter(record) {
FilterResult::Reject => return Ok(()),
FilterResult::Accept => break,
FilterResult::Neutral => {}
},
}
}

for append in &self.appends {
let layout = self
.preferred_layout
.as_ref()
.unwrap_or(&append.default_layout());

layout
.format_record(record)
.and_then(|formatted| append.try_append(&formatted))?;
}
Ok(())
}

fn flush(&self) {
for append in &self.appends {
append.flush();
}
}
}

#[derive(Debug)]
pub struct Logger {
dispatches: Vec<Dispatch>,
}

impl Default for Logger {
Expand All @@ -34,11 +113,11 @@ impl Default for Logger {

impl Logger {
pub fn new() -> Self {
Self { appends: vec![] }
Self { dispatches: vec![] }
}

pub fn add_append(mut self, append: impl Into<AppendImpl>) -> Self {
self.appends.push(append.into());
pub fn dispatch(mut self, dispatch: Dispatch) -> Self {
self.dispatches.push(dispatch);
self
}

Expand All @@ -51,20 +130,22 @@ impl Logger {

impl log::Log for Logger {
fn enabled(&self, metadata: &Metadata) -> bool {
self.appends.iter().any(|append| append.enabled(metadata))
self.dispatches
.iter()
.any(|dispatch| dispatch.enabled(metadata))
}

fn log(&self, record: &Record) {
for append in &self.appends {
if let Err(err) = append.try_append(record) {
for dispatch in &self.dispatches {
if let Err(err) = dispatch.try_append(record) {
handle_error(record, err);
}
}
}

fn flush(&self) {
for append in &self.appends {
append.flush();
for dispatch in &self.dispatches {
dispatch.flush();
}
}
}
Expand Down

0 comments on commit d0432e0

Please sign in to comment.