Skip to content

Commit b3683d5

Browse files
committed
Fix ESP8266 logging to not require global Serial object
1 parent bf0869b commit b3683d5

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

src/AsyncWebServerLogging.h

Lines changed: 18 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,43 @@
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[64]; \
108+
strncpy_P(__buf, __fmt, sizeof(__buf) - 1); \
109+
__buf[sizeof(__buf) - 1] = '\0'; \
110+
ets_printf(__buf, level, __LINE__, ##__VA_ARGS__); \
111+
} while (0)
99112
// error
100113
#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__)
114+
#define async_ws_log_e(format, ...) _ASYNC_WS_LOG('E', format, ##__VA_ARGS__)
102115
#else
103116
#define async_ws_log_e(format, ...)
104117
#endif
105118
// warn
106119
#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__)
120+
#define async_ws_log_w(format, ...) _ASYNC_WS_LOG('W', format, ##__VA_ARGS__)
108121
#else
109122
#define async_ws_log_w(format, ...)
110123
#endif
111124
// info
112125
#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__)
126+
#define async_ws_log_i(format, ...) _ASYNC_WS_LOG('I', format, ##__VA_ARGS__)
114127
#else
115128
#define async_ws_log_i(format, ...)
116129
#endif
117130
// debug
118131
#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__)
132+
#define async_ws_log_d(format, ...) _ASYNC_WS_LOG('D', format, ##__VA_ARGS__)
120133
#else
121134
#define async_ws_log_d(format, ...)
122135
#endif
123136
// verbose
124137
#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__)
138+
#define async_ws_log_v(format, ...) _ASYNC_WS_LOG('V', format, ##__VA_ARGS__)
126139
#else
127140
#define async_ws_log_v(format, ...)
128141
#endif

0 commit comments

Comments
 (0)