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

wuyuanyi
Posts: 24
Joined: Wed Mar 09, 2022 11:54 am

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

Postby wuyuanyi » Sun Sep 25, 2022 7:13 am

(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 1267 times
Is this logging library working as expected? How could I prevent the verbose and debug logging from being compiled?

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_Sprite » 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.

wuyuanyi
Posts: 24
Joined: Wed Mar 09, 2022 11:54 am

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

Postby wuyuanyi » Sun Sep 25, 2022 8:03 am

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!

Who is online

Users browsing this forum: Baidu [Spider] and 105 guests