From d479e88dd1510e0b0da4fc98eb9e0152710c6985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Sch=C3=B6nburg?= Date: Tue, 2 Mar 2021 22:33:13 +0000 Subject: [PATCH 1/2] subscriber: fix extra padding in pretty format This fixes #1212 where extra padding was written when logging with metadata. With this change we only write padding when we actually decide to write a value, not when skipping a metadata value. --- tracing-subscriber/src/fmt/format/pretty.rs | 27 ++++++++++----------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/tracing-subscriber/src/fmt/format/pretty.rs b/tracing-subscriber/src/fmt/format/pretty.rs index 3aa94b9b8b..5fb557eeff 100644 --- a/tracing-subscriber/src/fmt/format/pretty.rs +++ b/tracing-subscriber/src/fmt/format/pretty.rs @@ -241,12 +241,14 @@ impl<'a> PrettyVisitor<'a> { Self { style, ..self } } - fn maybe_pad(&mut self) { - if self.is_empty { + fn write_padded(&mut self, value: &dyn fmt::Debug) { + let padding = if self.is_empty { self.is_empty = false; + "" } else { - self.result = write!(self.writer, ", "); - } + ", " + }; + self.result = write!(self.writer, "{}{:?}", padding, value); } } @@ -287,28 +289,25 @@ impl<'a> field::Visit for PrettyVisitor<'a> { return; } let bold = self.style.bold(); - self.maybe_pad(); - self.result = match field.name() { - "message" => write!(self.writer, "{}{:?}", self.style.prefix(), value,), + match field.name() { + "message" => self.write_padded(&format_args!("{}{:?}", self.style.prefix(), value,)), // Skip fields that are actually log metadata that have already been handled #[cfg(feature = "tracing-log")] - name if name.starts_with("log.") => Ok(()), - name if name.starts_with("r#") => write!( - self.writer, + name if name.starts_with("log.") => self.result = Ok(()), + name if name.starts_with("r#") => self.write_padded(&format_args!( "{}{}{}: {:?}", bold.prefix(), &name[2..], bold.infix(self.style), value - ), - name => write!( - self.writer, + )), + name => self.write_padded(&format_args!( "{}{}{}: {:?}", bold.prefix(), name, bold.infix(self.style), value - ), + )), }; } } From e4f8cdbdc25df433fa788127ec8d3ee1af71889c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Sch=C3=B6nburg?= Date: Wed, 3 Mar 2021 09:54:41 +0100 Subject: [PATCH 2/2] subscriber: improve write_padded performance with impl Trait Co-authored-by: Eliza Weisman --- tracing-subscriber/src/fmt/format/pretty.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracing-subscriber/src/fmt/format/pretty.rs b/tracing-subscriber/src/fmt/format/pretty.rs index 5fb557eeff..af6919107c 100644 --- a/tracing-subscriber/src/fmt/format/pretty.rs +++ b/tracing-subscriber/src/fmt/format/pretty.rs @@ -241,7 +241,7 @@ impl<'a> PrettyVisitor<'a> { Self { style, ..self } } - fn write_padded(&mut self, value: &dyn fmt::Debug) { + fn write_padded(&mut self, value: &impl fmt::Debug) { let padding = if self.is_empty { self.is_empty = false; ""