esp-idf-cxx icon indicating copy to clipboard operation
esp-idf-cxx copied to clipboard

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

Open ljden opened this issue 2 years ago • 10 comments

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

ljden avatar Nov 21 '22 03:11 ljden

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.

igrr avatar Nov 21 '22 15:11 igrr

Might also be worth considering using spdlog for logging

ljden avatar Nov 22 '22 06:11 ljden

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 avatar Nov 23 '22 08:11 SoucheSouche

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

igrr avatar Nov 23 '22 09:11 igrr

@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

ljden avatar Nov 23 '22 09:11 ljden

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

SoucheSouche avatar Nov 23 '22 09:11 SoucheSouche

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

ljden avatar Jan 03 '23 00:01 ljden

Might also be worth considering using spdlog for logging

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

erhankur avatar Jan 06 '23 18:01 erhankur

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

ljden avatar Jan 08 '23 22:01 ljden

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.

X-Ryl669 avatar Jun 21 '24 16:06 X-Ryl669