esp_log.h 25 KB

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