diff --git a/tracing-subscriber/src/fmt/fmt_layer.rs b/tracing-subscriber/src/fmt/fmt_layer.rs index 9052e30a0..90269a773 100644 --- a/tracing-subscriber/src/fmt/fmt_layer.rs +++ b/tracing-subscriber/src/fmt/fmt_layer.rs @@ -533,37 +533,46 @@ where /// /// [extensions]: ../registry/struct.Extensions.html #[derive(Default)] -pub struct FormattedFields<E> { - _format_event: PhantomData<fn(E)>, +pub struct FormattedFields<E: ?Sized> { + _format_fields: PhantomData<fn(E)>, /// The formatted fields of a span. pub fields: String, } -impl<E> FormattedFields<E> { +impl<E: ?Sized> FormattedFields<E> { /// Returns a new `FormattedFields`. pub fn new(fields: String) -> Self { Self { fields, - _format_event: PhantomData, + _format_fields: PhantomData, } } + + /// Returns a new [`format::Writer`] for writing to this `FormattedFields`. + /// + /// 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) + } } -impl<E> fmt::Debug for FormattedFields<E> { +impl<E: ?Sized> fmt::Debug for FormattedFields<E> { 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>())) .finish() } } -impl<E> fmt::Display for FormattedFields<E> { +impl<E: ?Sized> fmt::Display for FormattedFields<E> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.fields) + fmt::Display::fmt(&self.fields, f) } } -impl<E> Deref for FormattedFields<E> { +impl<E: ?Sized> Deref for FormattedFields<E> { type Target = String; fn deref(&self) -> &Self::Target { &self.fields @@ -600,13 +609,13 @@ where let mut extensions = span.extensions_mut(); if extensions.get_mut::<FormattedFields<N>>().is_none() { - 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); + let mut fields = FormattedFields::<N>::new(String::new()); + if self + .fmt_fields + .format_fields(fields.as_writer(), attrs) + .is_ok() + { + extensions.insert(fields); } } @@ -629,19 +638,18 @@ where 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<N>>() - { + if let Some(fields) = extensions.get_mut::<FormattedFields<N>>() { let _ = self.fmt_fields.add_fields(fields, values); - } else { - let mut buf = String::new(); - if self.fmt_fields.format_fields(&mut buf, values).is_ok() { - let fmt_fields = FormattedFields { - fields: buf, - _format_event: PhantomData::<fn(N)>, - }; - extensions.insert(fmt_fields); - } + return; + } + + let mut fields = FormattedFields::<N>::new(String::new()); + if self + .fmt_fields + .format_fields(fields.as_writer(), values) + .is_ok() + { + extensions.insert(fields); } } @@ -743,7 +751,11 @@ where }; let ctx = self.make_ctx(ctx); - if self.fmt_event.format_event(&ctx, &mut buf, event).is_ok() { + if self + .fmt_event + .format_event(&ctx, format::Writer::new(&mut buf), event) + .is_ok() + { let mut writer = self.make_writer.make_writer_for(event.metadata()); let _ = io::Write::write_all(&mut writer, buf.as_bytes()); } @@ -786,7 +798,7 @@ where { fn format_fields<R: RecordFields>( &self, - writer: &'writer mut dyn fmt::Write, + writer: format::Writer<'writer>, fields: R, ) -> fmt::Result { self.fmt_fields.format_fields(writer, fields) diff --git a/tracing-subscriber/src/fmt/format/mod.rs b/tracing-subscriber/src/fmt/format/mod.rs index 7a7700a6e..952a39c23 100644 --- a/tracing-subscriber/src/fmt/format/mod.rs +++ b/tracing-subscriber/src/fmt/format/mod.rs @@ -1158,7 +1158,6 @@ where } // === impl FormatFields === - impl<'writer, M> FormatFields<'writer> for M where M: MakeOutput<Writer<'writer>, fmt::Result>,