Skip to content

Commit

Permalink
subscriber: propagate ANSI config via Writer (#1696)
Browse files Browse the repository at this point in the history
This backports PR #1696 to v0.1.x.

## Motivation

Currently, whether `tracing-subscriber`'s `fmt` subscriber will ANSI
formatting escape codes use is configured on the `Format` type. This
means that the configuration is honored by the event formatters
implemented in `tracing-subscriber`, but is not easily exposed to those
in other crates. Additionally, it's not currently easy to expose the
configuration to the field formatter, so it's difficult to implement
field formatters that use ANSI escape codes conditionally.

Issue #1651 suggested a new API for this, where the writer that's passed
in to the event and field formatters provides a method for checking if
ANSI escape codes are supported.

## Solution

This branch adds a new method to the `Writer` type added in #1661. The
`FormatEvent` and `FormatFields` implementations can call
`Writer::has_ansi_escapes` to determine whether ANSI escape codes are
supported. This is also propagated to `FormattedFields`, so that it can
be determined when adding new fields to a preexisting set of formatted
fields.

Fixes #1651

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Nov 8, 2021
1 parent a39261f commit 0a16972
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 161 deletions.
49 changes: 35 additions & 14 deletions tracing-subscriber/src/fmt/fmt_layer.rs
Expand Up @@ -69,6 +69,7 @@ pub struct Layer<
fmt_fields: N,
fmt_event: E,
fmt_span: format::FmtSpanConfig,
is_ansi: bool,
_inner: PhantomData<S>,
}

Expand Down Expand Up @@ -117,6 +118,7 @@ where
fmt_event: e,
fmt_span: self.fmt_span,
make_writer: self.make_writer,
is_ansi: self.is_ansi,
_inner: self._inner,
}
}
Expand Down Expand Up @@ -151,6 +153,7 @@ impl<S, N, E, W> Layer<S, N, E, W> {
fmt_fields: self.fmt_fields,
fmt_event: self.fmt_event,
fmt_span: self.fmt_span,
is_ansi: self.is_ansi,
make_writer,
_inner: self._inner,
}
Expand Down Expand Up @@ -183,10 +186,21 @@ impl<S, N, E, W> Layer<S, N, E, W> {
fmt_fields: self.fmt_fields,
fmt_event: self.fmt_event,
fmt_span: self.fmt_span,
is_ansi: self.is_ansi,
make_writer: TestWriter::default(),
_inner: self._inner,
}
}

/// Enable ANSI terminal colors for formatted output.
#[cfg(feature = "ansi")]
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
pub fn with_ansi(self, ansi: bool) -> Self {
Self {
is_ansi: ansi,
..self
}
}
}

impl<S, N, L, T, W> Layer<S, N, format::Format<L, T>, W>
Expand All @@ -213,6 +227,7 @@ where
fmt_fields: self.fmt_fields,
fmt_span: self.fmt_span,
make_writer: self.make_writer,
is_ansi: self.is_ansi,
_inner: self._inner,
}
}
Expand All @@ -224,6 +239,7 @@ where
fmt_fields: self.fmt_fields,
fmt_span: self.fmt_span.without_time(),
make_writer: self.make_writer,
is_ansi: self.is_ansi,
_inner: self._inner,
}
}
Expand Down Expand Up @@ -276,16 +292,6 @@ where
}
}

/// Enable ANSI encoding for formatted events.
#[cfg(feature = "ansi")]
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
pub fn with_ansi(self, ansi: bool) -> Layer<S, N, format::Format<L, T>, W> {
Layer {
fmt_event: self.fmt_event.with_ansi(ansi),
..self
}
}

/// Sets whether or not an event's target is displayed.
pub fn with_target(self, display_target: bool) -> Layer<S, N, format::Format<L, T>, W> {
Layer {
Expand Down Expand Up @@ -337,6 +343,7 @@ where
fmt_fields: self.fmt_fields,
fmt_span: self.fmt_span,
make_writer: self.make_writer,
is_ansi: self.is_ansi,
_inner: self._inner,
}
}
Expand All @@ -350,6 +357,7 @@ where
fmt_fields: format::Pretty::default(),
fmt_span: self.fmt_span,
make_writer: self.make_writer,
is_ansi: self.is_ansi,
_inner: self._inner,
}
}
Expand Down Expand Up @@ -378,6 +386,8 @@ where
fmt_fields: format::JsonFields::new(),
fmt_span: self.fmt_span,
make_writer: self.make_writer,
// always disable ANSI escapes in JSON mode!
is_ansi: false,
_inner: self._inner,
}
}
Expand Down Expand Up @@ -443,6 +453,7 @@ impl<S, N, E, W> Layer<S, N, E, W> {
fmt_fields,
fmt_span: self.fmt_span,
make_writer: self.make_writer,
is_ansi: self.is_ansi,
_inner: self._inner,
}
}
Expand All @@ -455,6 +466,7 @@ impl<S> Default for Layer<S> {
fmt_event: format::Format::default(),
fmt_span: format::FmtSpanConfig::default(),
make_writer: io::stdout,
is_ansi: cfg!(feature = "ansi"),
_inner: PhantomData,
}
}
Expand Down Expand Up @@ -488,6 +500,7 @@ where
#[derive(Default)]
pub struct FormattedFields<E: ?Sized> {
_format_fields: PhantomData<fn(E)>,
was_ansi: bool,
/// The formatted fields of a span.
pub fields: String,
}
Expand All @@ -497,6 +510,7 @@ impl<E: ?Sized> FormattedFields<E> {
pub fn new(fields: String) -> Self {
Self {
fields,
was_ansi: false,
_format_fields: PhantomData,
}
}
Expand All @@ -506,7 +520,7 @@ impl<E: ?Sized> FormattedFields<E> {
/// The returned [`format::Writer`] can be used with the
/// [`FormatFields::format_fields`] method.
pub fn as_writer(&mut self) -> format::Writer<'_> {
format::Writer::new(&mut self.fields)
format::Writer::new(&mut self.fields).with_ansi(self.was_ansi)
}
}

Expand All @@ -515,6 +529,7 @@ impl<E: ?Sized> fmt::Debug for FormattedFields<E> {
f.debug_struct("FormattedFields")
.field("fields", &self.fields)
.field("formatter", &format_args!("{}", std::any::type_name::<E>()))
.field("was_ansi", &self.was_ansi)
.finish()
}
}
Expand Down Expand Up @@ -565,9 +580,10 @@ where
let mut fields = FormattedFields::<N>::new(String::new());
if self
.fmt_fields
.format_fields(fields.as_writer(), attrs)
.format_fields(fields.as_writer().with_ansi(self.is_ansi), attrs)
.is_ok()
{
fields.was_ansi = self.is_ansi;
extensions.insert(fields);
}
}
Expand Down Expand Up @@ -599,9 +615,10 @@ where
let mut fields = FormattedFields::<N>::new(String::new());
if self
.fmt_fields
.format_fields(fields.as_writer(), values)
.format_fields(fields.as_writer().with_ansi(self.is_ansi), values)
.is_ok()
{
fields.was_ansi = self.is_ansi;
extensions.insert(fields);
}
}
Expand Down Expand Up @@ -706,7 +723,11 @@ where
let ctx = self.make_ctx(ctx);
if self
.fmt_event
.format_event(&ctx, format::Writer::new(&mut buf), event)
.format_event(
&ctx,
format::Writer::new(&mut buf).with_ansi(self.is_ansi),
event,
)
.is_ok()
{
let mut writer = self.make_writer.make_writer_for(event.metadata());
Expand Down

0 comments on commit 0a16972

Please sign in to comment.