|
81 | 81 |
|
82 | 82 | /** |
83 | 83 | * 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. |
84 | 86 | */ |
85 | 87 | #elif defined(ESP8266) |
86 | 88 | #include <ets_sys.h> |
| 89 | +#include <pgmspace.h> |
87 | 90 | // define log levels |
88 | 91 | #define ASYNC_WS_LOG_NONE 0 /*!< No log output */ |
89 | 92 | #define ASYNC_WS_LOG_ERROR 1 /*!< Critical errors, software module can not recover on its own */ |
|
96 | 99 | #ifndef ASYNCWEBSERVER_LOG_LEVEL |
97 | 100 | #define ASYNCWEBSERVER_LOG_LEVEL ASYNC_WS_LOG_INFO |
98 | 101 | #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) |
99 | 111 | // error |
100 | 112 | #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__) |
102 | 114 | #else |
103 | 115 | #define async_ws_log_e(format, ...) |
104 | 116 | #endif |
105 | 117 | // warn |
106 | 118 | #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__) |
108 | 120 | #else |
109 | 121 | #define async_ws_log_w(format, ...) |
110 | 122 | #endif |
111 | 123 | // info |
112 | 124 | #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__) |
114 | 126 | #else |
115 | 127 | #define async_ws_log_i(format, ...) |
116 | 128 | #endif |
117 | 129 | // debug |
118 | 130 | #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__) |
120 | 132 | #else |
121 | 133 | #define async_ws_log_d(format, ...) |
122 | 134 | #endif |
123 | 135 | // verbose |
124 | 136 | #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__) |
126 | 138 | #else |
127 | 139 | #define async_ws_log_v(format, ...) |
128 | 140 | #endif |
|
0 commit comments