Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
// SPDX-License-Identifier: LGPL-3.0-or-later
2
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
3
 
4
#pragma once
5
 
6
#ifdef ASYNCWEBSERVER_LOG_CUSTOM
7
// The user must provide the following macros in AsyncWebServerLoggingCustom.h:
8
//   async_ws_log_e, async_ws_log_w, async_ws_log_i, async_ws_log_d, async_ws_log_v
9
#include <AsyncWebServerLoggingCustom.h>
10
 
11
#elif defined(ASYNCWEBSERVER_LOG_DEBUG)
12
// Local Debug logging
13
#include <HardwareSerial.h>
14
#define async_ws_log_e(format, ...) Serial.printf("E async_ws %s() %d: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
15
#define async_ws_log_w(format, ...) Serial.printf("W async_ws %s() %d: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
16
#define async_ws_log_i(format, ...) Serial.printf("I async_ws %s() %d: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
17
#define async_ws_log_d(format, ...) Serial.printf("D async_ws %s() %d: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
18
#define async_ws_log_v(format, ...) Serial.printf("V async_ws %s() %d: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
19
 
20
#else
21
// Framework-based logging
22
 
23
/**
24
 * LibreTiny specific configurations
25
 */
26
#if defined(LIBRETINY)
27
#include <Arduino.h>
28
#define async_ws_log_e(format, ...) log_e(format, ##__VA_ARGS__)
29
#define async_ws_log_w(format, ...) log_w(format, ##__VA_ARGS__)
30
#define async_ws_log_i(format, ...) log_i(format, ##__VA_ARGS__)
31
#define async_ws_log_d(format, ...) log_d(format, ##__VA_ARGS__)
32
#define async_ws_log_v(format, ...) log_v(format, ##__VA_ARGS__)
33
 
34
#elif defined(HOST)
35
#include <cstdio>
36
// Arduino-Emulator has Serial, but it does not have Serial.printf, which
37
// is not a member of the Arduino Core API Print class.
38
 
39
// define log levels
40
#define ASYNC_WS_LOG_NONE    0 /*!< No log output */
41
#define ASYNC_WS_LOG_ERROR   1 /*!< Critical errors, software module can not recover on its own */
42
#define ASYNC_WS_LOG_WARN    2 /*!< Error conditions from which recovery measures have been taken */
43
#define ASYNC_WS_LOG_INFO    3 /*!< Information messages which describe normal flow of events */
44
#define ASYNC_WS_LOG_DEBUG   4 /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
45
#define ASYNC_WS_LOG_VERBOSE 5 /*!< Verbose information for debugging purposes */
46
#define ASYNC_WS_LOG_MAX     6 /*!< Number of levels supported */
47
// set default log level
48
#ifndef ASYNCWEBSERVER_LOG_LEVEL
49
#define ASYNCWEBSERVER_LOG_LEVEL ASYNC_WS_LOG_INFO
50
#endif
51
// error
52
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_ERROR
53
#define async_ws_log_e(format, ...)                                               \
54
  ::printf("E async_ws %s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
55
  ::putchar('\n');
56
#else
57
#define async_ws_log_e(format, ...)
58
#endif
59
// warn
60
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_WARN
61
#define async_ws_log_w(format, ...)                                               \
62
  ::printf("W async_ws %s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
63
  ::putchar('\n');
64
#else
65
#define async_ws_log_w(format, ...)
66
#endif
67
// info
68
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_INFO
69
#define async_ws_log_i(format, ...)                                               \
70
  ::printf("I async_ws %s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
71
  ::putchar('\n');
72
#else
73
#define async_ws_log_i(format, ...)
74
#endif
75
// debug
76
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_DEBUG
77
#define async_ws_log_d(format, ...)                                               \
78
  ::printf("D async_ws %s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
79
  ::putchar('\n');
80
#else
81
#define async_ws_log_d(format, ...)
82
#endif
83
// verbose
84
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_VERBOSE
85
#define async_ws_log_v(format, ...)                                               \
86
  ::printf("V async_ws %s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
87
  ::putchar('\n');
88
#else
89
#define async_ws_log_v(format, ...)
90
#endif
91
 
92
/**
93
 * Raspberry Pi Pico specific configurations
94
 */
95
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
96
#include <HardwareSerial.h>
97
// define log levels
98
#define ASYNC_WS_LOG_NONE    0 /*!< No log output */
99
#define ASYNC_WS_LOG_ERROR   1 /*!< Critical errors, software module can not recover on its own */
100
#define ASYNC_WS_LOG_WARN    2 /*!< Error conditions from which recovery measures have been taken */
101
#define ASYNC_WS_LOG_INFO    3 /*!< Information messages which describe normal flow of events */
102
#define ASYNC_WS_LOG_DEBUG   4 /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
103
#define ASYNC_WS_LOG_VERBOSE 5 /*!< Verbose information for debugging purposes */
104
#define ASYNC_WS_LOG_MAX     6 /*!< Number of levels supported */
105
// set default log level
106
#ifndef ASYNCWEBSERVER_LOG_LEVEL
107
#define ASYNCWEBSERVER_LOG_LEVEL ASYNC_WS_LOG_INFO
108
#endif
109
// error
110
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_ERROR
111
#define async_ws_log_e(format, ...) Serial.printf("E async_ws %s() %d: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
112
#else
113
#define async_ws_log_e(format, ...)
114
#endif
115
// warn
116
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_WARN
117
#define async_ws_log_w(format, ...) Serial.printf("W async_ws %s() %d: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
118
#else
119
#define async_ws_log_w(format, ...)
120
#endif
121
// info
122
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_INFO
123
#define async_ws_log_i(format, ...) Serial.printf("I async_ws %s() %d: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
124
#else
125
#define async_ws_log_i(format, ...)
126
#endif
127
// debug
128
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_DEBUG
129
#define async_ws_log_d(format, ...) Serial.printf("D async_ws %s() %d: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
130
#else
131
#define async_ws_log_d(format, ...)
132
#endif
133
// verbose
134
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_VERBOSE
135
#define async_ws_log_v(format, ...) Serial.printf("V async_ws %s() %d: " format "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
136
#else
137
#define async_ws_log_v(format, ...)
138
#endif
139
 
140
/**
141
 * ESP8266 specific configurations
142
 * Uses ets_printf to avoid dependency on global Serial object.
143
 * Format strings are stored in PROGMEM and copied to a stack buffer.
144
 */
145
#elif defined(ESP8266)
146
#include <ets_sys.h>
147
#include <pgmspace.h>
148
// define log levels
149
#define ASYNC_WS_LOG_NONE    0 /*!< No log output */
150
#define ASYNC_WS_LOG_ERROR   1 /*!< Critical errors, software module can not recover on its own */
151
#define ASYNC_WS_LOG_WARN    2 /*!< Error conditions from which recovery measures have been taken */
152
#define ASYNC_WS_LOG_INFO    3 /*!< Information messages which describe normal flow of events */
153
#define ASYNC_WS_LOG_DEBUG   4 /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
154
#define ASYNC_WS_LOG_VERBOSE 5 /*!< Verbose information for debugging purposes */
155
#define ASYNC_WS_LOG_MAX     6 /*!< Number of levels supported */
156
// set default log level
157
#ifndef ASYNCWEBSERVER_LOG_LEVEL
158
#define ASYNCWEBSERVER_LOG_LEVEL ASYNC_WS_LOG_INFO
159
#endif
160
// helper macro to copy PROGMEM format string to stack and call ets_printf
161
// level is a char literal ('E', 'W', etc.) to avoid RAM usage from string literals
162
#define _ASYNC_WS_LOG(level, format, ...)                               \
163
  do {                                                                  \
164
    static const char __fmt[] PROGMEM = "%c async_ws %d: " format "\n"; \
165
    char __buf[sizeof(__fmt)];                                          \
166
    strcpy_P(__buf, __fmt);                                             \
167
    ets_printf(__buf, level, __LINE__, ##__VA_ARGS__);                  \
168
  } while (0)
169
// error
170
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_ERROR
171
#define async_ws_log_e(format, ...) _ASYNC_WS_LOG('E', format, ##__VA_ARGS__)
172
#else
173
#define async_ws_log_e(format, ...)
174
#endif
175
// warn
176
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_WARN
177
#define async_ws_log_w(format, ...) _ASYNC_WS_LOG('W', format, ##__VA_ARGS__)
178
#else
179
#define async_ws_log_w(format, ...)
180
#endif
181
// info
182
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_INFO
183
#define async_ws_log_i(format, ...) _ASYNC_WS_LOG('I', format, ##__VA_ARGS__)
184
#else
185
#define async_ws_log_i(format, ...)
186
#endif
187
// debug
188
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_DEBUG
189
#define async_ws_log_d(format, ...) _ASYNC_WS_LOG('D', format, ##__VA_ARGS__)
190
#else
191
#define async_ws_log_d(format, ...)
192
#endif
193
// verbose
194
#if ASYNCWEBSERVER_LOG_LEVEL >= ASYNC_WS_LOG_VERBOSE
195
#define async_ws_log_v(format, ...) _ASYNC_WS_LOG('V', format, ##__VA_ARGS__)
196
#else
197
#define async_ws_log_v(format, ...)
198
#endif
199
 
200
/**
201
 * Arduino specific configurations
202
 */
203
#elif defined(ARDUINO)
204
#if defined(USE_ESP_IDF_LOG)
205
#include <esp_log.h>
206
#define async_ws_log_e(format, ...) ESP_LOGE("async_ws", "%s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
207
#define async_ws_log_w(format, ...) ESP_LOGW("async_ws", "%s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
208
#define async_ws_log_i(format, ...) ESP_LOGI("async_ws", "%s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
209
#define async_ws_log_d(format, ...) ESP_LOGD("async_ws", "%s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
210
#define async_ws_log_v(format, ...) ESP_LOGV("async_ws", "%s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
211
 
212
#else
213
#include <esp32-hal-log.h>
214
#define async_ws_log_e(format, ...) log_e(format, ##__VA_ARGS__)
215
#define async_ws_log_w(format, ...) log_w(format, ##__VA_ARGS__)
216
#define async_ws_log_i(format, ...) log_i(format, ##__VA_ARGS__)
217
#define async_ws_log_d(format, ...) log_d(format, ##__VA_ARGS__)
218
#define async_ws_log_v(format, ...) log_v(format, ##__VA_ARGS__)
219
#endif  // USE_ESP_IDF_LOG
220
 
221
/**
222
 * ESP-IDF specific configurations
223
 */
224
#else
225
#include <esp_log.h>
226
#define async_ws_log_e(format, ...) ESP_LOGE("async_ws", "%s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
227
#define async_ws_log_w(format, ...) ESP_LOGW("async_ws", "%s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
228
#define async_ws_log_i(format, ...) ESP_LOGI("async_ws", "%s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
229
#define async_ws_log_d(format, ...) ESP_LOGD("async_ws", "%s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
230
#define async_ws_log_v(format, ...) ESP_LOGV("async_ws", "%s() %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
231
#endif  // !LIBRETINY && !ARDUINO
232
 
233
#endif  // ASYNCWEBSERVER_LOG_CUSTOM