Page 1 of 1

esp_log.h: How to exclude logging at compile time?

Posted: Sun Sep 25, 2022 7:13 am
by wuyuanyi
(I submitted this issue to github a while ago but I thought this might be better asked here. https://github.com/espressif/esp-idf/issues/9851)

The documentation mentions that the logging level can be configured at compile time. I was expecting that the ESP_LOGx macro will be expanded to an empty statement if the level is not active, hence completely excluding it from the binary. But I am observing the macro to be expanded anyway and the filtering is done in runtime, adding to some overhead to process the code that should have been excluded from the build.

Below is the screenshot from my CLion IDE, My maximum log level was set to INFO but the verbose or debug level will still be expanded and compiled in my firmware.
192132043-80b16266-4f15-41fc-b938-1ad68102a97d.png
192132043-80b16266-4f15-41fc-b938-1ad68102a97d.png (57.13 KiB) Viewed 1472 times
Is this logging library working as expected? How could I prevent the verbose and debug logging from being compiled?

Re: esp_log.h: How to exclude logging at compile time?

Posted: Sun Sep 25, 2022 7:46 am
by ESP_Sprite
That is not done in the preprocessor, but by the compiler. The 'if (3 >= ESP_LOG_VERBOSE)' will always be expanded to a constant true or false by the compiler, and as such the code will compile to either the inner statement or no code at all, dependent on what the '3' value (which I assume comes from the max log level) is.

Re: esp_log.h: How to exclude logging at compile time?

Posted: Sun Sep 25, 2022 8:03 am
by wuyuanyi
ESP_Sprite wrote:
Sun Sep 25, 2022 7:46 am
That is not done in the preprocessor, but by the compiler. The 'if (3 >= ESP_LOG_VERBOSE)' will always be expanded to a constant true or false by the compiler, and as such the code will compile to either the inner statement or no code at all, dependent on what the '3' value (which I assume comes from the max log level) is.
I see. Thanks for the explanation!