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

refactor: opt out colored #1

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ 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"
required-features = ["colored"]
9 changes: 8 additions & 1 deletion 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,18 +29,23 @@ 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), )+ }
match self { $(
// opt #[cfg(feature = "colored")]
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
LayoutImpl::$name(layout) => layout.format_bytes(record),
)+ }
}
}

$(paste::paste! {
// opt #[cfg(feature = "colored")]
impl From<[<$name Layout>]> for LayoutImpl {
fn from(layout: [<$name Layout>]) -> Self { LayoutImpl::$name(layout) }
}
Expand Down
Loading