esp_log.h 26 KB

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