Colorize log output #97
4 changed files with 35 additions and 4 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -48,6 +48,15 @@ dependencies = [
|
||||||
"nom 4.2.3",
|
"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]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.69"
|
version = "1.0.69"
|
||||||
|
@ -897,6 +906,7 @@ dependencies = [
|
||||||
name = "dtmt-shared"
|
name = "dtmt-shared"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"ansi_term",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"serde",
|
"serde",
|
||||||
"steamlocate",
|
"steamlocate",
|
||||||
|
|
|
@ -47,8 +47,6 @@ pub fn create_tracing_subscriber(tx: UnboundedSender<Vec<u8>>) {
|
||||||
};
|
};
|
||||||
|
|
||||||
let channel_layer = fmt::layer()
|
let channel_layer = fmt::layer()
|
||||||
// TODO: Re-enable and implement a formatter for the Druid widget
|
|
||||||
.with_ansi(false)
|
|
||||||
.event_format(dtmt_shared::Formatter)
|
.event_format(dtmt_shared::Formatter)
|
||||||
.fmt_fields(debug_fn(dtmt_shared::format_fields))
|
.fmt_fields(debug_fn(dtmt_shared::format_fields))
|
||||||
.with_writer(move || ChannelWriter::new(tx.clone()))
|
.with_writer(move || ChannelWriter::new(tx.clone()))
|
||||||
|
|
|
@ -6,6 +6,7 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
ansi_term = "0.12.1"
|
||||||
color-eyre = "0.6.2"
|
color-eyre = "0.6.2"
|
||||||
serde = "1.0.152"
|
serde = "1.0.152"
|
||||||
steamlocate = { path = "../../lib/steamlocate-rs", version = "*" }
|
steamlocate = { path = "../../lib/steamlocate-rs", version = "*" }
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
use std::fmt::Result;
|
use std::fmt::Result;
|
||||||
|
|
||||||
|
use ansi_term::Color;
|
||||||
use time::format_description::FormatItem;
|
use time::format_description::FormatItem;
|
||||||
use time::macros::format_description;
|
use time::macros::format_description;
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
use tracing::field::Field;
|
use tracing::field::Field;
|
||||||
use tracing::{Event, Metadata, Subscriber};
|
use tracing::{Event, Level, Metadata, Subscriber};
|
||||||
use tracing_error::ErrorLayer;
|
use tracing_error::ErrorLayer;
|
||||||
use tracing_subscriber::filter::FilterFn;
|
use tracing_subscriber::filter::FilterFn;
|
||||||
use tracing_subscriber::fmt::format::{debug_fn, Writer};
|
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 = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
|
||||||
let time = time.format(TIME_FORMAT).map_err(|_| std::fmt::Error)?;
|
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)?;
|
ctx.field_format().format_fields(writer.by_ref(), event)?;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue