Skip to content

Commit

Permalink
Merge pull request #845 from andresovela/fix-println
Browse files Browse the repository at this point in the history
decoder: fix println!() records being printed with formatting
  • Loading branch information
Urhengulas authored Jun 6, 2024
2 parents 217a5a3 + e2c258b commit 8a7282d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- [#845]: `decoder`: fix println!() records being printed with formatting

[#845]: https:/knurling-rs/defmt/pull/845

## [v0.3.8] - 2024-05-17

- [#840]: `defmt`: Support pre-1.77
Expand Down
18 changes: 13 additions & 5 deletions decoder/src/log/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub(super) struct LogSegment {
pub(super) format: LogFormat,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Default)]
pub(super) struct LogFormat {
pub(super) width: Option<usize>,
pub(super) color: Option<LogColor>,
Expand Down Expand Up @@ -344,13 +344,13 @@ impl InternalFormatter {
if format_has_timestamp && !config.is_timestamp_available {
log::warn!(
"logger format contains timestamp but no timestamp implementation \
was provided; consider removing the timestamp (`{{t}}` or `{{T}}`) from the \
was provided; consider removing the timestamp (`{{t}}`) from the \
logger format or provide a `defmt::timestamp!` implementation"
);
} else if !format_has_timestamp && config.is_timestamp_available {
log::warn!(
"`defmt::timestamp!` implementation was found, but timestamp is not \
part of the log format; consider adding the timestamp (`{{t}}` or `{{T}}`) \
part of the log format; consider adding the timestamp (`{{t}}`) \
argument to the log format"
);
}
Expand All @@ -361,8 +361,16 @@ impl InternalFormatter {

fn format(&self, record: &Record) -> String {
let mut buf = String::new();
for segment in &self.format {
let s = self.build_segment(record, segment);
// Only format logs, not printlns
// printlns do not have a log level
if get_log_level_of_record(record).is_some() {
for segment in &self.format {
let s = self.build_segment(record, segment);
write!(buf, "{s}").expect("writing to String cannot fail");
}
} else {
let empty_format: LogFormat = Default::default();
let s = self.build_log(record, &empty_format);
write!(buf, "{s}").expect("writing to String cannot fail");
}
buf
Expand Down

0 comments on commit 8a7282d

Please sign in to comment.