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

Add optional defmt::Format support for generated types. #91

Merged
merged 1 commit into from
Dec 2, 2024

Conversation

projectgus
Copy link
Contributor

@projectgus projectgus commented Nov 9, 2024

Add support for the low resource defmt logging system.

  • Derived for enum types
  • Formats messages by printing individual field values
  • Enabled in can-messages test generation.

CAN messages are formatted by printing the fields, for example:

Vcu200(Vcu200 { RangeAdditionalClimateOff=0 CurrentGear=P RangeLSB=false Range=194 AccelPedal_Pos2=0 UnkStatus=0 Counter=0 AccelPedalPos3=0 }).

This is different to the current Debug support which just logs the enum name and the raw bytes, for example:

Vcu200(Vcu200([0, 0, 97, 0, 0, 0, 208, 0])).

My feeling is that it's always straightforward to log msg.id(), msg.raw() from the code, so having the structured formatting is useful.

defmt overhead is much less than Debug. In my embedded project which has 107 DBC Messages defined, using defmt to log an arbitrary Messages instance adds about 22KB of binary size. Using Debug (via Debug2Format) instead adds 85KB of binary size(!), despite the Debug output being less structured!! (Although to be fair most of this overhead is the one-off cost of including Debug formatting support.)

- Derived for enum types
- Formats messages by printing individual field values
- Enabled in can-messages test generation.
@tdittr
Copy link

tdittr commented Nov 28, 2024

This looks really nice :) The only thing I spotted that was missing was a #[derive(defmt::Format)] on the CanError.

@projectgus
Copy link
Contributor Author

This looks really nice :) The only thing I spotted that was missing was a #[derive(defmt::Format)] on the CanError.

Thanks! CanError is a bit fiddly, as some variants include embedded_can::Id which doesn't implement Format. So we can't derive it. It would be possible to manually implement it, I guess...?

@tdittr
Copy link

tdittr commented Dec 2, 2024

Thanks! CanError is a bit fiddly, as some variants include embedded_can::Id which doesn't implement Format.

Ah that makes sense. It looks like embedded-can does already have support for defmt in main but not released it yet. I can ask about that at the next rust-embedded meeting tomorrow. I think implementing it by hand is not worth the effort.

@linusharberg linusharberg merged commit 5b9d3d0 into technocreatives:main Dec 2, 2024
2 checks passed
@projectgus projectgus deleted the feature/defmt branch December 2, 2024 21:44
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.

3 participants