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

fmt: fix layers with different ANSI settings sharing the same TypeId #3221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
25 changes: 20 additions & 5 deletions tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ use fmt::{Debug, Display};
/// // formatter in the `FmtContext`.
/// let ext = span.extensions();
/// let fields = &ext
/// .get::<FormattedFields<N>>()
/// .get::<FormattedFields<N, true>>()
/// .expect("will never be `None`");
///
/// // Skip formatting the fields if the span had no fields.
Expand Down 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
Loading