esp_log.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "sdkconfig.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #include "esp32/rom/ets_sys.h"
  20. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  21. #include "esp32s2beta/rom/ets_sys.h"
  22. #endif
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * @brief Log level
  28. *
  29. */
  30. typedef enum {
  31. ESP_LOG_NONE, /*!< No log output */
  32. ESP_LOG_ERROR, /*!< Critical errors, software module can not recover on its own */
  33. ESP_LOG_WARN, /*!< Error conditions from which recovery measures have been taken */
  34. ESP_LOG_INFO, /*!< Information messages which describe normal flow of events */
  35. ESP_LOG_DEBUG, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
  36. ESP_LOG_VERBOSE /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
  37. } esp_log_level_t;
  38. typedef int (*vprintf_like_t)(const char *, va_list);
  39. /**
  40. * @brief Set log level for given tag
  41. *
  42. * If logging for given component has already been enabled, changes previous setting.
  43. *
  44. * Note that this function can not raise log level above the level set using
  45. * CONFIG_LOG_DEFAULT_LEVEL setting in menuconfig.
  46. *
  47. * To raise log level above the default one for a given file, define
  48. * LOG_LOCAL_LEVEL to one of the ESP_LOG_* values, before including
  49. * esp_log.h in this file.
  50. *
  51. * @param tag Tag of the log entries to enable. Must be a non-NULL zero terminated string.
  52. * Value "*" resets log level for all tags to the given value.
  53. *
  54. * @param level Selects log level to enable. Only logs at this and lower verbosity
  55. * levels will be shown.
  56. */
  57. void esp_log_level_set(const char* tag, esp_log_level_t level);
  58. /**
  59. * @brief Set function used to output log entries
  60. *
  61. * By default, log output goes to UART0. This function can be used to redirect log
  62. * output to some other destination, such as file or network. Returns the original
  63. * log handler, which may be necessary to return output to the previous destination.
  64. *
  65. * @param func new Function used for output. Must have same signature as vprintf.
  66. *
  67. * @return func old Function used for output.
  68. */
  69. vprintf_like_t esp_log_set_vprintf(vprintf_like_t func);
  70. /**
  71. * @brief Function which returns timestamp to be used in log output
  72. *
  73. * This function is used in expansion of ESP_LOGx macros.
  74. * In the 2nd stage bootloader, and at early application startup stage
  75. * this function uses CPU cycle counter as time source. Later when
  76. * FreeRTOS scheduler start running, it switches to FreeRTOS tick count.
  77. *
  78. * For now, we ignore millisecond counter overflow.
  79. *
  80. * @return timestamp, in milliseconds
  81. */
  82. uint32_t esp_log_timestamp(void);
  83. /**
  84. * @brief Function which returns timestamp to be used in log output
  85. *
  86. * This function uses HW cycle counter and does not depend on OS,
  87. * so it can be safely used after application crash.
  88. *
  89. * @return timestamp, in milliseconds
  90. */
  91. uint32_t esp_log_early_timestamp(void);
  92. /**
  93. * @brief Write message into the log
  94. *
  95. * This function is not intended to be used directly. Instead, use one of
  96. * ESP_LOGE, ESP_LOGW, ESP_LOGI, ESP_LOGD, ESP_LOGV macros.
  97. *
  98. * This function or these macros should not be used from an interrupt.
  99. */
  100. void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4)));
  101. /** @cond */
  102. #include "esp_log_internal.h"
  103. #ifndef LOG_LOCAL_LEVEL
  104. #ifndef BOOTLOADER_BUILD
  105. #define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL
  106. #else
  107. #define LOG_LOCAL_LEVEL CONFIG_BOOTLOADER_LOG_LEVEL
  108. #endif
  109. #endif
  110. /** @endcond */
  111. /**
  112. * @brief Log a buffer of hex bytes at specified level, separated into 16 bytes each line.
  113. *
  114. * @param tag description tag
  115. * @param buffer Pointer to the buffer array
  116. * @param buff_len length of buffer in bytes
  117. * @param level level of the log
  118. *
  119. */
  120. #define ESP_LOG_BUFFER_HEX_LEVEL( tag, buffer, buff_len, level ) \
  121. do {\
  122. if ( LOG_LOCAL_LEVEL >= (level) ) { \
  123. esp_log_buffer_hex_internal( tag, buffer, buff_len, level ); \
  124. } \
  125. } while(0)
  126. /**
  127. * @brief Log a buffer of characters at specified level, separated into 16 bytes each line. Buffer should contain only printable characters.
  128. *
  129. * @param tag description tag
  130. * @param buffer Pointer to the buffer array
  131. * @param buff_len length of buffer in bytes
  132. * @param level level of the log
  133. *
  134. */
  135. #define ESP_LOG_BUFFER_CHAR_LEVEL( tag, buffer, buff_len, level ) \
  136. do {\
  137. if ( LOG_LOCAL_LEVEL >= (level) ) { \
  138. esp_log_buffer_char_internal( tag, buffer, buff_len, level ); \
  139. } \
  140. } while(0)
  141. /**
  142. * @brief Dump a buffer to the log at specified level.
  143. *
  144. * The dump log shows just like the one below:
  145. *
  146. * W (195) log_example: 0x3ffb4280 45 53 50 33 32 20 69 73 20 67 72 65 61 74 2c 20 |ESP32 is great, |
  147. * W (195) log_example: 0x3ffb4290 77 6f 72 6b 69 6e 67 20 61 6c 6f 6e 67 20 77 69 |working along wi|
  148. * W (205) log_example: 0x3ffb42a0 74 68 20 74 68 65 20 49 44 46 2e 00 |th the IDF..|
  149. *
  150. * It is highly recommend to use terminals with over 102 text width.
  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. #define ESP_LOG_BUFFER_HEXDUMP( tag, buffer, buff_len, level ) \
  158. do { \
  159. if ( LOG_LOCAL_LEVEL >= (level) ) { \
  160. esp_log_buffer_hexdump_internal( tag, buffer, buff_len, level); \
  161. } \
  162. } while(0)
  163. /**
  164. * @brief Log a buffer of hex bytes at Info level
  165. *
  166. * @param tag description tag
  167. * @param buffer Pointer to the buffer array
  168. * @param buff_len length of buffer in bytes
  169. *
  170. * @see ``esp_log_buffer_hex_level``
  171. *
  172. */
  173. #define ESP_LOG_BUFFER_HEX(tag, buffer, buff_len) \
  174. do { \
  175. if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { \
  176. ESP_LOG_BUFFER_HEX_LEVEL( tag, buffer, buff_len, ESP_LOG_INFO ); \
  177. }\
  178. } while(0)
  179. /**
  180. * @brief Log a buffer of characters at Info level. 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. *
  186. * @see ``esp_log_buffer_char_level``
  187. *
  188. */
  189. #define ESP_LOG_BUFFER_CHAR(tag, buffer, buff_len) \
  190. do { \
  191. if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { \
  192. ESP_LOG_BUFFER_CHAR_LEVEL( tag, buffer, buff_len, ESP_LOG_INFO ); \
  193. }\
  194. } while(0)
  195. /** @cond */
  196. //to be back compatible
  197. #define esp_log_buffer_hex ESP_LOG_BUFFER_HEX
  198. #define esp_log_buffer_char ESP_LOG_BUFFER_CHAR
  199. #if CONFIG_LOG_COLORS
  200. #define LOG_COLOR_BLACK "30"
  201. #define LOG_COLOR_RED "31"
  202. #define LOG_COLOR_GREEN "32"
  203. #define LOG_COLOR_BROWN "33"
  204. #define LOG_COLOR_BLUE "34"
  205. #define LOG_COLOR_PURPLE "35"
  206. #define LOG_COLOR_CYAN "36"
  207. #define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
  208. #define LOG_BOLD(COLOR) "\033[1;" COLOR "m"
  209. #define LOG_RESET_COLOR "\033[0m"
  210. #define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
  211. #define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
  212. #define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
  213. #define LOG_COLOR_D
  214. #define LOG_COLOR_V
  215. #else //CONFIG_LOG_COLORS
  216. #define LOG_COLOR_E
  217. #define LOG_COLOR_W
  218. #define LOG_COLOR_I
  219. #define LOG_COLOR_D
  220. #define LOG_COLOR_V
  221. #define LOG_RESET_COLOR
  222. #endif //CONFIG_LOG_COLORS
  223. #define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%d) %s: " format LOG_RESET_COLOR "\n"
  224. /** @endcond */
  225. /// 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``
  226. #define ESP_EARLY_LOGE( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_ERROR, E, ##__VA_ARGS__)
  227. /// macro to output logs in startup code at ``ESP_LOG_WARN`` level. @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
  228. #define ESP_EARLY_LOGW( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_WARN, W, ##__VA_ARGS__)
  229. /// macro to output logs in startup code at ``ESP_LOG_INFO`` level. @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
  230. #define ESP_EARLY_LOGI( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_INFO, I, ##__VA_ARGS__)
  231. /// macro to output logs in startup code at ``ESP_LOG_DEBUG`` level. @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
  232. #define ESP_EARLY_LOGD( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_DEBUG, D, ##__VA_ARGS__)
  233. /// macro to output logs in startup code at ``ESP_LOG_VERBOSE`` level. @see ``ESP_EARLY_LOGE``,``ESP_LOGE``, ``printf``
  234. #define ESP_EARLY_LOGV( tag, format, ... ) ESP_LOG_EARLY_IMPL(tag, format, ESP_LOG_VERBOSE, V, ##__VA_ARGS__)
  235. #define ESP_LOG_EARLY_IMPL(tag, format, log_level, log_tag_letter, ...) do { \
  236. if (LOG_LOCAL_LEVEL >= log_level) { \
  237. ets_printf(LOG_FORMAT(log_tag_letter, format), esp_log_timestamp(), tag, ##__VA_ARGS__); \
  238. }} while(0)
  239. #ifndef BOOTLOADER_BUILD
  240. #define ESP_LOGE( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_ERROR, tag, format, ##__VA_ARGS__)
  241. #define ESP_LOGW( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_WARN, tag, format, ##__VA_ARGS__)
  242. #define ESP_LOGI( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_INFO, tag, format, ##__VA_ARGS__)
  243. #define ESP_LOGD( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_DEBUG, tag, format, ##__VA_ARGS__)
  244. #define ESP_LOGV( tag, format, ... ) ESP_LOG_LEVEL_LOCAL(ESP_LOG_VERBOSE, tag, format, ##__VA_ARGS__)
  245. #else
  246. /**
  247. * macro to output logs at ESP_LOG_ERROR level.
  248. *
  249. * @param tag tag of the log, which can be used to change the log level by ``esp_log_level_set`` at runtime.
  250. *
  251. * @see ``printf``
  252. */
  253. #define ESP_LOGE( tag, format, ... ) ESP_EARLY_LOGE(tag, format, ##__VA_ARGS__)
  254. /// macro to output logs at ``ESP_LOG_WARN`` level. @see ``ESP_LOGE``
  255. #define ESP_LOGW( tag, format, ... ) ESP_EARLY_LOGW(tag, format, ##__VA_ARGS__)
  256. /// macro to output logs at ``ESP_LOG_INFO`` level. @see ``ESP_LOGE``
  257. #define ESP_LOGI( tag, format, ... ) ESP_EARLY_LOGI(tag, format, ##__VA_ARGS__)
  258. /// macro to output logs at ``ESP_LOG_DEBUG`` level. @see ``ESP_LOGE``
  259. #define ESP_LOGD( tag, format, ... ) ESP_EARLY_LOGD(tag, format, ##__VA_ARGS__)
  260. /// macro to output logs at ``ESP_LOG_VERBOSE`` level. @see ``ESP_LOGE``
  261. #define ESP_LOGV( tag, format, ... ) ESP_EARLY_LOGV(tag, format, ##__VA_ARGS__)
  262. #endif // BOOTLOADER_BUILD
  263. /** runtime macro to output logs at a specified level.
  264. *
  265. * @param tag tag of the log, which can be used to change the log level by ``esp_log_level_set`` at runtime.
  266. * @param level level of the output log.
  267. * @param format format of the output log. see ``printf``
  268. * @param ... variables to be replaced into the log. see ``printf``
  269. *
  270. * @see ``printf``
  271. */
  272. #define ESP_LOG_LEVEL(level, tag, format, ...) do { \
  273. if (level==ESP_LOG_ERROR ) { esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
  274. else if (level==ESP_LOG_WARN ) { esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
  275. else if (level==ESP_LOG_DEBUG ) { esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
  276. else if (level==ESP_LOG_VERBOSE ) { esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
  277. else { esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } \
  278. } while(0)
  279. /** runtime macro to output logs at a specified level. Also check the level with ``LOG_LOCAL_LEVEL``.
  280. *
  281. * @see ``printf``, ``ESP_LOG_LEVEL``
  282. */
  283. #define ESP_LOG_LEVEL_LOCAL(level, tag, format, ...) do { \
  284. if ( LOG_LOCAL_LEVEL >= level ) ESP_LOG_LEVEL(level, tag, format, ##__VA_ARGS__); \
  285. } while(0)
  286. #ifdef __cplusplus
  287. }
  288. #endif
  289. #endif /* __ESP_LOG_H__ */