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

draft: try to make Pretty's field formatter respect the writer's ansi setting #1747

Merged
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
4 changes: 4 additions & 0 deletions examples/examples/fmt-pretty.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#![deny(rust_2018_idioms)]

use tracing_subscriber::fmt::format::PrettyFields;
#[path = "fmt/yak_shave.rs"]
mod yak_shave;

fn main() {
tracing_subscriber::fmt()
.pretty()
.with_ansi(false)
.fmt_fields(PrettyFields::new())
.with_thread_names(true)
// enable everything
.with_max_level(tracing::Level::TRACE)
Expand Down
62 changes: 41 additions & 21 deletions tracing-subscriber/src/fmt/format/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ impl PrettyFields {
Self { ansi: true }
}

#[deprecated(
since = "0.3.3",
note = "Use `fmt::Subscriber::with_ansi` or `fmt::Collector::with_ansi` instead."
)]
/// Enable ANSI encoding for formatted fields.
pub fn with_ansi(self, ansi: bool) -> Self {
Self { ansi, ..self }
Expand Down Expand Up @@ -298,6 +302,7 @@ impl<'a> PrettyVisitor<'a> {
}

fn bold(&self) -> Style {
dbg!(self.writer.has_ansi_escapes());
if self.writer.has_ansi_escapes() {
self.style.bold()
} else {
Expand Down Expand Up @@ -342,27 +347,42 @@ impl<'a> field::Visit for PrettyVisitor<'a> {
if self.result.is_err() {
return;
}
let bold = self.bold();
match field.name() {
"message" => self.write_padded(&format_args!("{}{:?}", self.style.prefix(), value,)),
// Skip fields that are actually log metadata that have already been handled
#[cfg(feature = "tracing-log")]
name if name.starts_with("log.") => self.result = Ok(()),
name if name.starts_with("r#") => self.write_padded(&format_args!(
"{}{}{}: {:?}",
bold.prefix(),
&name[2..],
bold.infix(self.style),
value
)),
name => self.write_padded(&format_args!(
"{}{}{}: {:?}",
bold.prefix(),
name,
bold.infix(self.style),
value
)),
};
if self.writer.has_ansi_escapes() {
let bold = self.bold();
match field.name() {
"message" => {
self.write_padded(&format_args!("{}{:?}", self.style.prefix(), value,))
}
// Skip fields that are actually log metadata that have already been handled
#[cfg(feature = "tracing-log")]
name if name.starts_with("log.") => self.result = Ok(()),
name if name.starts_with("r#") => self.write_padded(&format_args!(
"{}{}{}: {:?}",
bold.prefix(),
&name[2..],
bold.infix(self.style),
value
)),
name => self.write_padded(&format_args!(
"{}{}{}: {:?}",
bold.prefix(),
name,
bold.infix(self.style),
value
)),
};
} else {
match field.name() {
"message" => self.write_padded(&format_args!("{:?}", value)),
// Skip fields that are actually log metadata that have already been handled
#[cfg(feature = "tracing-log")]
name if name.starts_with("log.") => self.result = Ok(()),
name if name.starts_with("r#") => {
self.write_padded(&format_args!("{}: {:?}", &name[2..], value))
}
name => self.write_padded(&format_args!("{}: {:?}", name, value)),
};
}
}
}

Expand Down