Skip to content

Commit 0a16972

Browse files
authored
subscriber: propagate ANSI config via Writer (#1696)
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>
1 parent a39261f commit 0a16972

File tree

3 files changed

+160
-161
lines changed

3 files changed

+160
-161
lines changed

tracing-subscriber/src/fmt/fmt_layer.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub struct Layer<
6969
fmt_fields: N,
7070
fmt_event: E,
7171
fmt_span: format::FmtSpanConfig,
72+
is_ansi: bool,
7273
_inner: PhantomData<S>,
7374
}
7475

@@ -117,6 +118,7 @@ where
117118
fmt_event: e,
118119
fmt_span: self.fmt_span,
119120
make_writer: self.make_writer,
121+
is_ansi: self.is_ansi,
120122
_inner: self._inner,
121123
}
122124
}
@@ -151,6 +153,7 @@ impl<S, N, E, W> Layer<S, N, E, W> {
151153
fmt_fields: self.fmt_fields,
152154
fmt_event: self.fmt_event,
153155
fmt_span: self.fmt_span,
156+
is_ansi: self.is_ansi,
154157
make_writer,
155158
_inner: self._inner,
156159
}
@@ -183,10 +186,21 @@ impl<S, N, E, W> Layer<S, N, E, W> {
183186
fmt_fields: self.fmt_fields,
184187
fmt_event: self.fmt_event,
185188
fmt_span: self.fmt_span,
189+
is_ansi: self.is_ansi,
186190
make_writer: TestWriter::default(),
187191
_inner: self._inner,
188192
}
189193
}
194+
195+
/// Enable ANSI terminal colors for formatted output.
196+
#[cfg(feature = "ansi")]
197+
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
198+
pub fn with_ansi(self, ansi: bool) -> Self {
199+
Self {
200+
is_ansi: ansi,
201+
..self
202+
}
203+
}
190204
}
191205

192206
impl<S, N, L, T, W> Layer<S, N, format::Format<L, T>, W>
@@ -213,6 +227,7 @@ where
213227
fmt_fields: self.fmt_fields,
214228
fmt_span: self.fmt_span,
215229
make_writer: self.make_writer,
230+
is_ansi: self.is_ansi,
216231
_inner: self._inner,
217232
}
218233
}
@@ -224,6 +239,7 @@ where
224239
fmt_fields: self.fmt_fields,
225240
fmt_span: self.fmt_span.without_time(),
226241
make_writer: self.make_writer,
242+
is_ansi: self.is_ansi,
227243
_inner: self._inner,
228244
}
229245
}
@@ -276,16 +292,6 @@ where
276292
}
277293
}
278294

279-
/// Enable ANSI encoding for formatted events.
280-
#[cfg(feature = "ansi")]
281-
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
282-
pub fn with_ansi(self, ansi: bool) -> Layer<S, N, format::Format<L, T>, W> {
283-
Layer {
284-
fmt_event: self.fmt_event.with_ansi(ansi),
285-
..self
286-
}
287-
}
288-
289295
/// Sets whether or not an event's target is displayed.
290296
pub fn with_target(self, display_target: bool) -> Layer<S, N, format::Format<L, T>, W> {
291297
Layer {
@@ -337,6 +343,7 @@ where
337343
fmt_fields: self.fmt_fields,
338344
fmt_span: self.fmt_span,
339345
make_writer: self.make_writer,
346+
is_ansi: self.is_ansi,
340347
_inner: self._inner,
341348
}
342349
}
@@ -350,6 +357,7 @@ where
350357
fmt_fields: format::Pretty::default(),
351358
fmt_span: self.fmt_span,
352359
make_writer: self.make_writer,
360+
is_ansi: self.is_ansi,
353361
_inner: self._inner,
354362
}
355363
}
@@ -378,6 +386,8 @@ where
378386
fmt_fields: format::JsonFields::new(),
379387
fmt_span: self.fmt_span,
380388
make_writer: self.make_writer,
389+
// always disable ANSI escapes in JSON mode!
390+
is_ansi: false,
381391
_inner: self._inner,
382392
}
383393
}
@@ -443,6 +453,7 @@ impl<S, N, E, W> Layer<S, N, E, W> {
443453
fmt_fields,
444454
fmt_span: self.fmt_span,
445455
make_writer: self.make_writer,
456+
is_ansi: self.is_ansi,
446457
_inner: self._inner,
447458
}
448459
}
@@ -455,6 +466,7 @@ impl<S> Default for Layer<S> {
455466
fmt_event: format::Format::default(),
456467
fmt_span: format::FmtSpanConfig::default(),
457468
make_writer: io::stdout,
469+
is_ansi: cfg!(feature = "ansi"),
458470
_inner: PhantomData,
459471
}
460472
}
@@ -488,6 +500,7 @@ where
488500
#[derive(Default)]
489501
pub struct FormattedFields<E: ?Sized> {
490502
_format_fields: PhantomData<fn(E)>,
503+
was_ansi: bool,
491504
/// The formatted fields of a span.
492505
pub fields: String,
493506
}
@@ -497,6 +510,7 @@ impl<E: ?Sized> FormattedFields<E> {
497510
pub fn new(fields: String) -> Self {
498511
Self {
499512
fields,
513+
was_ansi: false,
500514
_format_fields: PhantomData,
501515
}
502516
}
@@ -506,7 +520,7 @@ impl<E: ?Sized> FormattedFields<E> {
506520
/// The returned [`format::Writer`] can be used with the
507521
/// [`FormatFields::format_fields`] method.
508522
pub fn as_writer(&mut self) -> format::Writer<'_> {
509-
format::Writer::new(&mut self.fields)
523+
format::Writer::new(&mut self.fields).with_ansi(self.was_ansi)
510524
}
511525
}
512526

@@ -515,6 +529,7 @@ impl<E: ?Sized> fmt::Debug for FormattedFields<E> {
515529
f.debug_struct("FormattedFields")
516530
.field("fields", &self.fields)
517531
.field("formatter", &format_args!("{}", std::any::type_name::<E>()))
532+
.field("was_ansi", &self.was_ansi)
518533
.finish()
519534
}
520535
}
@@ -565,9 +580,10 @@ where
565580
let mut fields = FormattedFields::<N>::new(String::new());
566581
if self
567582
.fmt_fields
568-
.format_fields(fields.as_writer(), attrs)
583+
.format_fields(fields.as_writer().with_ansi(self.is_ansi), attrs)
569584
.is_ok()
570585
{
586+
fields.was_ansi = self.is_ansi;
571587
extensions.insert(fields);
572588
}
573589
}
@@ -599,9 +615,10 @@ where
599615
let mut fields = FormattedFields::<N>::new(String::new());
600616
if self
601617
.fmt_fields
602-
.format_fields(fields.as_writer(), values)
618+
.format_fields(fields.as_writer().with_ansi(self.is_ansi), values)
603619
.is_ok()
604620
{
621+
fields.was_ansi = self.is_ansi;
605622
extensions.insert(fields);
606623
}
607624
}
@@ -706,7 +723,11 @@ where
706723
let ctx = self.make_ctx(ctx);
707724
if self
708725
.fmt_event
709-
.format_event(&ctx, format::Writer::new(&mut buf), event)
726+
.format_event(
727+
&ctx,
728+
format::Writer::new(&mut buf).with_ansi(self.is_ansi),
729+
event,
730+
)
710731
.is_ok()
711732
{
712733
let mut writer = self.make_writer.make_writer_for(event.metadata());

0 commit comments

Comments
 (0)