Great job. Thanks.
Here is a slightly improved and more unambiguous piece of test code and my fully documented version of your code.
also note the following:
-
I often use multiple serial ports so I always name the port that I use for logging and monitoring "SerialMon" and that is reflected in my code below. You do not necessarilly need to do that. You can eliminate the "#define SerialMon Serial" line and just use "Serial" if you like in each place where I used "SerialMon"
-
Some esp32 processors, such ESP32-S2 Mini, still have issues with esp-log. See the setDebugOutput(true) statement in the code below.
-
This must be included in platformio.ini if using platformio
build_flags =
-DCORE_DEBUG_LEVEL=5
Here is the test code.
#include "myesp_log.h"
#define SerialMon Serial
void setup(void)
{
SerialMon.begin(115200);
while (!Serial)
;
SerialMon.setDebugOutput(true); // this is important for some esp processors such as S2-mini
// Set global log level to to some level initially
esp_log_level_set("*", MY_GLOBAL_DEBUG_LEVEL);
static const char *TAG = "tag";
static const char *TAG2 = "OTHERtag";
esp_log_level_set(TAG, ESP_LOG_ERROR);
ESP_LOGV(TAG,"LOG_V");
ESP_LOGD(TAG,"LOG_D");
ESP_LOGE(TAG,"LOG_E");
ESP_LOGV(TAG2,"LOG_V");
ESP_LOGD(TAG2,"LOG_D");
ESP_LOGE(TAG2,"LOG_E");
esp_log_level_set(TAG, ESP_LOG_DEBUG);
ESP_LOGV(TAG,"LOG_V2");
ESP_LOGD(TAG,"LOG_D2");
ESP_LOGE(TAG,"LOG_E2");
ESP_LOGV(TAG2,"LOG_V");
ESP_LOGD(TAG2,"LOG_D");
ESP_LOGE(TAG2,"LOG_E");
for(;;)
}
fully documented version of myesp_log.h
#ifndef MYESP_LOG_H
#define MYESP_LOG_H
//** NOTES:
//***
//** USE myesp_log.h IN PLACE OF esp_log.h
//**
//** 1) this must be included in platformio.ini
//** build_flags =
//** -DCORE_DEBUG_LEVEL=5 */
//**
//** 2) in main.cpp, before setup(), include the following:
//**
//** #include "myesp_log.h"
//** #define MY_GLOBAL_DEBUG_LEVEL ESP_LOG_DEBUG // for example
//**
//** 3) in setup(), include the following after setting up Serial port
//** Serial.setDebugOutput(true); // this is important for some esp processors such as S2-mini
//**
//** // Set global log level to to some level initially
//** esp_log_level_set("*", MY_GLOBAL_DEBUG_LEVEL);
//**
//** then in subsequent code, you can use lines like the following to change the log level for each function, such as...
//**
//** esp_log_level_set(TAG, ESP_LOG_ERROR);
// Library/Arduino15/packages/esp32/tools/esp32-arduino-libs/idf-release_v5.1-632e0c2a/esp32/include/log/include/
#include <esp_log.h>
// Library/Arduino15/packages/esp32/hardware/esp32/3.0.7/cores/esp32/esp32-hal-log.h
#include <esp32-hal-log.h>
#undef CONFIG_LOG_MAXIMUM_LEVEL
#define CONFIG_LOG_MAXIMUM_LEVEL CORE_DEBUG_LEVEL
#undef ESP_LOGE
#undef ESP_LOGW
#undef ESP_LOGI
#undef ESP_LOGD
#undef ESP_LOGV
#define ESP_LOGE( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_ERROR, tag, ARDUHAL_LOG_FORMAT(E, format) __VA_OPT__(,) __VA_ARGS__)
#define ESP_LOGW( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_WARN, tag, ARDUHAL_LOG_FORMAT(W, format) __VA_OPT__(,) __VA_ARGS__)
#define ESP_LOGI( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_INFO, tag, ARDUHAL_LOG_FORMAT(I, format) __VA_OPT__(,) __VA_ARGS__)
#define ESP_LOGD( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_DEBUG, tag, ARDUHAL_LOG_FORMAT(D, format) __VA_OPT__(,) __VA_ARGS__)
#define ESP_LOGV( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_VERBOSE, tag, ARDUHAL_LOG_FORMAT(V, format) __VA_OPT__(,) __VA_ARGS__)
#undef ARDUHAL_LOG_FORMAT
#define ARDUHAL_LOG_FORMAT(letter, format) \
ARDUHAL_LOG_COLOR_##letter "[%s:%u] %s(): " format ARDUHAL_LOG_RESET_COLOR, \
pathToFileName(__FILE__), __LINE__, __FUNCTION__
#endif // MYESP_LOG_H