Skip to content

Commit

Permalink
refactor: tidy up append interface and DynLog (#10)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Aug 1, 2024
1 parent d162c8e commit 4be7d02
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 74 deletions.
15 changes: 10 additions & 5 deletions src/append/boxdyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@

use std::fmt::Debug;

use log::Metadata;
use log::Record;

use crate::append::Append;
use crate::append::AppendImpl;
use crate::filter::FilterImpl;
use crate::layout::LayoutImpl;

pub struct BoxDyn(Box<dyn Append + Send + Sync>);

Expand All @@ -35,17 +36,21 @@ impl Debug for BoxDyn {
}

impl Append for BoxDyn {
fn enabled(&self, metadata: &Metadata) -> bool {
(*self.0).enabled(metadata)
}

fn try_append(&self, record: &Record) -> anyhow::Result<()> {
(*self.0).try_append(record)
}

fn flush(&self) {
(*self.0).flush()
}

fn default_layout(&self) -> LayoutImpl {
(*self.0).default_layout()
}

fn default_filters(&self) -> Option<Vec<FilterImpl>> {
(*self.0).default_filters()
}
}

impl From<BoxDyn> for AppendImpl {
Expand Down
65 changes: 39 additions & 26 deletions src/append/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::dynlog::DynLog;
use crate::filter::FilterImpl;
use crate::layout;
use crate::layout::LayoutImpl;
pub use boxdyn::*;
pub use boxlog::*;
#[cfg(feature = "fastrace")]
pub use fastrace::*;
#[cfg(feature = "file")]
pub use file::*;
pub use stdio::*;

use crate::layout;
use crate::layout::LayoutImpl;

mod boxdyn;
mod boxlog;
#[cfg(feature = "fastrace")]
mod fastrace;
#[cfg(feature = "file")]
mod file;
mod stdio;

pub trait Append {
/// Whether this append is enabled; default to `true`.
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
}

/// Dispatches a log record to the append target.
fn try_append(&self, record: &log::Record) -> anyhow::Result<()>;

Expand All @@ -48,12 +42,18 @@ pub trait Append {
fn default_layout(&self) -> LayoutImpl {
LayoutImpl::Identical(layout::Identical)
}

/// Default filters associated to this append. [log::Log] is mixed with
/// [Filter][crate::filter::Filter] and [Append].
fn default_filters(&self) -> Option<Vec<FilterImpl>> {
None
}
}

#[derive(Debug)]
pub enum AppendImpl {
BoxDyn(BoxDyn),
BoxLog(BoxLog),
DynLog(DynLog),
#[cfg(feature = "fastrace")]
Fastrace(Fastrace),
#[cfg(feature = "file")]
Expand All @@ -63,23 +63,10 @@ pub enum AppendImpl {
}

impl Append for AppendImpl {
fn enabled(&self, metadata: &log::Metadata) -> bool {
match self {
AppendImpl::BoxDyn(append) => append.enabled(metadata),
AppendImpl::BoxLog(append) => append.enabled(metadata),
#[cfg(feature = "fastrace")]
AppendImpl::Fastrace(append) => append.enabled(metadata),
#[cfg(feature = "file")]
AppendImpl::RollingFile(append) => append.enabled(metadata),
AppendImpl::Stdout(append) => append.enabled(metadata),
AppendImpl::Stderr(append) => append.enabled(metadata),
}
}

fn try_append(&self, record: &log::Record) -> anyhow::Result<()> {
match self {
AppendImpl::BoxDyn(append) => append.try_append(record),
AppendImpl::BoxLog(append) => append.try_append(record),
AppendImpl::DynLog(append) => append.try_append(record),
#[cfg(feature = "fastrace")]
AppendImpl::Fastrace(append) => append.try_append(record),
#[cfg(feature = "file")]
Expand All @@ -92,7 +79,7 @@ impl Append for AppendImpl {
fn flush(&self) {
match self {
AppendImpl::BoxDyn(append) => append.flush(),
AppendImpl::BoxLog(append) => append.flush(),
AppendImpl::DynLog(append) => append.flush(),
#[cfg(feature = "fastrace")]
AppendImpl::Fastrace(append) => append.flush(),
#[cfg(feature = "file")]
Expand All @@ -101,4 +88,30 @@ impl Append for AppendImpl {
AppendImpl::Stderr(append) => append.flush(),
}
}

fn default_layout(&self) -> LayoutImpl {
match self {
AppendImpl::BoxDyn(append) => append.default_layout(),
AppendImpl::DynLog(append) => append.default_layout(),
#[cfg(feature = "fastrace")]
AppendImpl::Fastrace(append) => append.default_layout(),
#[cfg(feature = "file")]
AppendImpl::RollingFile(append) => append.default_layout(),
AppendImpl::Stdout(append) => append.default_layout(),
AppendImpl::Stderr(append) => append.default_layout(),
}
}

fn default_filters(&self) -> Option<Vec<FilterImpl>> {
match self {
AppendImpl::BoxDyn(append) => append.default_filters(),
AppendImpl::DynLog(append) => append.default_filters(),
#[cfg(feature = "fastrace")]
AppendImpl::Fastrace(append) => append.default_filters(),
#[cfg(feature = "file")]
AppendImpl::RollingFile(append) => append.default_filters(),
AppendImpl::Stdout(append) => append.default_filters(),
AppendImpl::Stderr(append) => append.default_filters(),
}
}
}
52 changes: 36 additions & 16 deletions src/append/boxlog.rs → src/dynlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,34 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt::Debug;

use log::Log;
use log::Metadata;
use log::Record;
use log::{Log, Metadata};
use std::fmt::Debug;
use std::sync::Arc;

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

pub struct BoxLog(Box<dyn Log>);
pub struct DynLog(Arc<dyn Log>);

impl Debug for BoxLog {
impl Debug for DynLog {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "BoxLogAppend {{ ... }}")
write!(f, "DynLog {{ ... }}")
}
}

impl BoxLog {
impl DynLog {
pub fn new(log: impl Log + 'static) -> Self {
Self(Box::new(log))
Self(Arc::new(log))
}
}

impl Append for BoxLog {
fn enabled(&self, metadata: &Metadata) -> bool {
(*self.0).enabled(metadata)
pub fn new_arc(log: Arc<dyn Log>) -> Self {
Self(log)
}
}

impl Append for DynLog {
fn try_append(&self, record: &Record) -> anyhow::Result<()> {
(*self.0).log(record);
Ok(())
Expand All @@ -48,10 +48,30 @@ impl Append for BoxLog {
fn flush(&self) {
(*self.0).flush()
}

fn default_filters(&self) -> Option<Vec<FilterImpl>> {
Some(vec![Self::new_arc(self.0.clone()).into()])
}
}

impl Filter for DynLog {
fn filter_metadata(&self, metadata: &Metadata) -> FilterResult {
if self.0.enabled(metadata) {
FilterResult::Neutral
} else {
FilterResult::Reject
}
}
}

impl From<DynLog> for AppendImpl {
fn from(append: DynLog) -> Self {
AppendImpl::DynLog(append)
}
}

impl From<BoxLog> for AppendImpl {
fn from(append: BoxLog) -> Self {
AppendImpl::BoxLog(append)
impl From<DynLog> for FilterImpl {
fn from(filter: DynLog) -> Self {
FilterImpl::DynLog(filter)
}
}
5 changes: 0 additions & 5 deletions src/filter/boxdyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use std::fmt::Debug;

use log::Metadata;
use log::Record;

use crate::filter::Filter;
use crate::filter::FilterImpl;
Expand All @@ -36,10 +35,6 @@ impl BoxDyn {
}

impl Filter for BoxDyn {
fn filter(&self, record: &Record) -> FilterResult {
(*self.0).filter(record)
}

fn filter_metadata(&self, metadata: &Metadata) -> FilterResult {
(*self.0).filter_metadata(metadata)
}
Expand Down
14 changes: 3 additions & 11 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::dynlog::DynLog;
pub use boxdyn::BoxDyn;
pub use log_level::LogLevel;

Expand All @@ -29,30 +30,21 @@ pub enum FilterResult {
}

pub trait Filter {
fn filter(&self, _record: &log::Record) -> FilterResult {
FilterResult::Neutral
}

fn filter_metadata(&self, metadata: &log::Metadata) -> FilterResult;
}

#[derive(Debug)]
pub enum FilterImpl {
BoxDyn(BoxDyn),
DynLog(DynLog),
LogLevel(LogLevel),
}

impl Filter for FilterImpl {
fn filter(&self, record: &log::Record) -> FilterResult {
match self {
FilterImpl::BoxDyn(filter) => filter.filter(record),
FilterImpl::LogLevel(filter) => filter.filter(record),
}
}

fn filter_metadata(&self, metadata: &log::Metadata) -> FilterResult {
match self {
FilterImpl::BoxDyn(filter) => filter.filter_metadata(metadata),
FilterImpl::DynLog(filter) => filter.filter_metadata(metadata),
FilterImpl::LogLevel(filter) => filter.filter_metadata(metadata),
}
}
Expand Down
1 change: 0 additions & 1 deletion src/layout/identical.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use crate::layout::Layout;
use crate::layout::LayoutImpl;

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

pub mod append;
pub mod dynlog;
pub mod filter;
pub mod layout;
pub mod logger;
36 changes: 26 additions & 10 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,43 @@ impl Dispatch {
}

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 => {}
},
FilterResult::Neutral => {}
}
}

for append in &self.appends {
match self.preferred_layout.as_ref() {
Some(layout) => layout.format(record, |record| append.try_append(record))?,
fn do_append(
record: &Record,
append: &AppendImpl,
preferred_layout: Option<&LayoutImpl>,
) -> anyhow::Result<()> {
if let Some(filters) = append.default_filters() {
for filter in filters {
match filter.filter_metadata(record.metadata()) {
FilterResult::Reject => return Ok(()),
FilterResult::Accept => break,
FilterResult::Neutral => {}
}
}
}

match preferred_layout {
Some(layout) => layout.format(record, |record| append.try_append(record)),
None => append
.default_layout()
.format(record, |record| append.try_append(record))?,
.format(record, |record| append.try_append(record)),
}
}

let preferred_layout = self.preferred_layout.as_ref();

for append in &self.appends {
do_append(record, append, preferred_layout)?
}

Ok(())
}

Expand Down

0 comments on commit 4be7d02

Please sign in to comment.