Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoder: fix println!() records being printed with formatting #845

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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://github.com/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
Loading