Skip to content

decoder: add Frame::fragments() and Frame::display_fragments() #966

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

darkwater
Copy link

One cool possibility with defmt is a log viewer that can do more than just display static text, eg. change number representations on the fly. These two methods should make doing something like that a bit easier.

Copying from the docs I put on display_fragments():


Returns an iterator over the fragments of the message contained in this log frame.

Collecting this into a String will yield the same result as Self::display_message, but
this iterator will yield interpolated fragments on their own. For example, the log:

defmt::info!("foo = {}, bar = {}", 1, 2);

Will yield the following strings:

vec!["foo = ", "1", ", bar = ", "2"]

Note that nested fragments will not yield separately:

defmt::info!("foo = {}", Foo { bar: 1 });

Will yield:

vec!["foo = ", "Foo { bar: 1 }"]

This iterator yields the same fragments as Self::fragments, so you can zip them
together to get both representations.


This is quite limited in that it doesn't go into nested fragments, but could already enable some limited extra functionality with minimal effort.

Haven't tested on an actual project yet, intend to do that tomorrow.

@darkwater darkwater force-pushed the main branch 3 times, most recently from 97a918d to 9c0d908 Compare May 14, 2025 18:33
@darkwater
Copy link
Author

Oh, forgot to mention, yes I tested this by making a prototype structured logging forwarder, where you can:

defmt::info!("something happened action={}", "foo bar");

and it'll parse "action" as a key, and "foo bar" as a value, sort of escaping the value.

@jonathanpallant
Copy link
Contributor

Thank you for your proposed changes. I had a quick look and they seem OK to me, but I've asked my colleagues for a second opinion.

@@ -715,6 +756,10 @@ mod tests {
frame.display(false).to_string(),
"0.000002 INFO x=S { x: 2a }",
);
assert_eq!(
frame.display_fragments().collect::<String>(),
Copy link
Contributor

@jonathanpallant jonathanpallant May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than collect these into a single String, it might be better to .join('|') them, so we can verify the splits between the components are where we expect.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That, or, assert_eq! against a &[&'static str], like &["x=", "None"] or something.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was to just make sure the output is the same as display(), but yeah that would be better anyway. Done

@@ -531,6 +578,23 @@ impl fmt::Display for DisplayMessage<'_> {
}
}

pub struct DisplayFragments<'t> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new public type, so it needs documenting.

(Also I should #[deny(missing_docs)]...)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (just a summary and a link to display_fragments())

@jonathanpallant
Copy link
Contributor

A couple more observations on a second pass, but I checked the diff in detail with a better diff tool, and most of the 'changed' code is unchanged - it's just de-indented, slightly reformatted thanks to the extra width it now has, and a reference can be dropped because the new function is passed a reference. Functionally it appears to do the same thing as before.

@darkwater
Copy link
Author

I just noticed that DisplayFragments and it's already existing neighbours eg. DisplayMessage aren't actually public, since mod frame isn't public. These types are returned by public methods on Frame, so would it be helpful to expose these types?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants