Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions components/utilities/log_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ChangeLog

## v0.1.1 - 2025-08-25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please place the latest changes at the top, i.e., before ## v0.1.0 - 2025-7-1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, my bad. Fixed: [adb014e]


### 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.
2 changes: 1 addition & 1 deletion components/utilities/log_router/idf_component.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 11 additions & 12 deletions components/utilities/log_router/log_router.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down