Skip to content

Commit 0a268e1

Browse files
committed
Merge branch 'feat/esp_log_support_router' into 'master'
feat(log_router): support redirection and filtering of esp_log See merge request ae_group/esp-iot-solution!1313
2 parents d6b2c33 + 838607b commit 0a268e1

File tree

19 files changed

+1081
-6
lines changed

19 files changed

+1081
-6
lines changed

.github/workflows/upload_component.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ jobs:
101101
components/usb/iot_usbh_ecm;
102102
components/usb/usb_stream;
103103
components/utilities/adc_tp_calibration;
104+
components/utilities/log_router;
104105
components/utilities/xz;
105106
components/zero_detection;
106107
tools/cmake_utilities;

.gitlab/ci/build.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,14 @@ build_components_utilities_adc_tp_calibration_test_apps:
18021802
variables:
18031803
EXAMPLE_DIR: components/utilities/adc_tp_calibration/test_apps
18041804

1805+
build_components_utilities_log_router_test_apps:
1806+
extends:
1807+
- .build_examples_template
1808+
- .rules:build:components_utilities_log_router_test_apps
1809+
- .build_idf_version_greater_equal_v5_0
1810+
variables:
1811+
EXAMPLE_DIR: components/utilities/log_router/test_apps
1812+
18051813
build_components_zero_detection_test_apps:
18061814
extends:
18071815
- .build_examples_template

.gitlab/ci/rules.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@
371371
- "components/utilities/adc_tp_calibration/**/*"
372372
- "tools/cmake_utilities/package_manager.cmake"
373373

374+
.patterns-components_utilities_log_router: &patterns-components_utilities_log_router
375+
- "components/utilities/log_router/**/*"
376+
- "tools/cmake_utilities/package_manager.cmake"
377+
374378
# examples folder, in the alphabetic order
375379
.patterns-docs: &patterns-docs
376380
- "docs/**/*"
@@ -2681,6 +2685,17 @@
26812685
- <<: *if-dev-push
26822686
changes: *patterns-components_utilities_adc_tp_calibration
26832687

2688+
.rules:build:components_utilities_log_router_test_apps:
2689+
rules:
2690+
- <<: *if-protected
2691+
- <<: *if-label-build
2692+
- <<: *if-label-target_test
2693+
- <<: *if-trigger-job
2694+
- <<: *if-dev-push
2695+
changes: *patterns-build_system
2696+
- <<: *if-dev-push
2697+
changes: *patterns-components_utilities_log_router
2698+
26842699
.rules:build:components_sensors_humiture_aht20_test_apps:
26852700
rules:
26862701
- <<: *if-protected

.gitlab/ci/target_test.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -799,12 +799,13 @@ components_test_zero_detection:
799799
- IDF_TARGET: esp32s3
800800
IDF_VERSION: "5.1"
801801
ENV_TAG: zero_detection
802-
- IDF_TARGET: esp32c3
803-
IDF_VERSION: "5.0"
804-
ENV_TAG: zero_detection
805-
- IDF_TARGET: esp32c3
806-
IDF_VERSION: "5.1"
807-
ENV_TAG: zero_detection
802+
# - IDF_TARGET: esp32c3
803+
# IDF_VERSION: "5.0"
804+
# ENV_TAG: zero_detection
805+
# - IDF_TARGET: esp32c3
806+
# IDF_VERSION: "5.1"
807+
# ENV_TAG: zero_detection
808+
# Due to memory issues, the ESP32-C3 test case is temporarily skipped and will be restored after the problem is resolved. AEG-2602
808809
tags:
809810
- ${IDF_TARGET}
810811
- ${ENV_TAG}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ The registered components in ESP-IoT-Solution are listed below:
121121
| [led_indicator](https://components.espressif.com/components/espressif/led_indicator) | [![Component Registry](https://components.espressif.com/components/espressif/led_indicator/badge.svg)](https://components.espressif.com/components/espressif/led_indicator) |
122122
| [lightbulb_driver](https://components.espressif.com/components/espressif/lightbulb_driver) | [![Component Registry](https://components.espressif.com/components/espressif/lightbulb_driver/badge.svg)](https://components.espressif.com/components/espressif/lightbulb_driver) |
123123
| [lis2dh12](https://components.espressif.com/components/espressif/lis2dh12) | [![Component Registry](https://components.espressif.com/components/espressif/lis2dh12/badge.svg)](https://components.espressif.com/components/espressif/lis2dh12) |
124+
| [log_router](https://components.espressif.com/components/espressif/log_router) | [![Component Registry](https://components.espressif.com/components/espressif/log_router/badge.svg)](https://components.espressif.com/components/espressif/log_router) |
124125
| [max17048](https://components.espressif.com/components/espressif/max17048) | [![Component Registry](https://components.espressif.com/components/espressif/max17048/badge.svg)](https://components.espressif.com/components/espressif/max17048) |
125126
| [mcp23017](https://components.espressif.com/components/espressif/mcp23017) | [![Component Registry](https://components.espressif.com/components/espressif/mcp23017/badge.svg)](https://components.espressif.com/components/espressif/mcp23017) |
126127
| [mcp3201](https://components.espressif.com/components/espressif/mcp3201) | [![Component Registry](https://components.espressif.com/components/espressif/mcp3201/badge.svg)](https://components.espressif.com/components/espressif/mcp3201) |

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ ESP-IoT-Solution 中注册的组件如下:
121121
| [led_indicator](https://components.espressif.com/components/espressif/led_indicator) | [![Component Registry](https://components.espressif.com/components/espressif/led_indicator/badge.svg)](https://components.espressif.com/components/espressif/led_indicator) |
122122
| [lightbulb_driver](https://components.espressif.com/components/espressif/lightbulb_driver) | [![Component Registry](https://components.espressif.com/components/espressif/lightbulb_driver/badge.svg)](https://components.espressif.com/components/espressif/lightbulb_driver) |
123123
| [lis2dh12](https://components.espressif.com/components/espressif/lis2dh12) | [![Component Registry](https://components.espressif.com/components/espressif/lis2dh12/badge.svg)](https://components.espressif.com/components/espressif/lis2dh12) |
124+
| [log_router](https://components.espressif.com/components/espressif/log_router) | [![Component Registry](https://components.espressif.com/components/espressif/log_router/badge.svg)](https://components.espressif.com/components/espressif/log_router) |
124125
| [max17048](https://components.espressif.com/components/espressif/max17048) | [![Component Registry](https://components.espressif.com/components/espressif/max17048/badge.svg)](https://components.espressif.com/components/espressif/max17048) |
125126
| [mcp23017](https://components.espressif.com/components/espressif/mcp23017) | [![Component Registry](https://components.espressif.com/components/espressif/mcp23017/badge.svg)](https://components.espressif.com/components/espressif/mcp23017) |
126127
| [mcp3201](https://components.espressif.com/components/espressif/mcp3201) | [![Component Registry](https://components.espressif.com/components/espressif/mcp3201/badge.svg)](https://components.espressif.com/components/espressif/mcp3201) |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ChangeLog
2+
3+
## v0.1.0 - 2025-7-1
4+
5+
First release version.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
idf_component_register(SRC_DIRS "."
2+
INCLUDE_DIRS "include"
3+
REQUIRES "fatfs" "esp_timer")
4+
5+
include(package_manager)
6+
cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR})
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
menu "LOG ROUTER"
2+
3+
config LOG_ROUTER_BUFFER_SIZE
4+
int "Buffer size (bytes)"
5+
range 2048 10240
6+
default 4096
7+
help
8+
Size of the buffer used for log buffering.
9+
Larger buffer reduces flash write frequency but uses more RAM.
10+
Range: 2KB (2048) to 10KB (10240)
11+
12+
config LOG_ROUTER_FLUSH_THRESHOLD_PERCENT
13+
int "Flush threshold percentage"
14+
range 50 90
15+
default 75
16+
help
17+
Percentage of buffer size at which to trigger a flush.
18+
Higher percentage means more data buffered before writing to flash.
19+
Range: 50% to 90%
20+
21+
config LOG_ROUTER_FLUSH_TIMEOUT_MS
22+
int "Flush timeout (milliseconds)"
23+
range 2000 10000
24+
default 2000
25+
help
26+
Maximum time to wait before flushing buffer to flash.
27+
Shorter timeout ensures more frequent writes but may reduce performance.
28+
Range: 2 seconds (2000ms) to 10 seconds (10000ms)
29+
30+
config LOG_ROUTER_FORMAT_BUFFER_SIZE
31+
int "Log format buffer size (bytes)"
32+
range 256 1024
33+
default 256
34+
help
35+
Size of the buffer used for formatting log messages in log_router.
36+
Larger buffer can handle longer log messages but uses more stack space.
37+
Range: 256 to 1024 bytes
38+
39+
config LOG_ROUTER_DEBUG_OUTPUT
40+
bool "Enable debug output"
41+
default y
42+
help
43+
Enable debug output for log router operations.
44+
When enabled, printf statements will show buffer flush events,
45+
timeout triggers, and error conditions.
46+
Disable this in production to improve performance.
47+
48+
endmenu
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# LOG ROUTER
2+
3+
The `log_router` is an extension component of `esp_log` that allows users to save logs of specific levels and tags through the file system. By configuring the save path, log level, and tag filter, logs can be redirected to corresponding log files while preserving the original log output. This component provides the following features:
4+
5+
1. Supports redirecting logs of specific levels to the file system while preserving the original log output interface
6+
2. Supports filtering logs by specific tags to capture only relevant log messages
7+
3. Supports batch writing of logs to the file system to avoid frequent flash operations
8+
9+
## How to Configure Router Parameters
10+
11+
First, you need to define a log partition with type `data` and subtype `fat` in the partition table, or access an SD card or external flash through the FAT file system, and complete the initialization and mounting of the FAT file system in your application.
12+
13+
This mounting process is typically implemented using convenience functions such as `esp_vfs_fat_spiflash_mount()` or `esp_vfs_fat_sdmmc_mount()`, which are responsible for formatting the underlying FAT partition (if needed), mounting it, and registering it to ESP-IDF's Virtual File System (VFS). Once completed, you can use standard C file operation functions (such as fopen(), fread(), fprintf(), etc.) to read and write to this file system, just like operating on local files, facilitating tasks such as log storage and configuration persistence.
14+
15+
> **Note:** `log_router` as a middleware does not handle the initialization of the FAT file system. Please complete the initialization and mounting of the FAT file system before using `log_router`. Additionally, the current version may occupy certain system time when writing due to timeout or reaching the set threshold, which may affect some time-sensitive programs.
16+
17+
## Add component to your project
18+
19+
Please use the component manager command `add-dependency` to add the `log_router` to your project's dependency, during the `CMake` step the component will be downloaded automatically
20+
21+
```
22+
idf.py add-dependency "espressif/log_router=*"
23+
```
24+
25+
Alternatively, you can create `idf_component.yml`. More is in [Espressif's documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html).
26+
27+
## How to use
28+
29+
The usage of ``log_router`` is very simple. You only need to call `esp_log_router_to_file` and fill in the file to redirect to, the specified `tag` and `level` to achieve log redirection. Note that if `tag` is `NULL`, all logs greater than or equal to the set `level` will be saved.
30+
31+
```c
32+
// Step1: Initialize FAT file system
33+
34+
...
35+
36+
// Step2: Redirect log output
37+
38+
esp_log_router_to_file("/log/log.txt", TAG, ESP_LOG_WARN);
39+
40+
// Step3: If you want to cancel log saving
41+
42+
esp_log_delete_router("/log/log.txt");
43+
44+
```
45+
46+
If you are using ``fatfs``, you can use the ``fatfsparse.py`` provided in the ``ESP-IDF`` SDK to parse it out. Take the following partition table as an example:
47+
48+
```csv
49+
# Name, Type, SubType, Offset, Size, Flags
50+
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
51+
factory, app, factory, 0x10000, 1M,
52+
log, data, fat, , 500K,
53+
```
54+
55+
First, you need to read out the content of the specific partition:
56+
57+
```shell
58+
parttool.py -p /dev/ttyUSB0 read_partition --partition-name=log --output "log.bin"
59+
```
60+
61+
Next, use the parsing script ``fatfsparse.py`` for parsing, which is located in the ``components/fatfs`` directory under the SDK:
62+
63+
```shell
64+
cd ${IDF_PATH}/components/fatfs
65+
./fatfsparse.py log.bin
66+
```

0 commit comments

Comments
 (0)