Skip to content

Commit 4d36225

Browse files
Fix ESP8266 logging to not require global Serial object (#380)
* Fix ESP8266 logging to not require global Serial object * ci(pre-commit): Apply automatic fixes * use sizeof --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent bf0869b commit 4d36225

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/AsyncWebServerLogging.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@
8181

8282
/**
8383
* ESP8266 specific configurations
84+
* Uses ets_printf to avoid dependency on global Serial object.
85+
* Format strings are stored in PROGMEM and copied to a stack buffer.
8486
*/
8587
#elif defined(ESP8266)
8688
#include <ets_sys.h>
89+
#include <pgmspace.h>
8790
// define log levels
8891
#define ASYNC_WS_LOG_NONE 0 /*!< No log output */
8992
#define ASYNC_WS_LOG_ERROR 1 /*!< Critical errors, software module can not recover on its own */
@@ -96,33 +99,42 @@
9699
#ifndef ASYNCWEBSERVER_LOG_LEVEL
97100
#define ASYNCWEBSERVER_LOG_LEVEL ASYNC_WS_LOG_INFO
98101
#endif
102+
// helper macro to copy PROGMEM format string to stack and call ets_printf
103+
// level is a char literal ('E', 'W', etc.) to avoid RAM usage from string literals
104+
#define _ASYNC_WS_LOG(level, format, ...) \
105+
do { \
106+
static const char __fmt[] PROGMEM = "%c async_ws %d: " format "\n"; \
107+
char __buf[sizeof(__fmt)]; \
108+
strcpy_P(__buf, __fmt); \
109+
ets_printf(__buf, level, __LINE__, ##__VA_ARGS__); \
110+
} while (0)
99111
// error
100112
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_ERROR
101-
#define async_ws_log_e(format, ...) Serial.printf_P(PSTR("E async_ws %d: " format "\n"), __LINE__, ##__VA_ARGS__)
113+
#define async_ws_log_e(format, ...) _ASYNC_WS_LOG('E', format, ##__VA_ARGS__)
102114
#else
103115
#define async_ws_log_e(format, ...)
104116
#endif
105117
// warn
106118
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_WARN
107-
#define async_ws_log_w(format, ...) Serial.printf_P(PSTR("W async_ws %d: " format "\n"), __LINE__, ##__VA_ARGS__)
119+
#define async_ws_log_w(format, ...) _ASYNC_WS_LOG('W', format, ##__VA_ARGS__)
108120
#else
109121
#define async_ws_log_w(format, ...)
110122
#endif
111123
// info
112124
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_INFO
113-
#define async_ws_log_i(format, ...) Serial.printf_P(PSTR("I async_ws %d: " format "\n"), __LINE__, ##__VA_ARGS__)
125+
#define async_ws_log_i(format, ...) _ASYNC_WS_LOG('I', format, ##__VA_ARGS__)
114126
#else
115127
#define async_ws_log_i(format, ...)
116128
#endif
117129
// debug
118130
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_DEBUG
119-
#define async_ws_log_d(format, ...) Serial.printf_P(PSTR("D async_ws %d: " format "\n"), __LINE__, ##__VA_ARGS__)
131+
#define async_ws_log_d(format, ...) _ASYNC_WS_LOG('D', format, ##__VA_ARGS__)
120132
#else
121133
#define async_ws_log_d(format, ...)
122134
#endif
123135
// verbose
124136
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_VERBOSE
125-
#define async_ws_log_v(format, ...) Serial.printf_P(PSTR("V async_ws %d: " format "\n"), __LINE__, ##__VA_ARGS__)
137+
#define async_ws_log_v(format, ...) _ASYNC_WS_LOG('V', format, ##__VA_ARGS__)
126138
#else
127139
#define async_ws_log_v(format, ...)
128140
#endif

0 commit comments

Comments
 (0)