Skip to content

Commit

Permalink
refactor: opt out colored (#1)
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 9794710 commit ae7d62b
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 70 deletions.
20 changes: 14 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,28 @@ name = "logforth"
authors = ["tison <[email protected]>"]
description = "A versatile and extensible logging implementation."
edition = "2021"
homepage = "https://github.com/tisonkun/logforth"
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/tisonkun/logforth"
rust-version = "1.71.0"
version = "0.1.0"
homepage = "https://github.com/tisonkun/logforth"
repository = "https://github.com/tisonkun/logforth"
readme = "README.md"

[features]
#colored = ["dep:colored"]
colored = ["dep:colored"]

[dependencies]
anyhow = { version = "1.0" }
#colored = { version = "2.1", optional = true }
colored = { version = "2.1" }
colored = { version = "2.1", optional = true }
humantime = { version = "2.1" }
log = { version = "0.4", features = ["std", "kv_unstable"] }
paste = { version = "1.0" }

[[example]]
name = "simple_stdio"
path = "examples/simple_stdio.rs"

[[example]]
name = "colored_stdio"
path = "examples/colored_stdio.rs"
required-features = ["colored"]
32 changes: 32 additions & 0 deletions examples/colored_stdio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 tison <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use log::LevelFilter;
use logforth::ColoredSimpleTextLayout;
use logforth::DispatchAppend;
use logforth::LogLevelFilter;
use logforth::Logger;
use logforth::StdoutAppend;

fn main() {
let append = StdoutAppend::new().with_layout(ColoredSimpleTextLayout::default());
let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace));
Logger::new().add_append(append).apply().unwrap();

log::error!("Hello error!");
log::warn!("Hello warn!");
log::info!("Hello info!");
log::debug!("Hello debug!");
log::trace!("Hello trace!");
}
13 changes: 3 additions & 10 deletions examples/simple_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@
// limitations under the License.

use log::LevelFilter;
use logforth::ColoredSimpleTextLayout;
use logforth::DispatchAppend;
use logforth::LogLevelFilter;
use logforth::Logger;
use logforth::StdoutAppend;

fn main() {
Logger::new()
.add_append(
DispatchAppend::new(
StdoutAppend::new().with_layout(ColoredSimpleTextLayout::default()),
)
.filter(LogLevelFilter::new(LevelFilter::Trace)),
)
.apply()
.unwrap();
let append = StdoutAppend::new();
let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace));
Logger::new().add_append(append).apply().unwrap();

log::error!("Hello error!");
log::warn!("Hello warn!");
Expand Down
6 changes: 6 additions & 0 deletions src/append/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ impl Append for DispatchAppend {
}
}
}

impl From<DispatchAppend> for AppendImpl {
fn from(append: DispatchAppend) -> Self {
AppendImpl::Dispatch(append)
}
}
42 changes: 21 additions & 21 deletions src/append/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,28 @@ pub enum AppendImpl {
Stderr(StderrAppend),
}

macro_rules! enum_dispatch_append {
($($name:ident),+) => {
impl Append for AppendImpl {
fn enabled(&self, metadata: &Metadata) -> bool {
match self { $( AppendImpl::$name(append) => append.enabled(metadata), )+ }
}

fn try_append(&self, record: &Record) -> anyhow::Result<()> {
match self { $( AppendImpl::$name(append) => append.try_append(record), )+ }
}
impl Append for AppendImpl {
fn enabled(&self, metadata: &Metadata) -> bool {
match self {
AppendImpl::Dispatch(append) => append.enabled(metadata),
AppendImpl::Stdout(append) => append.enabled(metadata),
AppendImpl::Stderr(append) => append.enabled(metadata),
}
}

fn flush(&self) {
match self { $( AppendImpl::$name(append) => append.flush(), )+ }
}
fn try_append(&self, record: &Record) -> anyhow::Result<()> {
match self {
AppendImpl::Dispatch(append) => append.try_append(record),
AppendImpl::Stdout(append) => append.try_append(record),
AppendImpl::Stderr(append) => append.try_append(record),
}
}

$(paste::paste! {
impl From<[<$name Append>]> for AppendImpl {
fn from(append: [<$name Append>]) -> Self { AppendImpl::$name(append) }
}
})+
};
fn flush(&self) {
match self {
AppendImpl::Dispatch(append) => append.flush(),
AppendImpl::Stdout(append) => append.flush(),
AppendImpl::Stderr(append) => append.flush(),
}
}
}

enum_dispatch_append!(Dispatch, Stdout, Stderr);
13 changes: 13 additions & 0 deletions src/append/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use std::io::Write;

use crate::Append;
use crate::AppendImpl;
use crate::Layout;
use crate::LayoutImpl;
use crate::SimpleTextLayout;
Expand Down Expand Up @@ -55,6 +56,12 @@ impl Append for StdoutAppend {
}
}

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

#[derive(Debug)]
pub struct StderrAppend {
layout: LayoutImpl,
Expand Down Expand Up @@ -90,3 +97,9 @@ impl Append for StderrAppend {
let _ = std::io::stderr().flush();
}
}

impl From<StderrAppend> for AppendImpl {
fn from(append: StderrAppend) -> Self {
AppendImpl::Stderr(append)
}
}
7 changes: 7 additions & 0 deletions src/filter/log_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use log::Metadata;

use crate::filter::Filter;
use crate::filter::FilterResult;
use crate::FilterImpl;

#[derive(Debug, Clone)]
pub struct LogLevelFilter {
Expand Down Expand Up @@ -69,3 +70,9 @@ impl Filter for LogLevelFilter {
}
}
}

impl From<LogLevelFilter> for FilterImpl {
fn from(filter: LogLevelFilter) -> Self {
FilterImpl::LogLevel(filter)
}
}
28 changes: 10 additions & 18 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,16 @@ pub enum FilterImpl {
LogLevel(LogLevelFilter),
}

macro_rules! enum_dispatch_filter {
($($name:ident),+) => {
impl Filter for FilterImpl {
fn filter(&self, record: &Record) -> FilterResult {
match self { $( FilterImpl::$name(filter) => filter.filter(record), )+ }
}

fn filter_metadata(&self, metadata: &Metadata) -> FilterResult {
match self { $( FilterImpl::$name(filter) => filter.filter_metadata(metadata), )+ }
}
impl Filter for FilterImpl {
fn filter(&self, record: &Record) -> FilterResult {
match self {
FilterImpl::LogLevel(filter) => filter.filter(record),
}
}

$(paste::paste! {
impl From<[<$name Filter>]> for FilterImpl {
fn from(filter: [<$name Filter>]) -> Self { FilterImpl::$name(filter) }
}
})+
};
fn filter_metadata(&self, metadata: &Metadata) -> FilterResult {
match self {
FilterImpl::LogLevel(filter) => filter.filter_metadata(metadata),
}
}
}

enum_dispatch_filter!(LogLevel);
7 changes: 7 additions & 0 deletions src/layout/colored_simple_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use log::Record;

use crate::layout::kv_display::KvDisplay;
use crate::Layout;
use crate::LayoutImpl;

#[derive(Default, Debug, Clone)]
pub struct ColoredSimpleTextLayout {
Expand Down Expand Up @@ -79,3 +80,9 @@ impl Layout for ColoredSimpleTextLayout {
Ok(text.into_bytes())
}
}

impl From<ColoredSimpleTextLayout> for LayoutImpl {
fn from(layout: ColoredSimpleTextLayout) -> Self {
LayoutImpl::ColoredSimpleText(layout)
}
}
25 changes: 10 additions & 15 deletions src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(feature = "colored")]
pub use colored_simple_text::ColoredSimpleTextLayout;
use log::Record;
pub use simple_text::SimpleTextLayout;

#[cfg(feature = "colored")]
mod colored_simple_text;
mod kv_display;
mod simple_text;
Expand All @@ -27,23 +29,16 @@ pub trait Layout {
#[derive(Debug)]
pub enum LayoutImpl {
SimpleText(SimpleTextLayout),
#[cfg(feature = "colored")]
ColoredSimpleText(ColoredSimpleTextLayout),
}

macro_rules! enum_dispatch_layout {
($($name:ident),+) => {
impl Layout for LayoutImpl {
fn format_bytes(&self, record: &Record) -> anyhow::Result<Vec<u8>> {
match self { $( LayoutImpl::$name(layout) => layout.format_bytes(record), )+ }
}
impl Layout for LayoutImpl {
fn format_bytes(&self, record: &Record) -> anyhow::Result<Vec<u8>> {
match self {
LayoutImpl::SimpleText(layout) => layout.format_bytes(record),
#[cfg(feature = "colored")]
LayoutImpl::ColoredSimpleText(layout) => layout.format_bytes(record),
}

$(paste::paste! {
impl From<[<$name Layout>]> for LayoutImpl {
fn from(layout: [<$name Layout>]) -> Self { LayoutImpl::$name(layout) }
}
})+
};
}
}

enum_dispatch_layout!(SimpleText, ColoredSimpleText);
7 changes: 7 additions & 0 deletions src/layout/simple_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use log::Record;

use crate::layout::kv_display::KvDisplay;
use crate::Layout;
use crate::LayoutImpl;

#[derive(Debug, Copy, Clone)]
pub struct SimpleTextLayout;
Expand All @@ -42,3 +43,9 @@ impl Layout for SimpleTextLayout {
Ok(text.into_bytes())
}
}

impl From<SimpleTextLayout> for LayoutImpl {
fn from(layout: SimpleTextLayout) -> Self {
LayoutImpl::SimpleText(layout)
}
}

0 comments on commit ae7d62b

Please sign in to comment.