-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added possibility to use ESP32-IDF log insted of redefined one #4845
Conversation
cores/esp32/esp32-hal-log.h
Outdated
#ifdef USE_ESP32_LOG | ||
|
||
#ifndef TAG | ||
#define TAG "ESP32" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using TAG like "arduino" makes more sense here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem , i will change it for sure :)
cores/esp32/esp32-hal-log.h
Outdated
@@ -37,6 +36,9 @@ extern "C" | |||
#define ARDUHAL_LOG_LEVEL CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL | |||
#else | |||
#define ARDUHAL_LOG_LEVEL CORE_DEBUG_LEVEL | |||
#ifdef USE_ESP32_LOG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that renaming the name to USE_ESP_IDF_LOG
would describe a bit better what the switch does :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, let me change it
cores/esp32/esp32-hal-log.c
Outdated
#define __MY_LOG__ | ||
#include "stdio.h" | ||
#include "esp32-hal-log.h" | ||
#ifdef USE_ESP32_LOG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can remove the #ifdef
switch. Function will not be included in the final binary if not called within the application and will at the same time allow USE_ESP32_LOG
to be defined on per file basis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! :)
cores/esp32/esp32-hal-log.c
Outdated
int len = vsnprintf(log_buffer, sizeof(log_buffer), format, va_args); | ||
if (len > 0) | ||
{ | ||
switch (level) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this switch can be replaced by the following line:
ESP_LOG_LEVEL_LOCAL(level, tag, "%s", log_buffer);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was my decision to doesn't call directly ESP_LOG_LEVEL_LOCAL, instead using the provided MACROs.
Bu i agree that the switch is resolved in this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by the way, the following should also work:
ESP_LOG_LEVEL_LOCAL(level, tag, (const char *)log_buffer);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, in this way the compiler says :
expression preceding parentheses of apparent call must have (pointer-to-) function typeC/C++(109)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok :)
cores/esp32/esp32-hal-log.c
Outdated
#include "stdio.h" | ||
#include "esp32-hal-log.h" | ||
#ifdef USE_ESP32_LOG | ||
void log_to_esp(esp_log_level_t level, const char *format, ...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please pass also the TAG as argument:
void log_to_esp(esp_log_level_t level, const char *tag, const char *format, ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here i wanted to replicate the actual behavior of the arduino library, so the tag is fixed.
But I understand it's better to put it as a parameter ( and passing it fixed when calling MACROs)
@me-no-dev I have noticed a strange behaviour; if i enable the esp-idf logs and lower the define of CORE_DEBUG_LEVEL to a value under DEBUG (4), the code doesn't compile anymore and this message is shown: I checked that line that raises the error but i cannot understand why it compilessince it's calling an undefined variable |
Because you also need to make the defines based on the log level selected: #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
#define log_e(format, ...) do {log_to_esp(ESP_LOG_ERROR, TAG, format, ##__VA_ARGS__);}while(0)
#define isr_log_e(format, ...) do {ets_printf(LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__);}while(0)
#else
#define log_e(format, ...)
#define isr_log_e(format, ...)
#endif |
Changed TAG definition to "arduino" Removed switch in log_to_esp and used ESP_LOG_LEVEL_LOCAL Added missing isr_log_* definitions FIxed conditional definitions of log MACROs
Many thanks for developing the link with the ESP IDF log. I have been looking at the code (as I had a buggy implementation of my own vprintf to install via esp_log_set_vprintf...) and was wondering why it is necessary to create the log_to_esp function in esp32-hal-log.c, instead if directly referring to ESP_LOG_LEVEL_LOCAL (which ultimately calls esp_log_write) in the definition of log_x macros in esp32-hal-log.h like this: #define log_n(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_ERROR, TAG, format, ##VA_ARGS) I made some tests and it looks to work very fine like that. It spares a function call, the 512b buffer of log_to_esp (esp_log_write in log.c is more prudent with a 64b buffer and malloc if bigger size is necessary) and a call to vsnprintf (esp_log_write in log.c already calls it twice). |
Hi @jfbuggen, thanks for appreciating. |
If your method makes it easier to redirect with esp_log_set_vprintf, then by all means submit it. |
Thank you @martirius. I will have to learn how to do that on GitHub but I will have a try. @lbernstone it will not change much in terms of functionality, esp_log_set_vprintf works fine already with the current code, it brings just a small performance improvement. |
…than using intermediate function log_to_esp As indicated in espressif#4845 (comment) it is more efficient to call directly the ESP LOG macros. This spares a function call, a 512b buffer and a call to vsnprintf. No change in functionality.
…than using intermediate function log_to_esp (#5081) As indicated in #4845 (comment) it is more efficient to call directly the ESP LOG macros. This spares a function call, a 512b buffer and a call to vsnprintf. No change in functionality.
Even after this merge esp32-idf logging is still not usable, because of lots of conflicts with TAG macro in libraries, e.g.
|
Any news on this? |
Hello @zekageri and @cyberman54, if this implementation doesn't work properly, please consider opening a new issue with behavior description. Thanks. |
Thank you for your response. I will test it soon. |
With this PR user can select to use the original ESP-IDF log instead of the redefined one.
User can also redefine the log function as per Logging Library so he can for example redirect logs to a file.
To enable this change just add -DUSE_ESP32_LOG to build flags.
User can also change the default TAG (that now is ES32) to whatever it wants adding '-DTAG="tag_value"' to build flags