Skip to content
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

Merged
merged 3 commits into from Feb 22, 2021

Conversation

martirius
Copy link
Contributor

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

#ifdef USE_ESP32_LOG

#ifndef TAG
#define TAG "ESP32"
Copy link
Member

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

Copy link
Contributor Author

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 :)

@@ -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
Copy link
Member

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 :)

Copy link
Contributor Author

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

#define __MY_LOG__
#include "stdio.h"
#include "esp32-hal-log.h"
#ifdef USE_ESP32_LOG
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! :)

int len = vsnprintf(log_buffer, sizeof(log_buffer), format, va_args);
if (len > 0)
{
switch (level)
Copy link
Member

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

Copy link
Contributor Author

@martirius martirius Feb 22, 2021

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.

Copy link
Member

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

Copy link
Contributor Author

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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok :)

#include "stdio.h"
#include "esp32-hal-log.h"
#ifdef USE_ESP32_LOG
void log_to_esp(esp_log_level_t level, const char *format, ...)
Copy link
Member

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, ...)

Copy link
Contributor Author

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)

@martirius
Copy link
Contributor Author

@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:
.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFiSTA.cpp:738:25: error: 'sc_status_strings' was not declared in this scope.
However if I don't enable my defined log_* and use the arduino log, the code compile.

I checked that line that raises the error but i cannot understand why it compilessince it's calling an undefined variable

@me-no-dev
Copy link
Member

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
@me-no-dev me-no-dev merged commit b8dab5e into espressif:master Feb 22, 2021
@jfbuggen
Copy link
Contributor

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

@martirius
Copy link
Contributor Author

Hi @jfbuggen, thanks for appreciating.
I'm honest, i don't remember why i took this decision, however you are free to made a PR to with your proposed solution :)

@lbernstone
Copy link
Contributor

If your method makes it easier to redirect with esp_log_set_vprintf, then by all means submit it.

@jfbuggen
Copy link
Contributor

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.

jfbuggen added a commit to jfbuggen/arduino-esp32 that referenced this pull request Apr 19, 2021
…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.
me-no-dev pushed a commit that referenced this pull request Apr 19, 2021
…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.
@cyberman54
Copy link
Contributor

Even after this merge esp32-idf logging is still not usable, because of lots of conflicts with TAG macro in libraries, e.g.

packages/framework-arduinoespressif32/libraries/WiFiClientSecure/src/esp_crt_bundle.c:25:20: note: in expansion of macro 'TAG'
 static const char *TAG = "esp-x509-crt-bundle";
                    ^~~

/cc @coratron @lbernstone @martirius

@zekageri
Copy link

Any news on this?

@VojtechBartoska
Copy link
Collaborator

Hello @zekageri and @cyberman54, if this implementation doesn't work properly, please consider opening a new issue with behavior description. Thanks.

@zekageri
Copy link

Thank you for your response. I will test it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants