esp_log.h 12 KB

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