feat(dtmm): Enable colors for regular log lines

This commit is contained in:
Lucas Schwiderski 2023-04-05 13:45:26 +02:00
parent c4425f5b6b
commit f30608e6f1
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8
4 changed files with 35 additions and 4 deletions

10
Cargo.lock generated
View file

@ -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",

View file

@ -47,8 +47,6 @@ pub fn create_tracing_subscriber(tx: UnboundedSender<Vec<u8>>) {
};
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()))

View file

@ -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 = "*" }

View file

@ -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)?;