From 1c3bb7087db7570e91d27cd40e5e27f0f9a2c8b1 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Tue, 28 Jan 2020 14:37:20 -0800 Subject: [PATCH] subscriber: fix incorrect use of `FormattedFields` extension in fmt layer 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. --- tracing-subscriber/src/fmt/fmt_layer.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/tracing-subscriber/src/fmt/fmt_layer.rs b/tracing-subscriber/src/fmt/fmt_layer.rs index 0c9f9f1615..f3e0b13fa9 100644 --- a/tracing-subscriber/src/fmt/fmt_layer.rs +++ b/tracing-subscriber/src/fmt/fmt_layer.rs @@ -395,14 +395,19 @@ 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::, - }; - extensions.insert(fmt_fields); + if let Some(FormattedFields { ref mut fields, .. }) = + extensions.get_mut::>() + { + 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::, + }; + extensions.insert(fmt_fields); + } } } @@ -410,7 +415,7 @@ where 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::>() + extensions.get_mut::>() { let _ = self.fmt_fields.format_fields(fields, values); } else {