Skip to content

Commit 8d51d49

Browse files
committed
Logging refactoring to supports macros redefinition and -D USE_ESP_IDF_LOG=1
1 parent af50210 commit 8d51d49

File tree

13 files changed

+230
-121
lines changed

13 files changed

+230
-121
lines changed

.github/workflows/build-esp32.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ jobs:
226226
env:
227227
- ci-arduino-rc-asynctcp
228228
- ci-arduino-3-no-json
229+
- ci-arduino-2-esp-idf-log
230+
- ci-arduino-3-esp-idf-log
229231

230232
steps:
231233
- name: Checkout

examples/ChunkRetryResponse/ChunkRetryResponse.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void setup() {
174174
return 0; // 0 means we are done
175175
}
176176

177-
// log_d("UART answered!");
177+
// async_ws_log_d("UART answered!");
178178

179179
String answer = "You typed: ";
180180
answer.concat((char)key);
@@ -193,18 +193,18 @@ void setup() {
193193
},
194194
NULL, // upload handler is not used so it should be NULL
195195
[](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
196-
// log_d("Body: index: %u, len: %u, total: %u", index, len, total);
196+
// async_ws_log_d("Body: index: %u, len: %u, total: %u", index, len, total);
197197

198198
if (!index) {
199-
// log_d("Start body parsing");
199+
// async_ws_log_d("Start body parsing");
200200
request->_tempObject = new String();
201201
// cast request->_tempObject pointer to String and reserve total size
202202
((String *)request->_tempObject)->reserve(total);
203203
// set timeout 30s
204204
request->client()->setRxTimeout(30);
205205
}
206206

207-
// log_d("Append body data");
207+
// async_ws_log_d("Append body data");
208208
((String *)request->_tempObject)->concat((const char *)data, len);
209209
}
210210
);
@@ -217,13 +217,13 @@ void setup() {
217217
void loop() {
218218
if (triggerUART.length() && key == -1) {
219219
Serial.println(triggerUART);
220-
// log_d("Waiting for UART input...");
220+
// async_ws_log_d("Waiting for UART input...");
221221
while (!Serial.available()) {
222222
delay(100);
223223
}
224224
key = Serial.read();
225225
Serial.flush();
226-
// log_d("UART input: %c", key);
226+
// async_ws_log_d("UART input: %c", key);
227227
triggerUART = emptyString;
228228
}
229229
}

platformio.ini

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ build_flags =
5252
-D CONFIG_ASYNC_TCP_RUNNING_CORE=1
5353
-D CONFIG_ASYNC_TCP_STACK_SIZE=4096
5454
; -D CONFIG_ASYNC_TCP_USE_WDT=0
55+
; -D USE_ESP_IDF_LOG
56+
; -D TAG=\"core\"
5557
upload_protocol = esptool
5658
monitor_speed = 115200
5759
monitor_filters = esp32_exception_decoder, log2file
@@ -135,6 +137,20 @@ board = ${sysenv.PIO_BOARD}
135137
platform = https://github.com/pioarduino/platform-espressif32/releases/download/54.03.20-rc2/platform-espressif32.zip
136138
board = ${sysenv.PIO_BOARD}
137139

140+
[env:ci-arduino-2-esp-idf-log]
141+
platform = espressif32@6.12.0
142+
board = ${sysenv.PIO_BOARD}
143+
build_flags =
144+
${env.build_flags}
145+
-D USE_ESP_IDF_LOG=1
146+
-D TAG=\"core\"
147+
148+
[env:ci-arduino-3-esp-idf-log]
149+
board = ${sysenv.PIO_BOARD}
150+
build_flags =
151+
${env.build_flags}
152+
-D USE_ESP_IDF_LOG=1
153+
138154
[env:ci-arduino-3-no-json]
139155
board = ${sysenv.PIO_BOARD}
140156
lib_deps =

src/AsyncEventSource.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
// SPDX-License-Identifier: LGPL-3.0-or-later
22
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
33

4-
#include "Arduino.h"
5-
#if defined(ESP32)
6-
#include <rom/ets_sys.h>
7-
#endif
84
#include "AsyncEventSource.h"
5+
#include "AsyncWebServerLogging.h"
96

107
#define ASYNC_SSE_NEW_LINE_CHAR (char)0xa
118

@@ -25,9 +22,7 @@ static String generateEventMessage(const char *message, const char *event, uint3
2522
len += 42; // give it some overhead
2623

2724
if (!str.reserve(len)) {
28-
#ifdef ESP32
29-
log_e("Failed to allocate");
30-
#endif
25+
async_ws_log_e("Failed to allocate");
3126
return emptyString;
3227
}
3328

@@ -201,11 +196,7 @@ AsyncEventSourceClient::~AsyncEventSourceClient() {
201196

202197
bool AsyncEventSourceClient::_queueMessage(const char *message, size_t len) {
203198
if (_messageQueue.size() >= SSE_MAX_QUEUED_MESSAGES) {
204-
#ifdef ESP8266
205-
ets_printf(String(F("ERROR: Too many messages queued\n")).c_str());
206-
#elif defined(ESP32)
207-
log_e("Event message queue overflow: discard message");
208-
#endif
199+
async_ws_log_e("Event message queue overflow: discard message");
209200
return false;
210201
}
211202

@@ -231,11 +222,7 @@ bool AsyncEventSourceClient::_queueMessage(const char *message, size_t len) {
231222

232223
bool AsyncEventSourceClient::_queueMessage(AsyncEvent_SharedData_t &&msg) {
233224
if (_messageQueue.size() >= SSE_MAX_QUEUED_MESSAGES) {
234-
#ifdef ESP8266
235-
ets_printf(String(F("ERROR: Too many messages queued\n")).c_str());
236-
#elif defined(ESP32)
237-
log_e("Event message queue overflow: discard message");
238-
#endif
225+
async_ws_log_e("Event message queue overflow: discard message");
239226
return false;
240227
}
241228

src/AsyncJson.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
33

44
#include "AsyncJson.h"
5+
#include "AsyncWebServerLogging.h"
56

67
#if ASYNC_JSON_SUPPORT == 1
78

@@ -123,9 +124,7 @@ void AsyncCallbackJsonWebHandler::handleRequest(AsyncWebServerRequest *request)
123124
// POST / PUT / ... requests:
124125
// check if JSON body is too large, if it is, don't deserialize
125126
if (request->contentLength() > _maxContentLength) {
126-
#ifdef ESP32
127-
log_e("Content length exceeds maximum allowed");
128-
#endif
127+
async_ws_log_e("Content length exceeds maximum allowed");
129128
request->send(413);
130129
return;
131130
}
@@ -172,9 +171,7 @@ void AsyncCallbackJsonWebHandler::handleBody(AsyncWebServerRequest *request, uin
172171
if (request->_tempObject == NULL) {
173172
request->_tempObject = calloc(total + 1, sizeof(uint8_t)); // null-terminated string
174173
if (request->_tempObject == NULL) {
175-
#ifdef ESP32
176-
log_e("Failed to allocate");
177-
#endif
174+
async_ws_log_e("Failed to allocate");
178175
request->abort();
179176
return;
180177
}

src/AsyncMessagePack.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
33

44
#include "AsyncMessagePack.h"
5+
#include "AsyncWebServerLogging.h"
56

67
#if ASYNC_MSG_PACK_SUPPORT == 1
78

@@ -103,9 +104,7 @@ void AsyncCallbackMessagePackWebHandler::handleBody(AsyncWebServerRequest *reque
103104
if (total > 0 && request->_tempObject == NULL && total < _maxContentLength) {
104105
request->_tempObject = malloc(total);
105106
if (request->_tempObject == NULL) {
106-
#ifdef ESP32
107-
log_e("Failed to allocate");
108-
#endif
107+
async_ws_log_e("Failed to allocate");
109108
request->abort();
110109
return;
111110
}

src/AsyncWebServerLogging.h

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
3+
4+
#pragma once
5+
6+
#ifdef ASYNCWEBSERVER_LOG_CUSTOM
7+
8+
// The user must provide the following macros in AsyncWebServerLoggingCustom.h:
9+
// async_ws_log_e, async_ws_log_w, async_ws_log_i, async_ws_log_d, async_ws_log_v
10+
#include <AsyncWebServerLoggingCustom.h>
11+
12+
#else // ASYNCWEBSERVER_LOG_CUSTOM
13+
14+
/**
15+
* LibreTiny specific configurations
16+
*/
17+
#if defined(LIBRETINY)
18+
#include <Arduino.h>
19+
#define async_ws_log_e(format, ...) log_e(format, ##__VA_ARGS__)
20+
#define async_ws_log_w(format, ...) log_w(format, ##__VA_ARGS__)
21+
#define async_ws_log_i(format, ...) log_i(format, ##__VA_ARGS__)
22+
#define async_ws_log_d(format, ...) log_d(format, ##__VA_ARGS__)
23+
#define async_ws_log_v(format, ...) log_v(format, ##__VA_ARGS__)
24+
25+
/**
26+
* Raspberry Pi Pico specific configurations
27+
*/
28+
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
29+
#include <HardwareSerial.h>
30+
// define log levels
31+
#define ASYNC_WS_LOG_NONE 0 /*!< No log output */
32+
#define ASYNC_WS_LOG_ERROR 1 /*!< Critical errors, software module can not recover on its own */
33+
#define ASYNC_WS_LOG_WARN 2 /*!< Error conditions from which recovery measures have been taken */
34+
#define ASYNC_WS_LOG_INFO 3 /*!< Information messages which describe normal flow of events */
35+
#define ASYNC_WS_LOG_DEBUG 4 /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
36+
#define ASYNC_WS_LOG_VERBOSE 5 /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
37+
#define ASYNC_WS_LOG_MAX 6 /*!< Number of levels supported */
38+
// set default log level
39+
#ifndef ASYNCWEBSERVER_LOG_LEVEL
40+
#define ASYNCWEBSERVER_LOG_LEVEL ASYNC_WS_LOG_WARN
41+
#endif
42+
// error
43+
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_ERROR
44+
#define async_ws_log_e(format, ...) Serial.printf("E async_ws: " format, ##__VA_ARGS__);
45+
#else
46+
#define async_ws_log_e(format, ...)
47+
#endif
48+
// warn
49+
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_WARN
50+
#define async_ws_log_w(format, ...) Serial.printf("W async_ws: " format, ##__VA_ARGS__);
51+
#else
52+
#define async_ws_log_w(format, ...)
53+
#endif
54+
// info
55+
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_INFO
56+
#define async_ws_log_i(format, ...) Serial.printf("I async_ws: " format, ##__VA_ARGS__);
57+
#else
58+
#define async_ws_log_i(format, ...)
59+
#endif
60+
// debug
61+
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_DEBUG
62+
#define async_ws_log_d(format, ...) Serial.printf("D async_ws: " format, ##__VA_ARGS__);
63+
#else
64+
#define async_ws_log_d(format, ...)
65+
#endif
66+
// verbose
67+
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_VERBOSE
68+
#define async_ws_log_v(format, ...) Serial.printf("V async_ws: " format, ##__VA_ARGS__);
69+
#else
70+
#define async_ws_log_v(format, ...)
71+
#endif
72+
73+
/**
74+
* ESP8266 specific configurations
75+
*/
76+
#elif defined(ESP8266)
77+
#include <ets_sys.h>
78+
// define log levels
79+
#define ASYNC_WS_LOG_NONE 0 /*!< No log output */
80+
#define ASYNC_WS_LOG_ERROR 1 /*!< Critical errors, software module can not recover on its own */
81+
#define ASYNC_WS_LOG_WARN 2 /*!< Error conditions from which recovery measures have been taken */
82+
#define ASYNC_WS_LOG_INFO 3 /*!< Information messages which describe normal flow of events */
83+
#define ASYNC_WS_LOG_DEBUG 4 /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
84+
#define ASYNC_WS_LOG_VERBOSE 5 /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
85+
#define ASYNC_WS_LOG_MAX 6 /*!< Number of levels supported */
86+
// set default log level
87+
#ifndef ASYNCWEBSERVER_LOG_LEVEL
88+
#define ASYNCWEBSERVER_LOG_LEVEL ASYNC_WS_LOG_WARN
89+
#endif
90+
// error
91+
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_ERROR
92+
#define async_ws_log_e(format, ...) ets_printf(String(F("E async_ws: " format)).c_str(), ##__VA_ARGS__);
93+
#else
94+
#define async_ws_log_e(format, ...)
95+
#endif
96+
// warn
97+
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_WARN
98+
#define async_ws_log_w(format, ...) ets_printf(String(F("W async_ws: " format)).c_str(), ##__VA_ARGS__);
99+
#else
100+
#define async_ws_log_w(format, ...)
101+
#endif
102+
// info
103+
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_INFO
104+
#define async_ws_log_i(format, ...) ets_printf(String(F("I async_ws: " format)).c_str(), ##__VA_ARGS__);
105+
#else
106+
#define async_ws_log_i(format, ...)
107+
#endif
108+
// debug
109+
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_DEBUG
110+
#define async_ws_log_d(format, ...) ets_printf(String(F("D async_ws: " format)).c_str(), ##__VA_ARGS__);
111+
#else
112+
#define async_ws_log_d(format, ...)
113+
#endif
114+
// verbose
115+
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_VERBOSE
116+
#define async_ws_log_v(format, ...) ets_printf(String(F("V async_ws: " format)).c_str(), ##__VA_ARGS__);
117+
#else
118+
#define async_ws_log_v(format, ...)
119+
#endif
120+
121+
/**
122+
* Arduino specific configurations
123+
*/
124+
#elif defined(ARDUINO)
125+
#if defined(USE_ESP_IDF_LOG)
126+
#include <esp_log.h>
127+
#define async_ws_log_e(format, ...) ESP_LOGE("async_ws", format, ##__VA_ARGS__)
128+
#define async_ws_log_w(format, ...) ESP_LOGW("async_ws", format, ##__VA_ARGS__)
129+
#define async_ws_log_i(format, ...) ESP_LOGI("async_ws", format, ##__VA_ARGS__)
130+
#define async_ws_log_d(format, ...) ESP_LOGD("async_ws", format, ##__VA_ARGS__)
131+
#define async_ws_log_v(format, ...) ESP_LOGV("async_ws", format, ##__VA_ARGS__)
132+
#else
133+
#include <esp32-hal-log.h>
134+
#define async_ws_log_e(format, ...) log_e(format, ##__VA_ARGS__)
135+
#define async_ws_log_w(format, ...) log_w(format, ##__VA_ARGS__)
136+
#define async_ws_log_i(format, ...) log_i(format, ##__VA_ARGS__)
137+
#define async_ws_log_d(format, ...) log_d(format, ##__VA_ARGS__)
138+
#define async_ws_log_v(format, ...) log_v(format, ##__VA_ARGS__)
139+
#endif // USE_ESP_IDF_LOG
140+
141+
/**
142+
* ESP-IDF specific configurations
143+
*/
144+
#else
145+
#include <esp_log.h>
146+
#define async_ws_log_e(format, ...) ESP_LOGE("async_ws", format, ##__VA_ARGS__)
147+
#define async_ws_log_w(format, ...) ESP_LOGW("async_ws", format, ##__VA_ARGS__)
148+
#define async_ws_log_i(format, ...) ESP_LOGI("async_ws", format, ##__VA_ARGS__)
149+
#define async_ws_log_d(format, ...) ESP_LOGD("async_ws", format, ##__VA_ARGS__)
150+
#define async_ws_log_v(format, ...) ESP_LOGV("async_ws", format, ##__VA_ARGS__)
151+
#endif // !LIBRETINY && !ARDUINO
152+
153+
#endif // ASYNCWEBSERVER_LOG_CUSTOM

0 commit comments

Comments
 (0)