Skip to content

Commit

Permalink
fmt: fix layers with different ANSI settings sharing the same TypeId
Browse files Browse the repository at this point in the history
Previously, the FormattedFields struct stored the ANSI state as a
boolean field, which could lead to inconsistencies when different layers
used different ANSI settings (true/false). This happened because we
store the FormattedFields in a HashMap using the TypeId as a key which
obviously means that ansi=true and ansi=false would have the same
key and the same formatted fields. This meant that whichever layer
stored its fields first would determine the ANSI formatting for all
subsequent layers.

This change refactors FormattedFields to use a const generic parameter
for the ANSI state, allowing us to store two different FormattedFields
(one with and one without ansi). Each layer can now store its fields
with the correct ANSI setting without interfering with other layers.

Fixes: tokio-rs#1310
Fixes: tokio-rs#1817
Fixes: tokio-rs#3065
Fixes: tokio-rs#3116

Signed-off-by: Gabriel Goller <gabrielgoller123@gmail.com>
  • Loading branch information
kaffarell committed Feb 22, 2025
1 parent b486867 commit 8a96ff1
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 39 deletions.
81 changes: 56 additions & 25 deletions tracing-subscriber/src/fmt/fmt_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,22 +753,22 @@ where
/// the [`Extensions`][extensions] type-map. This means that when multiple
/// formatters are in use, each can store its own formatted representation
/// without conflicting.
/// The `ANSI` const parameter determines whether ANSI escape codes should be used
/// for formatting the fields (i.e. colors, font styles).
///
/// [extensions]: crate::registry::Extensions
#[derive(Default)]
pub struct FormattedFields<E: ?Sized> {
pub struct FormattedFields<E: ?Sized, const ANSI: bool = false> {
_format_fields: PhantomData<fn(E)>,
was_ansi: bool,
/// The formatted fields of a span.
pub fields: String,
}

impl<E: ?Sized> FormattedFields<E> {
impl<E: ?Sized, const ANSI: bool> FormattedFields<E, ANSI> {
/// Returns a new `FormattedFields`.
pub fn new(fields: String) -> Self {
Self {
fields,
was_ansi: false,
_format_fields: PhantomData,
}
}
Expand All @@ -778,27 +778,27 @@ 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).with_ansi(self.was_ansi)
format::Writer::new(&mut self.fields).with_ansi(ANSI)
}
}

impl<E: ?Sized> fmt::Debug for FormattedFields<E> {
impl<E: ?Sized, const ANSI: bool> fmt::Debug for FormattedFields<E, ANSI> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FormattedFields")
.field("fields", &self.fields)
.field("formatter", &format_args!("{}", std::any::type_name::<E>()))
.field("was_ansi", &self.was_ansi)
.field("is_ansi", &format_args!("{}", ANSI))
.finish()
}
}

impl<E: ?Sized> fmt::Display for FormattedFields<E> {
impl<E: ?Sized, const ANSI: bool> fmt::Display for FormattedFields<E, ANSI> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.fields, f)
}
}

impl<E: ?Sized> Deref for FormattedFields<E> {
impl<E: ?Sized, const ANSI: bool> Deref for FormattedFields<E, ANSI> {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.fields
Expand Down Expand Up @@ -834,14 +834,29 @@ where
let span = ctx.span(id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();

if extensions.get_mut::<FormattedFields<N>>().is_none() {
let mut fields = FormattedFields::<N>::new(String::new());
if self.is_ansi {
if extensions.get_mut::<FormattedFields<N, true>>().is_none() {
let mut fields = FormattedFields::<N, true>::new(String::new());
if self
.fmt_fields
.format_fields(fields.as_writer().with_ansi(true), attrs)
.is_ok()
{
extensions.insert(fields);
} else {
eprintln!(
"[tracing-subscriber] Unable to format the following event, ignoring: {:?}",
attrs
);
}
}
} else if extensions.get_mut::<FormattedFields<N, false>>().is_none() {
let mut fields = FormattedFields::<N, false>::new(String::new());
if self
.fmt_fields
.format_fields(fields.as_writer().with_ansi(self.is_ansi), attrs)
.format_fields(fields.as_writer().with_ansi(false), attrs)
.is_ok()
{
fields.was_ansi = self.is_ansi;
extensions.insert(fields);
} else {
eprintln!(
Expand Down Expand Up @@ -870,19 +885,35 @@ where
fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, C>) {
let span = ctx.span(id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();
if let Some(fields) = extensions.get_mut::<FormattedFields<N>>() {
let _ = self.fmt_fields.add_fields(fields, values);
return;
}

let mut fields = FormattedFields::<N>::new(String::new());
if self
.fmt_fields
.format_fields(fields.as_writer().with_ansi(self.is_ansi), values)
.is_ok()
{
fields.was_ansi = self.is_ansi;
extensions.insert(fields);
if self.is_ansi {
if let Some(fields) = extensions.get_mut::<FormattedFields<N, true>>() {
let _ = self.fmt_fields.add_fields::<true>(fields, values);
return;
}

let mut fields = FormattedFields::<N, true>::new(String::new());
if self
.fmt_fields
.format_fields(fields.as_writer().with_ansi(true), values)
.is_ok()
{
extensions.insert(fields);
}
} else {
if let Some(fields) = extensions.get_mut::<FormattedFields<N, false>>() {
let _ = self.fmt_fields.add_fields::<false>(fields, values);
return;
}

let mut fields = FormattedFields::<N, false>::new(String::new());
if self
.fmt_fields
.format_fields(fields.as_writer().with_ansi(false), values)
.is_ok()
{
extensions.insert(fields);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions tracing-subscriber/src/fmt/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ where

let ext = self.0.extensions();
let data = ext
.get::<FormattedFields<N>>()
.get::<FormattedFields<N, false>>()
.expect("Unable to find FormattedFields in extensions; this is a bug");

// TODO: let's _not_ do this, but this resolves
Expand Down Expand Up @@ -358,9 +358,9 @@ impl<'a> FormatFields<'a> for JsonFields {
/// By default, this appends a space to the current set of fields if it is
/// non-empty, and then calls `self.format_fields`. If different behavior is
/// required, the default implementation of this method can be overridden.
fn add_fields(
fn add_fields<const ANSI: bool>(
&self,
current: &'a mut FormattedFields<Self>,
current: &'a mut FormattedFields<Self, ANSI>,
fields: &Record<'_>,
) -> fmt::Result {
if current.is_empty() {
Expand Down
23 changes: 19 additions & 4 deletions tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,12 @@ pub trait FormatFields<'writer> {
/// By default, this appends a space to the current set of fields if it is
/// non-empty, and then calls `self.format_fields`. If different behavior is
/// required, the default implementation of this method can be overridden.
fn add_fields(
///
/// The `ANSI` const parameter determines whether ANSI escape codes should be used
/// for formatting the fields (i.e. colors and font styles).
fn add_fields<const ANSI: bool>(
&self,
current: &'writer mut FormattedFields<Self>,
current: &'writer mut FormattedFields<Self, ANSI>,
fields: &span::Record<'_>,
) -> fmt::Result {
if !current.fields.is_empty() {
Expand Down Expand Up @@ -980,7 +983,13 @@ where
seen = true;

let ext = span.extensions();
if let Some(fields) = &ext.get::<FormattedFields<N>>() {
if let Some(true) = self.ansi {
if let Some(fields) = &ext.get::<FormattedFields<N, true>>() {
if !fields.is_empty() {
write!(writer, "{}{}{}", bold.paint("{"), fields, bold.paint("}"))?;
}
}
}else if let Some(fields) = &ext.get::<FormattedFields<N, false>>() {
if !fields.is_empty() {
write!(writer, "{}{}{}", bold.paint("{"), fields, bold.paint("}"))?;
}
Expand Down Expand Up @@ -1108,7 +1117,13 @@ where

for span in ctx.event_scope().into_iter().flat_map(Scope::from_root) {
let exts = span.extensions();
if let Some(fields) = exts.get::<FormattedFields<N>>() {
if let Some(true) = self.ansi {
if let Some(fields) = exts.get::<FormattedFields<N, true>>() {
if !fields.is_empty() {
write!(writer, " {}", dimmed.paint(&fields.fields))?;
}
}
} else if let Some(fields) = exts.get::<FormattedFields<N, false>>() {
if !fields.is_empty() {
write!(writer, " {}", dimmed.paint(&fields.fields))?;
}
Expand Down
23 changes: 16 additions & 7 deletions tracing-subscriber/src/fmt/format/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,20 @@ where
}

let ext = span.extensions();
let fields = &ext
.get::<FormattedFields<N>>()
.expect("Unable to find FormattedFields in extensions; this is a bug");
if !fields.is_empty() {
write!(writer, " {} {}", dimmed.paint("with"), fields)?;
if let Some(true) = self.ansi {
let fields = &ext
.get::<FormattedFields<N, true>>()
.expect("Unable to find FormattedFields in extensions; this is a bug");
if !fields.is_empty() {
write!(writer, " {} {}", dimmed.paint("with"), fields)?;
}
} else {
let fields = &ext
.get::<FormattedFields<N, false>>()
.expect("Unable to find FormattedFields in extensions; this is a bug");
if !fields.is_empty() {
write!(writer, " {} {}", dimmed.paint("with"), fields)?;
}
}
writer.write_char('\n')?;
}
Expand All @@ -329,9 +338,9 @@ impl<'writer> FormatFields<'writer> for Pretty {
v.finish()
}

fn add_fields(
fn add_fields<const ANSI: bool>(
&self,
current: &'writer mut FormattedFields<Self>,
current: &'writer mut FormattedFields<Self, ANSI>,
fields: &span::Record<'_>,
) -> fmt::Result {
let empty = current.is_empty();
Expand Down

0 comments on commit 8a96ff1

Please # to comment.