knurling-rs / defmt

Efficient, deferred formatting for logging on embedded systems
https://defmt.ferrous-systems.com/
Apache License 2.0
812 stars 74 forks source link

Implement "passthrough" Debug and Display instances for wrappers #858

Closed wyager closed 2 months ago

wyager commented 2 months ago

Useful for if you're writing code that switches between defmt::whatever and log::whatever or core::whatever using macros

jonathanpallant commented 2 months ago

Do you have a short example of where this is useful?

wyager commented 2 months ago

@jonathanpallant Sure - when writing code that will be used on embedded or desktop, it's pretty common to use a macro adapter like https://github.com/embassy-rs/embassy/blob/ac06ca2fa0aa061a0053336433fa9cbfad5212ca/embassy-sync/src/fmt.rs which switches whatever! between log::whatever! and defmt::whatever!

If the object you are logging is non-defmt (e.g. NorFlashErrorKind), you must wrap it in a Debug2Format to use it with defmt. However, if you do that, now the whatever! macro will fail when you compile for desktop.

This PR fixes that issue. Now, something wrapped in Debug2Format will compile whether using defmt or log as your macro implementation.

jonathanpallant commented 2 months ago

Oh! Right, that makes perfect sense.

jonathanpallant commented 2 months ago

@Urhengulas any thoughts on this one?