Skip to content

Commit

Permalink
subscriber: fix incorrect use of FormattedFields extension in fmt l…
Browse files Browse the repository at this point in the history
…ayer

This fixes two bugs. One where the formatted fields were incorrectly
parameterized over Self instead of the formatter that generated the
formatted fields. The second bug is to check if another layer has
already inserted formatted fields and to avoid inserting if so, which
would cause a panic.
  • Loading branch information
yaahc authored Jan 28, 2020
1 parent fa1f826 commit 1c3bb70
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions tracing-subscriber/src/fmt/fmt_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,22 +395,27 @@ where
fn new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
let span = ctx.span(id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();

let mut buf = String::new();
if self.fmt_fields.format_fields(&mut buf, attrs).is_ok() {
let fmt_fields = FormattedFields {
fields: buf,
_format_event: PhantomData::<fn(N)>,
};
extensions.insert(fmt_fields);
if let Some(FormattedFields { ref mut fields, .. }) =
extensions.get_mut::<FormattedFields<N>>()
{
let _ = self.fmt_fields.format_fields(fields, attrs);
} else {
let mut buf = String::new();
if self.fmt_fields.format_fields(&mut buf, attrs).is_ok() {
let fmt_fields = FormattedFields {
fields: buf,
_format_event: PhantomData::<fn(N)>,
};
extensions.insert(fmt_fields);
}
}
}

fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) {
let span = ctx.span(id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();
if let Some(FormattedFields { ref mut fields, .. }) =
extensions.get_mut::<FormattedFields<Self>>()
extensions.get_mut::<FormattedFields<N>>()
{
let _ = self.fmt_fields.format_fields(fields, values);
} else {
Expand Down

0 comments on commit 1c3bb70

Please sign in to comment.