Skip to content

Commit

Permalink
subscriber: fix extra padding in pretty format (#1275)
Browse files Browse the repository at this point in the history
## Motivation

This fixes #1212, where extra padding was written when logging with the
pretty formatter.

## Solution 

With this change we only write padding when we actually decide to write
a value but not when skipping a metadata value such as `log.file` or
`log.line`
  • Loading branch information
lenaschoenburg authored and hawkw committed Mar 12, 2021
1 parent a933bb7 commit 73b472e
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions tracing-subscriber/src/fmt/format/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,14 @@ impl<'a> PrettyVisitor<'a> {
Self { style, ..self }
}

fn maybe_pad(&mut self) {
if self.is_empty {
fn write_padded(&mut self, value: &impl 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);
}
}

Expand Down Expand Up @@ -288,28 +290,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
),
)),
};
}
}
Expand Down

0 comments on commit 73b472e

Please sign in to comment.