Skip to content

Commit

Permalink
subscriber: fix extra padding in pretty format
Browse files Browse the repository at this point in the history
This fixes tokio-rs#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.
  • Loading branch information
lenaschoenburg committed Mar 2, 2021
1 parent 62b828a commit d479e88
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 @@ -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);
}
}

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

0 comments on commit d479e88

Please sign in to comment.