From f30608e6f153a481dafc2c2704337489c5b3bb3c Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Wed, 5 Apr 2023 13:45:26 +0200 Subject: [PATCH] feat(dtmm): Enable colors for regular log lines --- Cargo.lock | 10 ++++++++++ crates/dtmm/src/util/log.rs | 2 -- lib/dtmt-shared/Cargo.toml | 1 + lib/dtmt-shared/src/log.rs | 26 ++++++++++++++++++++++++-- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 547e72e..ba688e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,6 +48,15 @@ dependencies = [ "nom 4.2.3", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "anyhow" version = "1.0.69" @@ -897,6 +906,7 @@ dependencies = [ name = "dtmt-shared" version = "0.1.0" dependencies = [ + "ansi_term", "color-eyre", "serde", "steamlocate", diff --git a/crates/dtmm/src/util/log.rs b/crates/dtmm/src/util/log.rs index 97bb9a5..75db550 100644 --- a/crates/dtmm/src/util/log.rs +++ b/crates/dtmm/src/util/log.rs @@ -47,8 +47,6 @@ pub fn create_tracing_subscriber(tx: UnboundedSender>) { }; let channel_layer = fmt::layer() - // TODO: Re-enable and implement a formatter for the Druid widget - .with_ansi(false) .event_format(dtmt_shared::Formatter) .fmt_fields(debug_fn(dtmt_shared::format_fields)) .with_writer(move || ChannelWriter::new(tx.clone())) diff --git a/lib/dtmt-shared/Cargo.toml b/lib/dtmt-shared/Cargo.toml index eb9591d..4412266 100644 --- a/lib/dtmt-shared/Cargo.toml +++ b/lib/dtmt-shared/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ansi_term = "0.12.1" color-eyre = "0.6.2" serde = "1.0.152" steamlocate = { path = "../../lib/steamlocate-rs", version = "*" } diff --git a/lib/dtmt-shared/src/log.rs b/lib/dtmt-shared/src/log.rs index 3c46a4b..ab0b7b5 100644 --- a/lib/dtmt-shared/src/log.rs +++ b/lib/dtmt-shared/src/log.rs @@ -1,10 +1,11 @@ use std::fmt::Result; +use ansi_term::Color; use time::format_description::FormatItem; use time::macros::format_description; use time::OffsetDateTime; use tracing::field::Field; -use tracing::{Event, Metadata, Subscriber}; +use tracing::{Event, Level, Metadata, Subscriber}; use tracing_error::ErrorLayer; use tracing_subscriber::filter::FilterFn; use tracing_subscriber::fmt::format::{debug_fn, Writer}; @@ -49,7 +50,28 @@ where let time = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc()); let time = time.format(TIME_FORMAT).map_err(|_| std::fmt::Error)?; - write!(writer, "[{}] [{:>5}] ", time, meta.level())?; + let level = meta.level(); + // Sadly, tracing's `Level` is a struct, not an enum, so we can't properly `match` it. + let color = if *level == Level::TRACE { + Color::Purple + } else if *level == Level::DEBUG { + Color::Blue + } else if *level == Level::INFO { + Color::Green + } else if *level == Level::WARN { + Color::Yellow + } else if *level == Level::ERROR { + Color::Red + } else { + unreachable!() + }; + + write!( + writer, + "[{}] [{:>5}] ", + time, + color.bold().paint(format!("{}", level)) + )?; ctx.field_format().format_fields(writer.by_ref(), event)?;