-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
base: main
Are you sure you want to change the base?
Conversation
97a918d
to
9c0d908
Compare
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. |
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. |
decoder/src/lib.rs
Outdated
@@ -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>(), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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)]
...)
There was a problem hiding this comment.
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()
)
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. |
I just noticed that |
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
, butthis iterator will yield interpolated fragments on their own. For example, the log:
Will yield the following strings:
Note that nested fragments will not yield separately:
Will yield:
This iterator yields the same fragments as
Self::fragments
, so you can zip themtogether 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.