Skip to content

Commit f10b5ca

Browse files
committed
Logging refactoring to supports macros redefinition and -D USE_ESP_IFG_LOG
1 parent af50210 commit f10b5ca

13 files changed

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

0 commit comments

Comments
 (0)