From 48303511e028a586b8cd1a0a0f3bf622527354b3 Mon Sep 17 00:00:00 2001 From: "Andres O. Vela" Date: Sat, 1 Jun 2024 14:47:41 +0200 Subject: [PATCH 1/2] decoder: fix println!() records being printed with formatting --- decoder/src/log/format/mod.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/decoder/src/log/format/mod.rs b/decoder/src/log/format/mod.rs index 0f62dd37..956bea30 100644 --- a/decoder/src/log/format/mod.rs +++ b/decoder/src/log/format/mod.rs @@ -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, pub(super) color: Option, @@ -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" ); } @@ -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 From e2c258b4a446f76729c24a63f512e91cab175523 Mon Sep 17 00:00:00 2001 From: "Andres O. Vela" Date: Sat, 1 Jun 2024 15:07:22 +0200 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f2666b3..690f6f3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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://github.com/knurling-rs/defmt/pull/845 + ## [v0.3.8] - 2024-05-17 - [#840]: `defmt`: Support pre-1.77