esp_log.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef __ESP_LOG_H__
  14. #define __ESP_LOG_H__
  15. #include <stdint.h>
  16. #include <stdarg.h>
  17. #include "esp_rom_sys.h"
  18. #include "sdkconfig.h"
  19. #if CONFIG_IDF_TARGET_ESP32
  20. #include "esp32/rom/ets_sys.h" // will be removed in idf v5.0
  21. #elif CONFIG_IDF_TARGET_ESP32S2
  22. #include "esp32s2/rom/ets_sys.h"
  23. #endif
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * @brief Log level
  29. *
  30. */
  31. typedef enum {
  32. ESP_LOG_NONE, /*!< No log output */
  33. ESP_LOG_ERROR, /*!< Critical errors, software module can not recover on its own */
  34. ESP_LOG_WARN, /*!< Error conditions from which recovery measures have been taken */
  35. ESP_LOG_INFO, /*!< Information messages which describe normal flow of events */
  36. ESP_LOG_DEBUG, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
  37. ESP_LOG_VERBOSE /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
  38. } esp_log_level_t;
  39. typedef int (*vprintf_like_t)(const char *, va_list);
  40. /**
  41. * @brief Set log level for given tag
  42. *
  43. * If logging for given component has already been enabled, changes previous setting.
  44. *
  45. * Note that this function can not raise log level above the level set using
  46. * CONFIG_LOG_DEFAULT_LEVEL setting in menuconfig.
  47. *
  48. * To raise log level above the default one for a given file, define
  49. * LOG_LOCAL_LEVEL to one of the ESP_LOG_* values, before including
  50. * esp_log.h in this file.
  51. *
  52. * @param tag Tag of the log entries to enable. Must be a non-NULL zero terminated string.
  53. * Value "*" resets log level for all tags to the given value.
  54. *
  55. * @param level Selects log level to enable. Only logs at this and lower verbosity
  56. * levels will be shown.
  57. */
  58. void esp_log_level_set(const char* tag, esp_log_level_t level);
  59. /**
  60. * @brief Set function used to output log entries
  61. *
  62. * By default, log output goes to UART0. This function can be used to redirect log
  63. * output to some other destination, such as file or network. Returns the original
  64. * log handler, which may be necessary to return output to the previous destination.
  65. *
  66. * @param func new Function used for output. Must have same signature as vprintf.
  67. *
  68. * @return func old Function used for output.
  69. */
  70. vprintf_like_t esp_log_set_vprintf(vprintf_like_t func);
  71. /**
  72. * @brief Function which returns timestamp to be used in log output
  73. *
  74. * This function is used in expansion of ESP_LOGx macros.
  75. * In the 2nd stage bootloader, and at early application startup stage
  76. * this function uses CPU cycle counter as time source. Later when
  77. * FreeRTOS scheduler start running, it switches to FreeRTOS tick count.
  78. *
  79. * For now, we ignore millisecond counter overflow.
  80. *
  81. * @return timestamp, in milliseconds
  82. */
  83. uint32_t esp_log_timestamp(void);
  84. /**
  85. * @brief Function which returns system timestamp to be used in log output
  86. *
  87. * This function is used in expansion of ESP_LOGx macros to print
  88. * the system time as "HH:MM:SS.sss". The system time is initialized to
  89. * 0 on startup, this can be set to the correct time with an SNTP sync,
  90. * or manually with standard POSIX time functions.
  91. *
  92. * Currently this will not get used in logging from binary blobs
  93. * (i.e WiFi & Bluetooth libraries), these will still print the RTOS tick time.
  94. *
  95. * @return timestamp, in "HH:MM:SS.sss"
  96. */
  97. char* esp_log_system_timestamp(void);
  98. /**
  99. * @brief Function which returns timestamp to be used in log output
  100. *
  101. * This function uses HW cycle counter and does not depend on OS,
  102. * so it can be safely used after application crash.
  103. *
  104. * @return timestamp, in milliseconds
  105. */
  106. uint32_t esp_log_early_timestamp(void);
  107. /**
  108. * @brief Write message into the log
  109. *
  110. * This function is not intended to be used directly. Instead, use one of
  111. * ESP_LOGE, ESP_LOGW, ESP_LOGI, ESP_LOGD, ESP_LOGV macros.
  112. *
  113. * This function or these macros should not be used from an interrupt.
  114. */
  115. void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4)));
  116. /**
  117. * @brief Write message into the log, va_list variant
  118. * @see esp_log_write()
  119. *
  120. * This function is provided to ease integration toward other logging framework,
  121. * so that esp_log can be used as a log sink.
  122. */
  123. void esp_log_writev(esp_log_level_t level, const char* tag, const char* format, va_list args);
  124. /** @cond */
  125. #include "esp_log_internal.h"
  126. #ifndef LOG_LOCAL_LEVEL
  127. #ifndef BOOTLOADER_BUILD
  128. #define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL
  129. #else
  130. #define LOG_LOCAL_LEVEL CONFIG_BOOTLOADER_LOG_LEVEL
  131. #endif
  132. #endif
  133. /** @endcond */
  134. /**
  135. * @brief Log a buffer of hex bytes at specified level, separated into 16 bytes each line.
  136. *
  137. * @param tag description tag
  138. * @param buffer Pointer to the buffer array
  139. * @param buff_len length of buffer in bytes
  140. * @param level level of the log
  141. *
  142. */
  143. #define ESP_LOG_BUFFER_HEX_LEVEL( tag, buffer, buff_len, level ) \
  144. do {\
  145. if ( LOG_LOCAL_LEVEL >= (level) ) { \
  146. esp_log_buffer_hex_internal( tag, buffer, buff_len, level ); \
  147. } \
  148. } while(0)
  149. /**
  150. * @brief Log a buffer of characters at specified level, separated into 16 bytes each line. Buffer should contain only printable characters.
  151. *
  152. * @param tag description tag
  153. * @param buffer Pointer to the buffer array
  154. * @param buff_len length of buffer in bytes
  155. * @param level level of the log
  156. *
  157. */
  158. #define ESP_LOG_BUFFER_CHAR_LEVEL( tag, buffer, buff_len, level ) \
  159. do {\
  160. if ( LOG_LOCAL_LEVEL >= (level) ) { \
  161. esp_log_buffer_char_internal( tag, buffer, buff_len, level ); \
  162. } \
  163. } while(0)
  164. /**
  165. * @brief Dump a buffer to the log at specified level.
  166. *
  167. * The dump log shows just like the one below:
  168. *
  169. * W (195) log_example: 0x3ffb4280 45 53 50 33 32 20 69 73 20 67 72 65 61 74 2c 20 |ESP32 is great, |
  170. * W (195) log_example: 0x3ffb4290 77 6f 72 6b 69 6e 67 20 61 6c 6f 6e 67 20 77 69 |working along wi|
  171. * W (205) log_example: 0x3ffb42a0 74 68 20 74 68 65 20 49 44 46 2e 00 |th the IDF..|
  172. *
  173. * It is highly recommend to use terminals with over 102 text width.
  174. *
  175. * @param tag description tag
  176. * @param buffer Pointer to the buffer array
  177. * @param buff_len length of buffer in bytes
  178. * @param level level of the log
  179. */
  180. #define ESP_LOG_BUFFER_HEXDUMP( tag, buffer, buff_len, level ) \
  181. do { \
  182. if ( LOG_LOCAL_LEVEL >= (level) ) { \
  183. esp_log_buffer_hexdump_internal( tag, buffer, buff_len, level); \
  184. } \
  185. } while(0)
  186. /**
  187. * @brief Log a buffer of hex bytes at Info level
  188. *
  189. * @param tag description tag
  190. * @param buffer Pointer to the buffer array
  191. * @param buff_len length of buffer in bytes
  192. *
  193. * @see ``esp_log_buffer_hex_level``
  194. *
  195. */
  196. #define ESP_LOG_BUFFER_HEX(tag, buffer, buff_len) \
  197. do { \
  198. if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { \
  199. ESP_LOG_BUFFER_HEX_LEVEL( tag, buffer, buff_len, ESP_LOG_INFO ); \
  200. }\
  201. } while(0)
  202. /**
  203. * @brief Log a buffer of characters at Info level. Buffer should contain only printable characters.
  204. *
  205. * @param tag description tag
  206. * @param buffer Pointer to the buffer array
  207. * @param buff_len length of buffer in bytes
  208. *
  209. * @see ``esp_log_buffer_char_level``
  210. *
  211. */
  212. #define ESP_LOG_BUFFER_CHAR(tag, buffer, buff_len) \
  213. do { \
  214. if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { \
  215. ESP_LOG_BUFFER_CHAR_LEVEL( tag, buffer, buff_len, ESP_LOG_INFO ); \
  216. }\
  217. } while(0)
  218. /** @cond */
  219. //to be back compatible
  220. #define esp_log_buffer_hex ESP_LOG_BUFFER_HEX
  221. #define esp_log_buffer_char ESP_LOG_BUFFER_CHAR
  222. #if CONFIG_LOG_COLORS
  223. #define LOG_COLOR_BLACK "30"
  224. #define LOG_COLOR_RED "31"
  225. #define LOG_COLOR_GREEN "32"
  226. #define LOG_COLOR_BROWN "33"
  227. #define LOG_COLOR_BLUE "34"
  228. #define LOG_COLOR_PURPLE "35"
  229. #define LOG_COLOR_CYAN "36"
  230. #define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
  231. #define LOG_BOLD(COLOR) "\033[1;" COLOR "m"
  232. #define LOG_RESET_COLOR "\033[0m"
  233. #define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
  234. #define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
  235. #define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
  236. #define LOG_COLOR_D
  237. #define LOG_COLOR_V
  238. #else //CONFIG_LOG_COLORS
  239. #define LOG_COLOR_E
  240. #define LOG_COLOR_W
  241. #define LOG_COLOR_I
  242. #define LOG_COLOR_D
  243. #define LOG_COLOR_V
  244. #define LOG_RESET_COLOR
  245. #endif //CONFIG_LOG_COLORS
  246. #define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%d) %s: " format LOG_RESET_COLOR "\n"
  247. #define LOG_SYSTEM_TIME_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%s) %s: " format LOG_RESET_COLOR "\n"
  248. /** @endcond */
  249. /// 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``
  250. #define ESP_EARLY_LOGE( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_ERROR, E, ##__VA_ARGS__)
  251. /// macro to output logs in startup code at ``ESP_LOG_WARN`` level. @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
  252. #define ESP_EARLY_LOGW( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_WARN, W, ##__VA_ARGS__)
  253. /// macro to output logs in startup code at ``ESP_LOG_INFO`` level. @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
  254. #define ESP_EARLY_LOGI( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_INFO, I, ##__VA_ARGS__)
  255. /// macro to output logs in startup code at ``ESP_LOG_DEBUG`` level. @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
  256. #define ESP_EARLY_LOGD( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_DEBUG, D, ##__VA_ARGS__)
  257. /// macro to output logs in startup code at ``ESP_LOG_VERBOSE`` level. @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
  258. #define ESP_EARLY_LOGV( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_VERBOSE, V, ##__VA_ARGS__)
  259. #define ESP_LOG_EARLY_IMPL(tag, format, log_level, log_tag_letter, ...) do { \
  260. if (LOG_LOCAL_LEVEL >= log_level) { \
  261. esp_rom_printf(LOG_FORMAT(log_tag_letter, format), esp_log_timestamp(), tag, ##__VA_ARGS__); \
  262. }} while(0)
  263. #ifndef BOOTLOADER_BUILD
  264. #define ESP_LOGE( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_ERROR, tag, format, ##__VA_ARGS__)
  265. #define ESP_LOGW( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_WARN, tag, format, ##__VA_ARGS__)
  266. #define ESP_LOGI( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_INFO, tag, format, ##__VA_ARGS__)
  267. #define ESP_LOGD( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_DEBUG, tag, format, ##__VA_ARGS__)
  268. #define ESP_LOGV( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_VERBOSE, tag, format, ##__VA_ARGS__)
  269. #else
  270. /**
  271. * macro to output logs at ESP_LOG_ERROR level.
  272. *
  273. * @param tag tag of the log, which can be used to change the log level by ``esp_log_level_set`` at runtime.
  274. *
  275. * @see ``printf``
  276. */
  277. #define ESP_LOGE( tag, format, ... ) ESP_EARLY_LOGE(tag, format, ##__VA_ARGS__)
  278. /// macro to output logs at ``ESP_LOG_WARN`` level. @see ``ESP_LOGE``
  279. #define ESP_LOGW( tag, format, ... ) ESP_EARLY_LOGW(tag, format, ##__VA_ARGS__)
  280. /// macro to output logs at ``ESP_LOG_INFO`` level. @see ``ESP_LOGE``
  281. #define ESP_LOGI( tag, format, ... ) ESP_EARLY_LOGI(tag, format, ##__VA_ARGS__)
  282. /// macro to output logs at ``ESP_LOG_DEBUG`` level. @see ``ESP_LOGE``
  283. #define ESP_LOGD( tag, format, ... ) ESP_EARLY_LOGD(tag, format, ##__VA_ARGS__)
  284. /// macro to output logs at ``ESP_LOG_VERBOSE`` level. @see ``ESP_LOGE``
  285. #define ESP_LOGV( tag, format, ... ) ESP_EARLY_LOGV(tag, format, ##__VA_ARGS__)
  286. #endif // BOOTLOADER_BUILD
  287. /** runtime macro to output logs at a specified level.
  288. *
  289. * @param tag tag of the log, which can be used to change the log level by ``esp_log_level_set`` at runtime.
  290. * @param level level of the output log.
  291. * @param format format of the output log. see ``printf``
  292. * @param ... variables to be replaced into the log. see ``printf``
  293. *
  294. * @see ``printf``
  295. */
  296. #if CONFIG_LOG_TIMESTAMP_SOURCE_RTOS
  297. #define ESP_LOG_LEVEL(level, tag, format, ...) do { \
  298. if (level==ESP_LOG_ERROR ) { esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
  299. else if (level==ESP_LOG_WARN ) { esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
  300. else if (level==ESP_LOG_DEBUG ) { esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
  301. else if (level==ESP_LOG_VERBOSE ) { esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
  302. else { esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
  303. } while(0)
  304. #elif CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM
  305. #define ESP_LOG_LEVEL(level, tag, format, ...) do { \
  306. 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__); } \
  307. 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__); } \
  308. 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__); } \
  309. 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__); } \
  310. else { esp_log_write(ESP_LOG_INFO, tag, LOG_SYSTEM_TIME_FORMAT(I, format), esp_log_system_timestamp(), tag, ##__VA_ARGS__); } \
  311. } while(0)
  312. #endif //CONFIG_LOG_TIMESTAMP_SOURCE_xxx
  313. /** runtime macro to output logs at a specified level. Also check the level with ``LOG_LOCAL_LEVEL``.
  314. *
  315. * @see ``printf``, ``ESP_LOG_LEVEL``
  316. */
  317. #define ESP_LOG_LEVEL_LOCAL(level, tag, format, ...) do { \
  318. if ( LOG_LOCAL_LEVEL >= level ) ESP_LOG_LEVEL(level, tag, format, ##__VA_ARGS__); \
  319. } while(0)
  320. /**
  321. * @brief Macro to output logs when the cache is disabled. log at ``ESP_LOG_ERROR`` level.
  322. *
  323. * Similar to `ESP_EARLY_LOGE`, the log level cannot be changed by `esp_log_level_set`.
  324. *
  325. * Usage: `ESP_DRAM_LOGE(DRAM_STR("my_tag"), "format", or `ESP_DRAM_LOGE(TAG, "format", ...)`,
  326. * where TAG is a char* that points to a str in the DRAM.
  327. *
  328. * @note Placing log strings in DRAM reduces available DRAM, so only use when absolutely essential.
  329. *
  330. * @see ``esp_rom_printf``,``ESP_LOGE``
  331. */
  332. #define ESP_DRAM_LOGE( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_ERROR, E, ##__VA_ARGS__)
  333. /// macro to output logs when the cache is disabled at ``ESP_LOG_WARN`` level. @see ``ESP_DRAM_LOGW``,``ESP_LOGW``, ``esp_rom_printf``
  334. #define ESP_DRAM_LOGW( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_WARN, W, ##__VA_ARGS__)
  335. /// macro to output logs when the cache is disabled at ``ESP_LOG_INFO`` level. @see ``ESP_DRAM_LOGI``,``ESP_LOGI``, ``esp_rom_printf``
  336. #define ESP_DRAM_LOGI( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_INFO, I, ##__VA_ARGS__)
  337. /// macro to output logs when the cache is disabled at ``ESP_LOG_DEBUG`` level. @see ``ESP_DRAM_LOGD``,``ESP_LOGD``, ``esp_rom_printf``
  338. #define ESP_DRAM_LOGD( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_DEBUG, D, ##__VA_ARGS__)
  339. /// macro to output logs when the cache is disabled at ``ESP_LOG_VERBOSE`` level. @see ``ESP_DRAM_LOGV``,``ESP_LOGV``, ``esp_rom_printf``
  340. #define ESP_DRAM_LOGV( tag, format, ... ) ESP_DRAM_LOG_IMPL(tag, format, ESP_LOG_VERBOSE, V, ##__VA_ARGS__)
  341. /** @cond */
  342. #define _ESP_LOG_DRAM_LOG_FORMAT(letter, format) DRAM_STR(#letter " %s: " format "\n")
  343. #define ESP_DRAM_LOG_IMPL(tag, format, log_level, log_tag_letter, ...) do { \
  344. if (LOG_LOCAL_LEVEL >= log_level) { \
  345. esp_rom_printf(_ESP_LOG_DRAM_LOG_FORMAT(log_tag_letter, format), tag, ##__VA_ARGS__); \
  346. }} while(0)
  347. /** @endcond */
  348. #ifdef __cplusplus
  349. }
  350. #endif
  351. #endif /* __ESP_LOG_H__ */