Skip to content

Commit 5604616

Browse files
shubhamdppull[bot]
authored andcommitted
[ESP32] Use esp_log_writev() for logging rather than ESP_LOGx macros (#26348)
* [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. * Fix the bug when setting the log level * remove the log level check * Fix the color codes for pigweed logs * Added comment explaining the addition of color codes in pigweed logger
1 parent 5d87e5c commit 5604616

File tree

3 files changed

+101
-18
lines changed

3 files changed

+101
-18
lines changed

config/esp32/components/chip/CMakeLists.txt

+27-6
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,29 @@ chip_gn_arg_append("esp32_cpu" "\"esp32\"")
8686
chip_gn_arg_bool("is_debug" ${is_debug})
8787

8888
# Config the chip log level by IDF menuconfig
89-
chip_gn_arg_bool ("chip_error_logging" CONFIG_LOG_DEFAULT_LEVEL GREATER_EQUAL 1)
90-
chip_gn_arg_bool ("chip_progress_logging" CONFIG_LOG_DEFAULT_LEVEL GREATER_EQUAL 3)
91-
chip_gn_arg_bool ("chip_detail_logging" CONFIG_LOG_DEFAULT_LEVEL GREATER_EQUAL 4)
92-
chip_gn_arg_bool ("chip_automation_logging" CONFIG_LOG_DEFAULT_LEVEL GREATER_EQUAL 5)
89+
if (CONFIG_LOG_DEFAULT_LEVEL GREATER_EQUAL 1)
90+
chip_gn_arg_bool ("chip_error_logging" "true")
91+
else()
92+
chip_gn_arg_bool ("chip_error_logging" "false")
93+
endif()
94+
95+
if (CONFIG_LOG_DEFAULT_LEVEL GREATER_EQUAL 3)
96+
chip_gn_arg_bool ("chip_progress_logging" "true")
97+
else()
98+
chip_gn_arg_bool ("chip_progress_logging" "false")
99+
endif()
100+
101+
if (CONFIG_LOG_DEFAULT_LEVEL GREATER_EQUAL 4)
102+
chip_gn_arg_bool ("chip_detail_logging" "true")
103+
else()
104+
chip_gn_arg_bool ("chip_detail_logging" "false")
105+
endif()
106+
107+
if (CONFIG_LOG_DEFAULT_LEVEL GREATER_EQUAL 5)
108+
chip_gn_arg_bool ("chip_automation_logging" "true")
109+
else()
110+
chip_gn_arg_bool ("chip_automation_logging" "false")
111+
endif()
93112

94113
if(CONFIG_ENABLE_CHIPOBLE)
95114
chip_gn_arg_append("chip_config_network_layer_ble" "true")
@@ -373,8 +392,10 @@ target_link_libraries(${COMPONENT_LIB} INTERFACE -Wl,--start-group
373392
add_dependencies(${COMPONENT_LIB} chip_gn)
374393

375394
if(CONFIG_ENABLE_PW_RPC)
376-
set(WRAP_FUNCTIONS esp_log_write)
377-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${WRAP_FUNCTIONS}")
395+
set(WRAP_FUNCTIONS esp_log_write esp_log_writev)
396+
foreach(func ${WRAP_FUNCTIONS})
397+
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${func}")
398+
endforeach()
378399
endif()
379400

380401
# Build Matter OTA image

examples/platform/esp32/PigweedLogger.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,28 @@ SemaphoreHandle_t * getSemaphore()
8686
return &esp_log_mutex;
8787
}
8888

89+
static const char * getLogColorForLevel(esp_log_level_t level)
90+
{
91+
switch (level)
92+
{
93+
case ESP_LOG_ERROR:
94+
return LOG_COLOR_E "E";
95+
96+
case ESP_LOG_INFO:
97+
return LOG_COLOR_I "I";
98+
99+
default:
100+
// default is kept as ESP_LOG_DEBUG
101+
return LOG_COLOR_D "D";
102+
}
103+
}
104+
105+
// ESP_LOGx(...) logs are funneled to esp_log_write() and it has the specific format. It contains color codes,
106+
// log level character, timestamp, tag, and actual log message. Everything here is part of format and variadic argument.
107+
//
108+
// ChipLogx(...) logs are funneled to esp_log_writev() will only have actual log message without color codes, log level
109+
// character, timestamp, and tag. So, to match up __wrap_esp_log_writev() adds those details when sending log to pigweed
110+
// logger.
89111
extern "C" void __wrap_esp_log_write(esp_log_level_t level, const char * tag, const char * format, ...)
90112
{
91113
va_list v;
@@ -107,4 +129,30 @@ extern "C" void __wrap_esp_log_write(esp_log_level_t level, const char * tag, co
107129
va_end(v);
108130
}
109131

132+
extern "C" void __wrap_esp_log_writev(esp_log_level_t level, const char * tag, const char * format, va_list v)
133+
{
134+
#ifndef CONFIG_LOG_DEFAULT_LEVEL_NONE
135+
if (uartInitialised && level <= CONFIG_LOG_MAXIMUM_LEVEL)
136+
{
137+
const char * logColor = getLogColorForLevel(level);
138+
PigweedLogger::putString(logColor, strlen(logColor));
139+
140+
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
141+
size_t len = snprintf(formattedMsg, sizeof formattedMsg, " (%u) %s: ", esp_log_timestamp(), tag);
142+
PigweedLogger::putString(formattedMsg, len);
143+
144+
memset(formattedMsg, 0, sizeof formattedMsg);
145+
len = vsnprintf(formattedMsg, sizeof formattedMsg, format, v);
146+
if (len >= sizeof formattedMsg)
147+
{
148+
len = sizeof formattedMsg - 1;
149+
}
150+
PigweedLogger::putString(formattedMsg, len);
151+
152+
const char * logResetColor = LOG_RESET_COLOR "\n";
153+
PigweedLogger::putString(logResetColor, strlen(logResetColor));
154+
}
155+
#endif
156+
}
157+
110158
} // namespace PigweedLogger

src/platform/ESP32/Logging.cpp

+26-12
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,35 @@ void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char
2626
snprintf(tag, sizeof(tag), "chip[%s]", module);
2727
tag[sizeof(tag) - 1] = 0;
2828

29-
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
30-
vsnprintf(formattedMsg, sizeof(formattedMsg), msg, v);
31-
3229
switch (category)
3330
{
34-
case kLogCategory_Error:
35-
ESP_LOGE(tag, "%s", formattedMsg);
36-
break;
31+
case kLogCategory_Error: {
32+
{
33+
printf(LOG_COLOR_E "E (%u) %s: ", esp_log_timestamp(), tag);
34+
esp_log_writev(ESP_LOG_ERROR, tag, msg, v);
35+
printf(LOG_RESET_COLOR "\n");
36+
}
37+
}
38+
break;
39+
3740
case kLogCategory_Progress:
38-
default:
39-
ESP_LOGI(tag, "%s", formattedMsg);
40-
break;
41-
case kLogCategory_Detail:
42-
ESP_LOGD(tag, "%s", formattedMsg);
43-
break;
41+
default: {
42+
{
43+
printf(LOG_COLOR_I "I (%u) %s: ", esp_log_timestamp(), tag);
44+
esp_log_writev(ESP_LOG_INFO, tag, msg, v);
45+
printf(LOG_RESET_COLOR "\n");
46+
}
47+
}
48+
break;
49+
50+
case kLogCategory_Detail: {
51+
{
52+
printf(LOG_COLOR_D "D (%u) %s: ", esp_log_timestamp(), tag);
53+
esp_log_writev(ESP_LOG_DEBUG, tag, msg, v);
54+
printf(LOG_RESET_COLOR "\n");
55+
}
56+
}
57+
break;
4458
}
4559
}
4660

0 commit comments

Comments
 (0)