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

subscriber: use ANSI formatting in field formatters #1702

Merged
merged 8 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
112 changes: 76 additions & 36 deletions tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,39 @@ impl<'writer> Writer<'writer> {
pub fn has_ansi_escapes(&self) -> bool {
self.is_ansi
}

pub(in crate::fmt::format) fn bold(&self) -> Style {
#[cfg(feature = "ansi")]
{
if self.is_ansi {
return Style::new().bold();
}
}

Style::new()
}

pub(in crate::fmt::format) fn dimmed(&self) -> Style {
#[cfg(feature = "ansi")]
{
if self.is_ansi {
return Style::new().dimmed();
}
}

Style::new()
}

pub(in crate::fmt::format) fn italic(&self) -> Style {
#[cfg(feature = "ansi")]
{
if self.is_ansi {
return Style::new().italic();
}
}

Style::new()
}
}

impl fmt::Write for Writer<'_> {
Expand Down Expand Up @@ -654,17 +687,6 @@ impl<F, T> Format<F, T> {
}
writer.write_char(' ')
}

fn bold(&self, is_ansi: bool) -> Style {
#[cfg(feature = "ansi")]
{
if self.ansi.unwrap_or(true) && is_ansi {
return Style::new().bold();
}
}

Style::new()
}
}

#[cfg(feature = "json")]
Expand Down Expand Up @@ -756,8 +778,11 @@ where
write!(writer, "{:0>2?} ", std::thread::current().id())?;
}

let dimmed = writer.dimmed();

if let Some(scope) = ctx.ctx.event_scope(event) {
let bold = self.bold(writer.has_ansi_escapes());
let bold = writer.bold();

let mut seen = false;

for span in scope.from_root() {
Expand All @@ -770,7 +795,7 @@ where
write!(writer, "{}{}{}", bold.paint("{"), fields, bold.paint("}"))?;
}
}
writer.write_char(':')?;
write!(writer, "{}", dimmed.paint(":"))?;
}

if seen {
Expand All @@ -779,7 +804,12 @@ where
}

if self.display_target {
write!(writer, "{}: ", meta.target())?;
write!(
writer,
"{}{} ",
dimmed.paint(meta.target()),
dimmed.paint(":")
)?;
}

ctx.format_fields(writer.by_ref(), event)?;
Expand Down Expand Up @@ -828,25 +858,17 @@ where
}

if self.display_target {
let target = meta.target();
#[cfg(feature = "ansi")]
let target = if writer.has_ansi_escapes() {
Style::new().bold().paint(target)
} else {
Style::new().paint(target)
};

write!(writer, "{}:", target)?;
write!(
writer,
"{}{}",
writer.bold().paint(meta.target()),
writer.dimmed().paint(":")
)?;
}

ctx.format_fields(writer.by_ref(), event)?;

#[cfg(feature = "ansi")]
let dimmed = if writer.has_ansi_escapes() {
Style::new().dimmed()
} else {
Style::new()
};
let dimmed = writer.dimmed();
for span in ctx
.ctx
.event_scope(event)
Expand All @@ -857,9 +879,7 @@ where
let exts = span.extensions();
if let Some(fields) = exts.get::<FormattedFields<N>>() {
if !fields.is_empty() {
#[cfg(feature = "ansi")]
let fields = dimmed.paint(fields.as_str());
write!(writer, " {}", fields)?;
write!(writer, " {}", dimmed.paint(&fields.fields))?;
}
}
}
Expand Down Expand Up @@ -965,9 +985,17 @@ impl<'a> field::Visit for DefaultVisitor<'a> {

fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) {
if let Some(source) = value.source() {
let italic = self.writer.italic();
self.record_debug(
field,
&format_args!("{} {}.sources={}", value, field, ErrorSourceList(source)),
&format_args!(
"{} {}{}{}{}",
value,
italic.paint(field.name()),
italic.paint(".sources"),
self.writer.dimmed().paint("="),
ErrorSourceList(source)
),
)
} else {
self.record_debug(field, &format_args!("{}", value))
Expand All @@ -985,8 +1013,20 @@ impl<'a> field::Visit for DefaultVisitor<'a> {
// Skip fields that are actually log metadata that have already been handled
#[cfg(feature = "tracing-log")]
name if name.starts_with("log.") => Ok(()),
name if name.starts_with("r#") => write!(self.writer, "{}={:?}", &name[2..], value),
name => write!(self.writer, "{}={:?}", name, value),
name if name.starts_with("r#") => write!(
self.writer,
"{}{}{:?}",
self.writer.italic().paint(&name[2..]),
self.writer.dimmed().paint("="),
value
),
name => write!(
self.writer,
"{}{}{:?}",
self.writer.italic().paint(name),
self.writer.dimmed().paint("="),
value
),
};
}
}
Expand Down Expand Up @@ -1405,7 +1445,7 @@ pub(super) mod test {
#[cfg(feature = "ansi")]
#[test]
fn with_ansi_true() {
let expected = "\u{1b}[2mfake time\u{1b}[0m \u{1b}[32m INFO\u{1b}[0m tracing_subscriber::fmt::format::test: hello\n";
let expected = "\u{1b}[2mfake time\u{1b}[0m \u{1b}[32m INFO\u{1b}[0m \u{1b}[2mtracing_subscriber::fmt::format::test\u{1b}[0m\u{1b}[2m:\u{1b}[0m hello\n";
test_ansi(true, expected);
}

Expand Down
6 changes: 1 addition & 5 deletions tracing-subscriber/src/fmt/format/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ where
writer.write_char('\n')?;
}

let bold = if writer.has_ansi_escapes() {
Style::new().bold()
} else {
Style::new()
};
let bold = writer.bold();
let span = event
.parent()
.and_then(|id| ctx.span(id))
Expand Down