From dd4b4d72b684a9e105b7cbc9bce5ba525ec4f771 Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Wed, 3 May 2023 16:57:19 +0530 Subject: [PATCH] [ESP32] Use esp_log_writev() for logging rather than ESP_LOGx macros Basically this unreverts the #26282 with additional changes to fixed the rpc logging problem introduced in #26227. --- config/esp32/components/chip/CMakeLists.txt | 6 ++- examples/platform/esp32/PigweedLogger.cpp | 42 +++++++++++++++++++++ src/platform/ESP32/Logging.cpp | 41 ++++++++++++++------ 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 927c5ddeaa21f4..21f0c2e3bd6936 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -373,8 +373,10 @@ target_link_libraries(${COMPONENT_LIB} INTERFACE -Wl,--start-group add_dependencies(${COMPONENT_LIB} chip_gn) if(CONFIG_ENABLE_PW_RPC) - set(WRAP_FUNCTIONS esp_log_write) - target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${WRAP_FUNCTIONS}") + set(WRAP_FUNCTIONS esp_log_write esp_log_writev) + foreach(func ${WRAP_FUNCTIONS}) + target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${func}") + endforeach() endif() # Build Matter OTA image diff --git a/examples/platform/esp32/PigweedLogger.cpp b/examples/platform/esp32/PigweedLogger.cpp index cc75c60043683d..a158d802b7e4de 100644 --- a/examples/platform/esp32/PigweedLogger.cpp +++ b/examples/platform/esp32/PigweedLogger.cpp @@ -86,6 +86,22 @@ SemaphoreHandle_t * getSemaphore() return &esp_log_mutex; } +static const char * getLogColorForLevel(esp_log_level_t level) +{ + switch (level) + { + case ESP_LOG_ERROR: + return LOG_COLOR_E "E"; + + case ESP_LOG_INFO: + return LOG_COLOR_E "I"; + + default: + // default is kept as ESP_LOG_DEBUG + return LOG_COLOR_E "D"; + } +} + extern "C" void __wrap_esp_log_write(esp_log_level_t level, const char * tag, const char * format, ...) { va_list v; @@ -107,4 +123,30 @@ extern "C" void __wrap_esp_log_write(esp_log_level_t level, const char * tag, co va_end(v); } +extern "C" void __wrap_esp_log_writev(esp_log_level_t level, const char * tag, const char * format, va_list v) +{ +#ifndef CONFIG_LOG_DEFAULT_LEVEL_NONE + if (uartInitialised && level <= CONFIG_LOG_MAXIMUM_LEVEL) + { + const char * logColor = getLogColorForLevel(level); + PigweedLogger::putString(logColor, strlen(logColor)); + + char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE]; + size_t len = snprintf(formattedMsg, sizeof formattedMsg, " (%u) %s: ", esp_log_timestamp(), tag); + PigweedLogger::putString(formattedMsg, len); + + memset(formattedMsg, 0, sizeof formattedMsg); + len = vsnprintf(formattedMsg, sizeof formattedMsg, format, v); + if (len >= sizeof formattedMsg) + { + len = sizeof formattedMsg - 1; + } + PigweedLogger::putString(formattedMsg, len); + + const char * logResetColor = LOG_RESET_COLOR "\n"; + PigweedLogger::putString(logResetColor, strlen(logResetColor)); + } +#endif +} + } // namespace PigweedLogger diff --git a/src/platform/ESP32/Logging.cpp b/src/platform/ESP32/Logging.cpp index b2fbaac3cfbd1a..e62a8a8de3e50d 100644 --- a/src/platform/ESP32/Logging.cpp +++ b/src/platform/ESP32/Logging.cpp @@ -26,21 +26,38 @@ void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char snprintf(tag, sizeof(tag), "chip[%s]", module); tag[sizeof(tag) - 1] = 0; - char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE]; - vsnprintf(formattedMsg, sizeof(formattedMsg), msg, v); - switch (category) { - case kLogCategory_Error: - ESP_LOGE(tag, "%s", formattedMsg); - break; + case kLogCategory_Error: { + if (esp_log_default_level >= ESP_LOG_ERROR) + { + printf(LOG_COLOR_E "E (%u) %s: ", esp_log_timestamp(), tag); + esp_log_writev(ESP_LOG_ERROR, tag, msg, v); + printf(LOG_RESET_COLOR "\n"); + } + } + break; + case kLogCategory_Progress: - default: - ESP_LOGI(tag, "%s", formattedMsg); - break; - case kLogCategory_Detail: - ESP_LOGD(tag, "%s", formattedMsg); - break; + default: { + if (esp_log_default_level >= ESP_LOG_INFO) + { + printf(LOG_COLOR_I "I (%u) %s: ", esp_log_timestamp(), tag); + esp_log_writev(ESP_LOG_INFO, tag, msg, v); + printf(LOG_RESET_COLOR "\n"); + } + } + break; + + case kLogCategory_Detail: { + if (esp_log_default_level >= ESP_LOG_DEBUG) + { + printf(LOG_COLOR_D "D (%u) %s: ", esp_log_timestamp(), tag); + esp_log_writev(ESP_LOG_DEBUG, tag, msg, v); + printf(LOG_RESET_COLOR "\n"); + } + } + break; } }