Colored Logs with ESP32 and PlatformIO

Logging is one of the most important tools in software development. The Espressif IoT Development Framework offers a library for this purpose that logs to the UART interface by default. In the PlatformIO IDE, the output with the default settings looks like this:

␛[0;32mI (42) boot.esp32: SPI Mode       : DIO␛[0m
␛[0;32mI (46) boot.esp32: SPI Flash Size : 4MB␛[0m
␛[0;32mI (51) boot: Enabling RNG early entropy source...␛[0m
␛[0;32mI (56) boot: Partition Table:␛[0m
␛[0;32mI (60) boot: ## Label            Usage          Type ST Offset   Length␛[0m
␛[0;32mI (67) boot:  0 nvs              WiFi data        01 02 00009000 00006000␛[0m
␛[0;32mI (75) boot:  1 phy_init         RF data          01 01 0000f000 00001000␛[0m
␛[0;32mI (82) boot:  2 factory          factory app      00 00 00010000 0013d620␛[0m
␛[0;32mI (90) boot: End of partition table␛[0m
␛[0;32mI (94) boot_comm: chip revision: 3, min. application chip revision: 0␛[0m
␛[0;32mI (101) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=18cc8h (101576) map␛[0m
␛[0;32mI (146) esp_image: segment 1: paddr=00028cf0 vaddr=3ffb0000 size=04098h ( 16536) load␛[0m
␛[0;32mI (153) esp_image: segment 2: paddr=0002cd90 vaddr=40080000 size=03288h ( 12936) load␛[0m
␛[0;32mI (158) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=7c164h (508260) map␛[0m
␛[0;32mI (344) esp_image: segment 4: paddr=000ac18c vaddr=40083288 size=12050h ( 73808) load␛[0m
␛[0;32mI (375) esp_image: segment 5: paddr=000be1e4 vaddr=50000000 size=00010h (    16) load␛[0m
␛[0;32mI (386) boot: Loaded app from partition at offset 0x10000␛[0m
␛[0;32mI (386) boot: Disabling RNG early entropy source...␛[0m
␛[0;32mI (397) cpu_start: Pro cpu up.␛[0m
␛[0;32mI (397) cpu_start: Starting app cpu, entry point is 0x40081c14␛[0m
␛[0;32mI (0) cpu_start: App cpu up.␛[0m

The strange characters at the beginning and at the end of each line are so-called ANSI escape sequences with which text properties like foreground and background color can be set.

The structure of an escape sequence looks like this:

␛[CODEm

Several codes can also occur in one sequence. The individual codes are separated by a semicolon:

␛[CODE1;CODE2m

The most important codes for logging are

31 Red  
32 Green  
33 Yellow  
0 Reset/Normal  

 

The Device Monitor of the PlatformIO IDE can handle these escape sequences. To do this, the following bold line must be inserted in the platform.ini file:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter, extra scripting
;   Upload options: custom port, speed and extra flags
;   Library options: dependencies, extra library storages
;
; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html

[env:esp32dev]
platform = espressif32
framework = espidf
board = esp32dev
monitor_speed = 115200
monitor_port = COM4
monitor_flags = --raw

With this setting, the colors encoded in the escape sequences are displayed correctly:

I (42) boot.esp32: SPI Mode       : DIO
I (46) boot.esp32: SPI Flash Size : 4MB
I (51) boot: Enabling RNG early entropy source...
I (56) boot: Partition Table:
I (60) boot: ## Label            Usage          Type ST Offset   Length
I (67) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (75) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (82) boot:  2 factory          factory app      00 00 00010000 0013d620
I (90) boot: End of partition table
I (94) boot_comm: chip revision: 3, min. application chip revision: 0
I (101) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=18cc8h (101576) map
I (146) esp_image: segment 1: paddr=00028cf0 vaddr=3ffb0000 size=04098h ( 16536) load
I (153) esp_image: segment 2: paddr=0002cd90 vaddr=40080000 size=03288h ( 12936) load
I (158) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=7c164h (508260) map
I (344) esp_image: segment 4: paddr=000ac18c vaddr=40083288 size=12050h ( 73808) load
I (375) esp_image: segment 5: paddr=000be1e4 vaddr=50000000 size=00010h (    16) load
I (386) boot: Loaded app from partition at offset 0x10000
I (386) boot: Disabling RNG early entropy source...
I (397) cpu_start: Pro cpu up.
I (397) cpu_start: Starting app cpu, entry point is 0x40081c14
I (0) cpu_start: App cpu up.

The individual log levels are displayed as follows:

E (1301) Loglevel test: Error
W (1305) Loglevel test: Warning
I (1309) Loglevel test: Info
D (1312) Loglevel test: Debug
V (1315) Loglevel test: Verbose

With this simple setting the strange characters have disappeared and the logfile has also become a bit clearer.

Update Aug 05, 2022

Since one of the last PlatformIO updates the following error message appears in the Device Monitor:

Warning! Ignore unknown configuration option `monitor_flags` in section [env:esp32dev]

This is because the monitor_flags = --raw setting is no longer supported. Instead, the monitor_raw = yes setting must be used. The platform.ini file then looks like this:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf
monitor_port = COM3
monitor_speed = 115200
monitor_raw = yes
Loading Conversation