Skip to content

Commit

Permalink
[ESP32] Use esp_log_writev() for logging rather than ESP_LOGx macros
Browse files Browse the repository at this point in the history
Basically this unreverts the project-chip#26282 with additional changes to fixed
the rpc logging problem introduced in project-chip#26227.
  • Loading branch information
shubhamdp committed May 8, 2023
1 parent 6c0c5ea commit dd4b4d7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
6 changes: 4 additions & 2 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions examples/platform/esp32/PigweedLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
41 changes: 29 additions & 12 deletions src/platform/ESP32/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit dd4b4d7

Please sign in to comment.