espressif / esp-idf-cxx

C++ wrapper classes for ESP-IDF components.
Apache License 2.0
77 stars 13 forks source link

use fmt library for C++ logging (IDFGH-8779) #17

Open ljden opened 1 year ago

ljden commented 1 year ago

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++

igrr commented 1 year 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.

ljden commented 1 year ago

Might also be worth considering using spdlog for logging

SoucheSouche commented 1 year ago

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

igrr commented 1 year ago

@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);.

ljden commented 1 year ago

@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

SoucheSouche commented 1 year ago

@igrr, @ljden, sorry I misunderstood the ticket. This is definitely something that could be supported in esp-idf-cxx.

ljden commented 1 year ago

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

erhankur commented 1 year ago

Might also be worth considering using spdlog for logging

Just a basic example of spdlog on esp-idf. https://github.com/erhankur/esp32-spdlog

ljden commented 1 year ago

Oh thanks @erhankur! I wasn't able to get spdlog compiling - I must have been doing something wrong...

X-Ryl669 commented 2 months ago

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.