1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __ESP_LOG_H__
8 #define __ESP_LOG_H__
9 
10 #include <stdint.h>
11 #include <stdarg.h>
12 #include <inttypes.h>
13 #include "sdkconfig.h"
14 #include "esp_rom_sys.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /**
21  * @brief Log level
22  *
23  */
24 typedef enum {
25     ESP_LOG_NONE,       /*!< No log output */
26     ESP_LOG_ERROR,      /*!< Critical errors, software module can not recover on its own */
27     ESP_LOG_WARN,       /*!< Error conditions from which recovery measures have been taken */
28     ESP_LOG_INFO,       /*!< Information messages which describe normal flow of events */
29     ESP_LOG_DEBUG,      /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
30     ESP_LOG_VERBOSE     /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
31 } esp_log_level_t;
32 
33 typedef int (*vprintf_like_t)(const char *, va_list);
34 
35 /**
36  * @brief Default log level
37  *
38  * This is used by the definition of ESP_EARLY_LOGx macros. It is not
39  * recommended to set this directly, call esp_log_level_set("*", level)
40  * instead.
41  */
42 extern esp_log_level_t esp_log_default_level;
43 
44 /**
45  * @brief Set log level for given tag
46  *
47  * If logging for given component has already been enabled, changes previous setting.
48  *
49  * @note Note that this function can not raise log level above the level set using
50  * CONFIG_LOG_MAXIMUM_LEVEL setting in menuconfig.
51  * To raise log level above the default one for a given file, define
52  * LOG_LOCAL_LEVEL to one of the ESP_LOG_* values, before including
53  * esp_log.h in this file.
54  *
55  * @param tag Tag of the log entries to enable. Must be a non-NULL zero terminated string.
56  *            Value "*" resets log level for all tags to the given value.
57  *
58  * @param level  Selects log level to enable. Only logs at this and lower verbosity
59  * levels will be shown.
60  */
61 void esp_log_level_set(const char* tag, esp_log_level_t level);
62 
63 /**
64  * @brief Get log level for a given tag, can be used to avoid expensive log statements
65  *
66  * @param tag Tag of the log to query current level. Must be a non-NULL zero terminated
67  *            string.
68  *
69  * @return The current log level for the given tag
70  */
71 esp_log_level_t esp_log_level_get(const char* tag);
72 
73 /**
74  * @brief Set function used to output log entries
75  *
76  * By default, log output goes to UART0. This function can be used to redirect log
77  * output to some other destination, such as file or network. Returns the original
78  * log handler, which may be necessary to return output to the previous destination.
79  *
80  * @note Please note that function callback here must be re-entrant as it can be
81  * invoked in parallel from multiple thread context.
82  *
83  * @param func new Function used for output. Must have same signature as vprintf.
84  *
85  * @return func old Function used for output.
86  */
87 vprintf_like_t esp_log_set_vprintf(vprintf_like_t func);
88 
89 /**
90  * @brief Function which returns timestamp to be used in log output
91  *
92  * This function is used in expansion of ESP_LOGx macros.
93  * In the 2nd stage bootloader, and at early application startup stage
94  * this function uses CPU cycle counter as time source. Later when
95  * FreeRTOS scheduler start running, it switches to FreeRTOS tick count.
96  *
97  * For now, we ignore millisecond counter overflow.
98  *
99  * @return timestamp, in milliseconds
100  */
101 uint32_t esp_log_timestamp(void);
102 
103 /**
104  * @brief Function which returns system timestamp to be used in log output
105  *
106  * This function is used in expansion of ESP_LOGx macros to print
107  * the system time as "HH:MM:SS.sss". The system time is initialized to
108  * 0 on startup, this can be set to the correct time with an SNTP sync,
109  * or manually with standard POSIX time functions.
110  *
111  * Currently, this will not get used in logging from binary blobs
112  * (i.e. Wi-Fi & Bluetooth libraries), these will still print the RTOS tick time.
113  *
114  * @return timestamp, in "HH:MM:SS.sss"
115  */
116 char* esp_log_system_timestamp(void);
117 
118 /**
119  * @brief Function which returns timestamp to be used in log output
120  *
121  * This function uses HW cycle counter and does not depend on OS,
122  * so it can be safely used after application crash.
123  *
124  * @return timestamp, in milliseconds
125  */
126 uint32_t esp_log_early_timestamp(void);
127 
128 /**
129  * @brief Write message into the log
130  *
131  * This function is not intended to be used directly. Instead, use one of
132  * ESP_LOGE, ESP_LOGW, ESP_LOGI, ESP_LOGD, ESP_LOGV macros.
133  *
134  * This function or these macros should not be used from an interrupt.
135  */
136 void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4)));
137 
138 /**
139  * @brief Write message into the log, va_list variant
140  * @see esp_log_write()
141  *
142  * This function is provided to ease integration toward other logging framework,
143  * so that esp_log can be used as a log sink.
144  */
145 void esp_log_writev(esp_log_level_t level, const char* tag, const char* format, va_list args);
146 
147 /** @cond */
148 
149 #include "esp_log_internal.h"
150 
151 #ifndef LOG_LOCAL_LEVEL
152 #ifndef BOOTLOADER_BUILD
153 #define LOG_LOCAL_LEVEL  CONFIG_BOOTLOADER_LOG_LEVEL
154 #else
155 #define LOG_LOCAL_LEVEL  CONFIG_BOOTLOADER_LOG_LEVEL
156 #endif
157 #endif
158 
159 /** @endcond */
160 
161 /**
162  * @brief Log a buffer of hex bytes at specified level, separated into 16 bytes each line.
163  *
164  * @param  tag      description tag
165  * @param  buffer   Pointer to the buffer array
166  * @param  buff_len length of buffer in bytes
167  * @param  level    level of the log
168  *
169  */
170 #define ESP_LOG_BUFFER_HEX_LEVEL( tag, buffer, buff_len, level ) \
171     do {\
172         if ( LOG_LOCAL_LEVEL >= (level) ) { \
173             esp_log_buffer_hex_internal( tag, buffer, buff_len, level ); \
174         } \
175     } while(0)
176 
177 /**
178  * @brief Log a buffer of characters at specified level, separated into 16 bytes each line. Buffer should contain only printable characters.
179  *
180  * @param  tag      description tag
181  * @param  buffer   Pointer to the buffer array
182  * @param  buff_len length of buffer in bytes
183  * @param  level    level of the log
184  *
185  */
186 #define ESP_LOG_BUFFER_CHAR_LEVEL( tag, buffer, buff_len, level ) \
187     do {\
188         if ( LOG_LOCAL_LEVEL >= (level) ) { \
189             esp_log_buffer_char_internal( tag, buffer, buff_len, level ); \
190         } \
191     } while(0)
192 
193 /**
194  * @brief Dump a buffer to the log at specified level.
195  *
196  * The dump log shows just like the one below:
197  *
198  *      W (195) log_example: 0x3ffb4280   45 53 50 33 32 20 69 73  20 67 72 65 61 74 2c 20  |ESP32 is great, |
199  *      W (195) log_example: 0x3ffb4290   77 6f 72 6b 69 6e 67 20  61 6c 6f 6e 67 20 77 69  |working along wi|
200  *      W (205) log_example: 0x3ffb42a0   74 68 20 74 68 65 20 49  44 46 2e 00              |th the IDF..|
201  *
202  * It is highly recommended to use terminals with over 102 text width.
203  *
204  * @param tag description tag
205  * @param buffer Pointer to the buffer array
206  * @param buff_len length of buffer in bytes
207  * @param level level of the log
208  */
209 #define ESP_LOG_BUFFER_HEXDUMP( tag, buffer, buff_len, level ) \
210     do { \
211         if ( LOG_LOCAL_LEVEL >= (level) ) { \
212             esp_log_buffer_hexdump_internal( tag, buffer, buff_len, level); \
213         } \
214     } while(0)
215 
216 /**
217  * @brief Log a buffer of hex bytes at Info level
218  *
219  * @param  tag      description tag
220  * @param  buffer   Pointer to the buffer array
221  * @param  buff_len length of buffer in bytes
222  *
223  * @see ``esp_log_buffer_hex_level``
224  *
225  */
226 #define ESP_LOG_BUFFER_HEX(tag, buffer, buff_len) \
227     do { \
228         if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { \
229             ESP_LOG_BUFFER_HEX_LEVEL( tag, buffer, buff_len, ESP_LOG_INFO ); \
230         }\
231     } while(0)
232 
233 /**
234  * @brief Log a buffer of characters at Info level. Buffer should contain only printable characters.
235  *
236  * @param  tag      description tag
237  * @param  buffer   Pointer to the buffer array
238  * @param  buff_len length of buffer in bytes
239  *
240  * @see ``esp_log_buffer_char_level``
241  *
242  */
243 #define ESP_LOG_BUFFER_CHAR(tag, buffer, buff_len) \
244     do { \
245         if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { \
246             ESP_LOG_BUFFER_CHAR_LEVEL( tag, buffer, buff_len, ESP_LOG_INFO ); \
247         }\
248     } while(0)
249 
250 /** @cond */
251 
252 //to be back compatible
253 #define esp_log_buffer_hex      ESP_LOG_BUFFER_HEX
254 #define esp_log_buffer_char     ESP_LOG_BUFFER_CHAR
255 
256 
257 #if CONFIG_LOG_COLORS
258 #define LOG_COLOR_BLACK   "30"
259 #define LOG_COLOR_RED     "31"
260 #define LOG_COLOR_GREEN   "32"
261 #define LOG_COLOR_BROWN   "33"
262 #define LOG_COLOR_BLUE    "34"
263 #define LOG_COLOR_PURPLE  "35"
264 #define LOG_COLOR_CYAN    "36"
265 #define LOG_COLOR(COLOR)  "\033[0;" COLOR "m"
266 #define LOG_BOLD(COLOR)   "\033[1;" COLOR "m"
267 #define LOG_RESET_COLOR   "\033[0m"
268 #define LOG_COLOR_E       LOG_COLOR(LOG_COLOR_RED)
269 #define LOG_COLOR_W       LOG_COLOR(LOG_COLOR_BROWN)
270 #define LOG_COLOR_I       LOG_COLOR(LOG_COLOR_GREEN)
271 #define LOG_COLOR_D
272 #define LOG_COLOR_V
273 #else //CONFIG_LOG_COLORS
274 #define LOG_COLOR_E
275 #define LOG_COLOR_W
276 #define LOG_COLOR_I
277 #define LOG_COLOR_D
278 #define LOG_COLOR_V
279 #define LOG_RESET_COLOR
280 #endif //CONFIG_LOG_COLORS
281 
282 #define LOG_FORMAT(letter, format)  LOG_COLOR_ ## letter #letter " (%" PRIu32 ") %s: " format LOG_RESET_COLOR "\n"
283 #define LOG_SYSTEM_TIME_FORMAT(letter, format)  LOG_COLOR_ ## letter #letter " (%s) %s: " format LOG_RESET_COLOR "\n"
284 
285 /** @endcond */
286 
287 /// macro to output logs in startup code, before heap allocator and syscalls have been initialized.
288 /// Log at ``ESP_LOG_ERROR`` level. @see ``printf``,``ESP_LOGE``,``ESP_DRAM_LOGE``
289 
290 /**
291  * In the future, we want to become compatible with clang.
292  * Hence, we provide two versions of the following macros which are using variadic arguments.
293  * The first one is using the GNU extension \#\#__VA_ARGS__. The second one is using the C++20 feature __VA_OPT__(,).
294  * This allows users to compile their code with standard C++20 enabled instead of the GNU extension.
295  * Below C++20, we haven't found any good alternative to using \#\#__VA_ARGS__.
296  */
297 #if defined(__cplusplus) && (__cplusplus >  201703L)
298 #define ESP_EARLY_LOGE( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_ERROR,   E __VA_OPT__(,) __VA_ARGS__)
299 /// macro to output logs in startup code at ``ESP_LOG_WARN`` level.  @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
300 #define ESP_EARLY_LOGW( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_WARN,    W __VA_OPT__(,) __VA_ARGS__)
301 /// macro to output logs in startup code at ``ESP_LOG_INFO`` level.  @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
302 #define ESP_EARLY_LOGI( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_INFO,    I __VA_OPT__(,) __VA_ARGS__)
303 /// macro to output logs in startup code at ``ESP_LOG_DEBUG`` level.  @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
304 #define ESP_EARLY_LOGD( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_DEBUG,   D __VA_OPT__(,) __VA_ARGS__)
305 /// macro to output logs in startup code at ``ESP_LOG_VERBOSE`` level.  @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
306 #define ESP_EARLY_LOGV( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_VERBOSE, V __VA_OPT__(,) __VA_ARGS__)
307 #else // !(defined(__cplusplus) && (__cplusplus >  201703L))
308 #define ESP_EARLY_LOGE( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_ERROR,   E, ##__VA_ARGS__)
309 /// macro to output logs in startup code at ``ESP_LOG_WARN`` level.  @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
310 #define ESP_EARLY_LOGW( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_WARN,    W, ##__VA_ARGS__)
311 /// macro to output logs in startup code at ``ESP_LOG_INFO`` level.  @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
312 #define ESP_EARLY_LOGI( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_INFO,    I, ##__VA_ARGS__)
313 /// macro to output logs in startup code at ``ESP_LOG_DEBUG`` level.  @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
314 #define ESP_EARLY_LOGD( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_DEBUG,   D, ##__VA_ARGS__)
315 /// macro to output logs in startup code at ``ESP_LOG_VERBOSE`` level.  @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
316 #define ESP_EARLY_LOGV( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_VERBOSE, V, ##__VA_ARGS__)
317 #endif // !(defined(__cplusplus) && (__cplusplus >  201703L))
318 
319 #ifdef BOOTLOADER_BUILD
320 #define _ESP_LOG_EARLY_ENABLED(log_level) (LOG_LOCAL_LEVEL >= (log_level))
321 #else
322 /* For early log, there is no log tag filtering. So we want to log only if both the LOG_LOCAL_LEVEL and the
323    currently configured min log level are higher than the log level */
324 #define _ESP_LOG_EARLY_ENABLED(log_level) (LOG_LOCAL_LEVEL >= (log_level) && esp_log_default_level >= (log_level))
325 #endif
326 
327 #define ESP_LOG_EARLY_IMPL(tag, format, log_level, log_tag_letter, ...) do {                             \
328         if (_ESP_LOG_EARLY_ENABLED(log_level)) {                                                         \
329             esp_rom_printf(LOG_FORMAT(log_tag_letter, format), esp_log_timestamp(), tag, ##__VA_ARGS__); \
330         }} while(0)
331 
332 #ifndef BOOTLOADER_BUILD
333 #if defined(__cplusplus) && (__cplusplus >  201703L)
334 #define ESP_LOGE( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_ERROR,   tag, format __VA_OPT__(,) __VA_ARGS__)
335 #define ESP_LOGW( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_WARN,    tag, format __VA_OPT__(,) __VA_ARGS__)
336 #define ESP_LOGI( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_INFO,    tag, format __VA_OPT__(,) __VA_ARGS__)
337 #define ESP_LOGD( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_DEBUG,   tag, format __VA_OPT__(,) __VA_ARGS__)
338 #define ESP_LOGV( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_VERBOSE, tag, format __VA_OPT__(,) __VA_ARGS__)
339 #else // !(defined(__cplusplus) && (__cplusplus >  201703L))
340 #define ESP_LOGE( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_ERROR,   tag, format, ##__VA_ARGS__)
341 #define ESP_LOGW( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_WARN,    tag, format, ##__VA_ARGS__)
342 #define ESP_LOGI( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_INFO,    tag, format, ##__VA_ARGS__)
343 #define ESP_LOGD( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_DEBUG,   tag, format, ##__VA_ARGS__)
344 #define ESP_LOGV( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_VERBOSE, tag, format, ##__VA_ARGS__)
345 #endif // !(defined(__cplusplus) && (__cplusplus >  201703L))
346 #else
347 
348 /**
349  * Macro to output logs at ESP_LOG_ERROR level.
350  *
351  * @note This macro cannot be used when interrupts are disabled or inside an ISR. @see ``ESP_DRAM_LOGE``.
352  *
353  * @param tag tag of the log, which can be used to change the log level by ``esp_log_level_set`` at runtime.
354  *
355  * @see ``printf``
356  */
357 #if defined(__cplusplus) && (__cplusplus >  201703L)
358 #define ESP_LOGE( tag, format, ... )  ESP_EARLY_LOGE(tag, format __VA_OPT__(,) __VA_ARGS__)
359 /// macro to output logs at ``ESP_LOG_WARN`` level.  @see ``ESP_LOGE``
360 #define ESP_LOGW( tag, format, ... )  ESP_EARLY_LOGW(tag, format __VA_OPT__(,) __VA_ARGS__)
361 /// macro to output logs at ``ESP_LOG_INFO`` level.  @see ``ESP_LOGE``
362 #define ESP_LOGI( tag, format, ... )  ESP_EARLY_LOGI(tag, format __VA_OPT__(,) __VA_ARGS__)
363 /// macro to output logs at ``ESP_LOG_DEBUG`` level.  @see ``ESP_LOGE``
364 #define ESP_LOGD( tag, format, ... )  ESP_EARLY_LOGD(tag, format __VA_OPT__(,) __VA_ARGS__)
365 /// macro to output logs at ``ESP_LOG_VERBOSE`` level.  @see ``ESP_LOGE``
366 #define ESP_LOGV( tag, format, ... )  ESP_EARLY_LOGV(tag, format __VA_OPT__(,) __VA_ARGS__)
367 #else // !(defined(__cplusplus) && (__cplusplus >  201703L))
368 #define ESP_LOGE( tag, format, ... )  ESP_EARLY_LOGE(tag, format, ##__VA_ARGS__)
369 /// macro to output logs at ``ESP_LOG_WARN`` level.  @see ``ESP_LOGE``
370 #define ESP_LOGW( tag, format, ... )  ESP_EARLY_LOGW(tag, format, ##__VA_ARGS__)
371 /// macro to output logs at ``ESP_LOG_INFO`` level.  @see ``ESP_LOGE``
372 #define ESP_LOGI( tag, format, ... )  ESP_EARLY_LOGI(tag, format, ##__VA_ARGS__)
373 /// macro to output logs at ``ESP_LOG_DEBUG`` level.  @see ``ESP_LOGE``
374 #define ESP_LOGD( tag, format, ... )  ESP_EARLY_LOGD(tag, format, ##__VA_ARGS__)
375 /// macro to output logs at ``ESP_LOG_VERBOSE`` level.  @see ``ESP_LOGE``
376 #define ESP_LOGV( tag, format, ... )  ESP_EARLY_LOGV(tag, format, ##__VA_ARGS__)
377 #endif // !(defined(__cplusplus) && (__cplusplus >  201703L))
378 #endif  // BOOTLOADER_BUILD
379 
380 /** runtime macro to output logs at a specified level.
381  *
382  * @param tag tag of the log, which can be used to change the log level by ``esp_log_level_set`` at runtime.
383  * @param level level of the output log.
384  * @param format format of the output log. See ``printf``
385  * @param ... variables to be replaced into the log. See ``printf``
386  *
387  * @see ``printf``
388  */
389 #if defined(__cplusplus) && (__cplusplus >  201703L)
390 #if CONFIG_LOG_TIMESTAMP_SOURCE_RTOS
391 #define ESP_LOG_LEVEL(level, tag, format, ...) do {                     \
392         if (level==ESP_LOG_ERROR )          { esp_log_write(ESP_LOG_ERROR,      tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
393         else if (level==ESP_LOG_WARN )      { esp_log_write(ESP_LOG_WARN,       tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
394         else if (level==ESP_LOG_DEBUG )     { esp_log_write(ESP_LOG_DEBUG,      tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
395         else if (level==ESP_LOG_VERBOSE )   { esp_log_write(ESP_LOG_VERBOSE,    tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
396         else                                { esp_log_write(ESP_LOG_INFO,       tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
397     } while(0)
398 #elif CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM
399 #define ESP_LOG_LEVEL(level, tag, format, ...) do {                     \
400         if (level==ESP_LOG_ERROR )          { esp_log_write(ESP_LOG_ERROR,      tag, LOG_SYSTEM_TIME_FORMAT(E, format), esp_log_system_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
401         else if (level==ESP_LOG_WARN )      { esp_log_write(ESP_LOG_WARN,       tag, LOG_SYSTEM_TIME_FORMAT(W, format), esp_log_system_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
402         else if (level==ESP_LOG_DEBUG )     { esp_log_write(ESP_LOG_DEBUG,      tag, LOG_SYSTEM_TIME_FORMAT(D, format), esp_log_system_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
403         else if (level==ESP_LOG_VERBOSE )   { esp_log_write(ESP_LOG_VERBOSE,    tag, LOG_SYSTEM_TIME_FORMAT(V, format), esp_log_system_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
404         else                                { esp_log_write(ESP_LOG_INFO,       tag, LOG_SYSTEM_TIME_FORMAT(I, format), esp_log_system_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
405     } while(0)
406 #endif //CONFIG_LOG_TIMESTAMP_SOURCE_xxx
407 #else // !(defined(__cplusplus) && (__cplusplus >  201703L))
408 #if CONFIG_LOG_TIMESTAMP_SOURCE_RTOS
409 #define ESP_LOG_LEVEL(level, tag, format, ...) do {                     \
410         if (level==ESP_LOG_ERROR )          { esp_log_write(ESP_LOG_ERROR,      tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
411         else if (level==ESP_LOG_WARN )      { esp_log_write(ESP_LOG_WARN,       tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
412         else if (level==ESP_LOG_DEBUG )     { esp_log_write(ESP_LOG_DEBUG,      tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
413         else if (level==ESP_LOG_VERBOSE )   { esp_log_write(ESP_LOG_VERBOSE,    tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
414         else                                { esp_log_write(ESP_LOG_INFO,       tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
415     } while(0)
416 #elif CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM
417 #define ESP_LOG_LEVEL(level, tag, format, ...) do {                     \
418         if (level==ESP_LOG_ERROR )          { esp_log_write(ESP_LOG_ERROR,      tag, LOG_SYSTEM_TIME_FORMAT(E, format), esp_log_system_timestamp(), tag, ##__VA_ARGS__); } \
419         else if (level==ESP_LOG_WARN )      { esp_log_write(ESP_LOG_WARN,       tag, LOG_SYSTEM_TIME_FORMAT(W, format), esp_log_system_timestamp(), tag, ##__VA_ARGS__); } \
420         else if (level==ESP_LOG_DEBUG )     { esp_log_write(ESP_LOG_DEBUG,      tag, LOG_SYSTEM_TIME_FORMAT(D, format), esp_log_system_timestamp(), tag, ##__VA_ARGS__); } \
421         else if (level==ESP_LOG_VERBOSE )   { esp_log_write(ESP_LOG_VERBOSE,    tag, LOG_SYSTEM_TIME_FORMAT(V, format), esp_log_system_timestamp(), tag, ##__VA_ARGS__); } \
422         else                                { esp_log_write(ESP_LOG_INFO,       tag, LOG_SYSTEM_TIME_FORMAT(I, format), esp_log_system_timestamp(), tag, ##__VA_ARGS__); } \
423     } while(0)
424 #endif //CONFIG_LOG_TIMESTAMP_SOURCE_xxx
425 #endif // !(defined(__cplusplus) && (__cplusplus >  201703L))
426 
427 /** runtime macro to output logs at a specified level. Also check the level with ``LOG_LOCAL_LEVEL``.
428  *
429  * @see ``printf``, ``ESP_LOG_LEVEL``
430  */
431 #define ESP_LOG_LEVEL_LOCAL(level, tag, format, ...) do {               \
432         if ( LOG_LOCAL_LEVEL >= level ) ESP_LOG_LEVEL(level, tag, format, ##__VA_ARGS__); \
433     } while(0)
434 
435 
436 /**
437  * @brief Macro to output logs when the cache is disabled. Log at ``ESP_LOG_ERROR`` level.
438  *
439  * @note Unlike normal logging macros, it's possible to use this macro when interrupts are
440  * disabled or inside an ISR.
441  *
442  * Similar to @see ``ESP_EARLY_LOGE``, the log level cannot be changed per-tag, however
443  * esp_log_level_set("*", level) will set the default level which controls these log lines also.
444  *
445  * Usage: `ESP_DRAM_LOGE(DRAM_STR("my_tag"), "format", or `ESP_DRAM_LOGE(TAG, "format", ...)`,
446  * where TAG is a char* that points to a str in the DRAM.
447  *
448  * @note Placing log strings in DRAM reduces available DRAM, so only use when absolutely essential.
449  *
450  * @see ``esp_rom_printf``,``ESP_LOGE``
451  */
452 #if defined(__cplusplus) && (__cplusplus >  201703L)
453 #define ESP_DRAM_LOGE( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_ERROR,   E __VA_OPT__(,) __VA_ARGS__)
454 /// macro to output logs when the cache is disabled at ``ESP_LOG_WARN`` level.  @see ``ESP_DRAM_LOGW``,``ESP_LOGW``, ``esp_rom_printf``
455 #define ESP_DRAM_LOGW( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_WARN,    W __VA_OPT__(,) __VA_ARGS__)
456 /// macro to output logs when the cache is disabled at ``ESP_LOG_INFO`` level.  @see ``ESP_DRAM_LOGI``,``ESP_LOGI``, ``esp_rom_printf``
457 #define ESP_DRAM_LOGI( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_INFO,    I __VA_OPT__(,) __VA_ARGS__)
458 /// macro to output logs when the cache is disabled at ``ESP_LOG_DEBUG`` level.  @see ``ESP_DRAM_LOGD``,``ESP_LOGD``, ``esp_rom_printf``
459 #define ESP_DRAM_LOGD( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_DEBUG,   D __VA_OPT__(,) __VA_ARGS__)
460 /// macro to output logs when the cache is disabled at ``ESP_LOG_VERBOSE`` level.  @see ``ESP_DRAM_LOGV``,``ESP_LOGV``, ``esp_rom_printf``
461 #define ESP_DRAM_LOGV( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_VERBOSE, V __VA_OPT__(,) __VA_ARGS__)
462 #else // !(defined(__cplusplus) && (__cplusplus >  201703L))
463 #define ESP_DRAM_LOGE( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_ERROR,   E, ##__VA_ARGS__)
464 /// macro to output logs when the cache is disabled at ``ESP_LOG_WARN`` level.  @see ``ESP_DRAM_LOGW``,``ESP_LOGW``, ``esp_rom_printf``
465 #define ESP_DRAM_LOGW( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_WARN,    W, ##__VA_ARGS__)
466 /// macro to output logs when the cache is disabled at ``ESP_LOG_INFO`` level.  @see ``ESP_DRAM_LOGI``,``ESP_LOGI``, ``esp_rom_printf``
467 #define ESP_DRAM_LOGI( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_INFO,    I, ##__VA_ARGS__)
468 /// macro to output logs when the cache is disabled at ``ESP_LOG_DEBUG`` level.  @see ``ESP_DRAM_LOGD``,``ESP_LOGD``, ``esp_rom_printf``
469 #define ESP_DRAM_LOGD( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_DEBUG,   D, ##__VA_ARGS__)
470 /// macro to output logs when the cache is disabled at ``ESP_LOG_VERBOSE`` level.  @see ``ESP_DRAM_LOGV``,``ESP_LOGV``, ``esp_rom_printf``
471 #define ESP_DRAM_LOGV( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_VERBOSE, V, ##__VA_ARGS__)
472 #endif // !(defined(__cplusplus) && (__cplusplus >  201703L))
473 
474 /** @cond */
475 #define _ESP_LOG_DRAM_LOG_FORMAT(letter, format)  DRAM_STR(#letter " %s: " format "\n")
476 
477 #if defined(__cplusplus) && (__cplusplus >  201703L)
478 #define ESP_DRAM_LOG_IMPL(tag, format, log_level, log_tag_letter, ...) do {                                  \
479         if (_ESP_LOG_EARLY_ENABLED(log_level)) {                                                             \
480             esp_rom_printf(_ESP_LOG_DRAM_LOG_FORMAT(log_tag_letter, format), tag __VA_OPT__(,) __VA_ARGS__); \
481         }} while(0)
482 #else // !(defined(__cplusplus) && (__cplusplus >  201703L))
483 #define ESP_DRAM_LOG_IMPL(tag, format, log_level, log_tag_letter, ...) do {                       \
484         if (_ESP_LOG_EARLY_ENABLED(log_level)) {                                                  \
485             esp_rom_printf(_ESP_LOG_DRAM_LOG_FORMAT(log_tag_letter, format), tag, ##__VA_ARGS__); \
486         }} while(0)
487 #endif // !(defined(__cplusplus) && (__cplusplus >  201703L))
488 /** @endcond */
489 
490 #ifdef __cplusplus
491 }
492 #endif
493 
494 
495 #endif /* __ESP_LOG_H__ */
496