diff --git a/components/utilities/log_router/CHANGELOG.md b/components/utilities/log_router/CHANGELOG.md index 0a0d3646e..b62204915 100644 --- a/components/utilities/log_router/CHANGELOG.md +++ b/components/utilities/log_router/CHANGELOG.md @@ -1,5 +1,12 @@ # ChangeLog +## v0.1.1 - 2025-08-25 + +### Bug fix ### + +* Do not enter critical section until the mutex is taken. +* Return ESP_ERR_NO_MEM if creating the mutex fails. +* ## v0.1.0 - 2025-7-1 First release version. diff --git a/components/utilities/log_router/idf_component.yml b/components/utilities/log_router/idf_component.yml index 00aad5918..1c5307c42 100644 --- a/components/utilities/log_router/idf_component.yml +++ b/components/utilities/log_router/idf_component.yml @@ -1,4 +1,4 @@ -version: "0.1.0" +version: "0.1.1" description: LogRouter redirects and filters ESP logs to various storage media url: https://github.com/espressif/esp-iot-solution/tree/master/components/utilities/log_router repository: https://github.com/espressif/esp-iot-solution.git diff --git a/components/utilities/log_router/log_router.c b/components/utilities/log_router/log_router.c index 213080f81..941081b27 100644 --- a/components/utilities/log_router/log_router.c +++ b/components/utilities/log_router/log_router.c @@ -118,9 +118,7 @@ int esp_log_router_flash_vprintf(const char *format, va_list args) } // Lock mutex for thread safety - if (g_log_router_mutex) { - xSemaphoreTake(g_log_router_mutex, pdMS_TO_TICKS(100)); - } + xSemaphoreTake(g_log_router_mutex, portMAX_DELAY); // Write to all files that match the log level esp_log_router_slist_t *item; @@ -244,9 +242,7 @@ int esp_log_router_flash_vprintf(const char *format, va_list args) } // Release mutex - if (g_log_router_mutex) { - xSemaphoreGive(g_log_router_mutex); - } + xSemaphoreGive(g_log_router_mutex); return ret; } @@ -290,6 +286,15 @@ static void esp_log_router_shutdown(void) esp_err_t esp_log_router_to_file(const char* file_path, const char* tag, esp_log_level_t level) { + // Create mutex for thread safety, the first time this function is called. + if (g_log_router_mutex == NULL) { + g_log_router_mutex = xSemaphoreCreateMutex(); + if (g_log_router_mutex == NULL) { + ESP_LOGE(TAG, "Failed to create mutex for log router"); + return ESP_ERR_NO_MEM; + } + } + if (!file_path) { return ESP_ERR_INVALID_ARG; } @@ -376,12 +381,6 @@ esp_err_t esp_log_router_to_file(const char* file_path, const char* tag, esp_log SLIST_INSERT_HEAD(&g_esp_log_router_slist_head, new_log_router, next); g_esp_log_router_vprintf = esp_log_set_vprintf(esp_log_router_flash_vprintf); esp_register_shutdown_handler(esp_log_router_shutdown); - - // Create mutex for thread safety when first node is added - g_log_router_mutex = xSemaphoreCreateMutex(); - if (g_log_router_mutex == NULL) { - ESP_LOGW(TAG, "Failed to create mutex for log router"); - } } else { // Find the last node and insert after it esp_log_router_slist_t *last = SLIST_FIRST(&g_esp_log_router_slist_head);