Open ljden opened 2 years ago
This might be something we could support in https://github.com/espressif/esp-idf-cxx repository, which contains C++-specific libraries for ESP-IDF. I would suggest moving this issue there.
Might also be worth considering using spdlog for logging
Hi @ljden,
You can already use the fmt library by including it into your project as a component using the IDF component manager.
Here is the link to the GitHub repository of this component: https://github.com/espressif/idf-extra-components/tree/master/fmt
@SoucheSouche just to clarify the feature request: it is to provide a logging API which can take fmt-compatible format strings.
For example, to let C++ developers use something like esp::log::error(tag, "error: {}", err_code);
instead of ESP_LOGE(TAG, "%d", err_code);
.
@SoucheSouche this is not currently possible, and @igrr has provided a good example of what I'm after. Currently to use fmt with logging you need to construct a string, extract the c_str, and pass it to esp logger as "%s"
. Having native support for fmt will allow for much more performant code without the need to construct strings or have a preallocated buffer for logging
@igrr, @ljden, sorry I misunderstood the ticket. This is definitely something that could be supported in esp-idf-cxx.
Just to add an extra use case for this, this would also mean string_views can be used in logging without needing to copy them. The current logging cannot do this as printf requires null terminated strings and string_views cannot guarantee they are null terminated
Might also be worth considering using spdlog for logging
Just a basic example of spdlog
on esp-idf. https://github.com/erhankur/esp32-spdlog
Oh thanks @erhankur! I wasn't able to get spdlog compiling - I must have been doing something wrong...
The current logging cannot do this as printf requires null terminated strings and string_views cannot guarantee they are null terminated
I'm using string_view
just fine with printf like C function. Simply use "%.*s"
for the syntax, and str.length(), str.data()
in the argument list. I'm using it for string where I already know their length just to avoid a useless strlen computation in the runtime code.
Is your feature request related to a problem?
More powerful formatting for log messages
Describe the solution you'd like.
If using C++11/14/17: https://github.com/fmtlib/fmt For C++20 it is part of the standard https://en.cppreference.com/w/cpp/utility/format
Describe alternatives you've considered.
No response
Additional context.
This would only work for C++