defmt icon indicating copy to clipboard operation
defmt copied to clipboard

Adding a std logging backend

Open diondokter opened this issue 3 years ago • 25 comments

So I've been using defmt in a couple of libraries of mine and it's super nice!

However, when I want to add tests to the library that I run on my PC, defmt needs a global logger and there are no loggers that work for PC tests afaik. What I want to propose is similar to #714, but maybe not quite the same.

I would like to see a feature flag that switches defmt over from using its own logger and deferred formatting to using println, panic, the log crate and Rust's built-in formatter.

I took a stab at it today, but only for the defmt::println macro to try this out: https://github.com/knurling-rs/defmt/compare/main...diondokter:defmt:std-log

In essence I think it should be possible to translate the defmt formatting to the rust formatting which can then be routed to panic, println and log::**** as required.

This is pretty doable if the API would be that every type needs to implement Debug when in this mode, but theoretically it should be possible to use the Format trait as input too.

So I want to know, is there interest in this? Has something in this direction a chance of being accepted? If so I think I could spare some time to work on this.

diondokter avatar Jan 27 '23 14:01 diondokter

Hi @diondokter, I would like this. Many people report that they struggle with this issue, especially in CI. Since it would be behind a feature flag it is not even a breaking change as far as I understand.

Also, we mostly tried to stay close to the std::fmt grammar for the format strings, so it should be possible to translate with not too much effort. Please tell me about the cases where the defmt grammar diverges from the std::fmt grammar.

Urhengulas avatar Jan 27 '23 16:01 Urhengulas

Based on the defmt book I can see:

  • Primitive specifications. But these can mostly be ignored, except for the bitfields and interned strings (https://defmt.ferrous-systems.com/primitives.html)
  • The :a ascii hint
  • The :us hint

diondokter avatar Jan 28 '23 15:01 diondokter