diff --git a/platformio.ini b/platformio.ini index 428ba61..1b3eeee 100644 --- a/platformio.ini +++ b/platformio.ini @@ -37,8 +37,9 @@ build_flags = -DCONFIG_ESP_COREDUMP_CHECKSUM_CRC32=1 -DCONFIG_ESP_TASK_WDT_PANIC=1 -DCONFIG_ESP_COREDUMP_DECODE_INFO=1 - ; Route log_printf through src/LogLock.cpp so lines from both cores don't interleave. - -Wl,--wrap=log_printf + ; Route log_printfv through src/LogLock.cpp so lines from both cores don't + ; interleave. The core already wraps log_printf for ESP Insights. + -Wl,--wrap=log_printfv monitor_speed = 115200 monitor_filters = time diff --git a/src/LogLock.cpp b/src/LogLock.cpp index 2c06388..7f3198d 100644 --- a/src/LogLock.cpp +++ b/src/LogLock.cpp @@ -1,20 +1,21 @@ // Prints each log line whole. // -// The Arduino core's log_printf() writes with ets_printf() and has its UART +// The Arduino core's log_printfv() writes with ets_printf() and has its UART // lock commented out, so lines logged at the same time from async_tcp (core 0) // and loop() (core 1) come out interleaved character by character. It also // formats short lines into one static buffer shared by every task. // -// platformio.ini links with -Wl,--wrap=log_printf so every ESP_LOGx/log_x call -// lands here and takes this lock around the core's log_printfv(). +// The core already links with --wrap=log_printf, which sends every +// ESP_LOGx/log_x call to the ESP Insights hook in libesp_diagnostics, and that +// hook prints through log_printfv(). platformio.ini adds +// -Wl,--wrap=log_printfv so that call lands here and takes this lock. #include #include #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "freertos/task.h" -// Defined in the core's esp32-hal-uart.c but not declared in a header. -extern "C" int log_printfv(const char *format, va_list arg); +extern "C" int __real_log_printfv(const char *format, va_list arg); // Created during static initialisation, before anything can race for it. A // constructor that logs before this runs simply goes unlocked. @@ -23,14 +24,11 @@ static SemaphoreHandle_t logMutex = xSemaphoreCreateRecursiveMutex(); // task stuck mid-log can only garble output, not hang another task. #define LOG_LOCK_WAIT_MS 50 -extern "C" int __wrap_log_printf(const char *format, ...) { +extern "C" int __wrap_log_printfv(const char *format, va_list arg) { // No blocking before the scheduler runs, in an ISR or in a critical section. const bool canLock = logMutex && xTaskGetSchedulerState() == taskSCHEDULER_RUNNING && xPortCanYield(); const bool locked = canLock && xSemaphoreTakeRecursive(logMutex, pdMS_TO_TICKS(LOG_LOCK_WAIT_MS)) == pdTRUE; - va_list arg; - va_start(arg, format); - int len = log_printfv(format, arg); - va_end(arg); + int len = __real_log_printfv(format, arg); if(locked) xSemaphoreGiveRecursive(logMutex); return len; }