esp_log.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /**
  22. * @brief Logging library
  23. *
  24. * Log library has two ways of managing log verbosity: compile time, set via
  25. * menuconfig, and runtime, using esp_log_set_level function.
  26. *
  27. * At compile time, filtering is done using CONFIG_LOG_DEFAULT_LEVEL macro, set via
  28. * menuconfig. All logging statments for levels higher than CONFIG_LOG_DEFAULT_LEVEL
  29. * will be removed by the preprocessor.
  30. *
  31. * At run time, all logs below CONFIG_LOG_DEFAULT_LEVEL are enabled by default.
  32. * esp_log_set_level function may be used to set logging level per module.
  33. * Modules are identified by their tags, which are human-readable ASCII
  34. * zero-terminated strings.
  35. *
  36. * How to use this library:
  37. *
  38. * In each C file which uses logging functionality, define TAG variable like this:
  39. *
  40. * static const char* TAG = "MyModule";
  41. *
  42. * then use one of logging macros to produce output, e.g:
  43. *
  44. * ESP_LOGW(TAG, "Baud rate error %.1f%%. Requested: %d baud, actual: %d baud", error * 100, baud_req, baud_real);
  45. *
  46. * Several macros are available for different verbosity levels:
  47. *
  48. * ESP_LOGE — error
  49. * ESP_LOGW — warning
  50. * ESP_LOGI — info
  51. * ESP_LOGD - debug
  52. * ESP_LOGV - verbose
  53. *
  54. * Additionally there is an _EARLY_ variant for each of these macros (e.g. ESP_EARLY_LOGE).
  55. * These variants can run in startup code, before heap allocator and syscalls
  56. * have been initialized.
  57. * When compiling bootloader, normal ESP_LOGx macros fall back to the same implementation
  58. * as ESP_EARLY_LOGx macros. So the only place where ESP_EARLY_LOGx have to be used explicitly
  59. * is the early startup code, such as heap allocator initialization code.
  60. *
  61. * (Note that such distinction would not have been necessary if we would have an
  62. * ets_vprintf function in the ROM. Then it would be possible to switch implementation
  63. * from _EARLY version to normal version on the fly. Unfortunately, ets_vprintf in ROM
  64. * has been inlined by the compiler into ets_printf, so it is not accessible outside.)
  65. *
  66. * To override default verbosity level at file or component scope, define LOG_LOCAL_LEVEL macro.
  67. * At file scope, define it before including esp_log.h, e.g.:
  68. *
  69. * #define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
  70. * #include "esp_log.h"
  71. *
  72. * At component scope, define it in component makefile:
  73. *
  74. * CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_DEBUG
  75. *
  76. * To configure logging output per module at runtime, add calls to esp_log_set_level function:
  77. *
  78. * esp_log_set_level("*", ESP_LOG_ERROR); // set all components to ERROR level
  79. * esp_log_set_level("wifi", ESP_LOG_WARN); // enable WARN logs from WiFi stack
  80. * esp_log_set_level("dhcpc", ESP_LOG_INFO); // enable INFO logs from DHCP client
  81. *
  82. */
  83. typedef enum {
  84. ESP_LOG_NONE, // No log output
  85. ESP_LOG_ERROR, // Critical errors, software module can not recover on its own
  86. ESP_LOG_WARN, // Error conditions from which recovery measures have been taken
  87. ESP_LOG_INFO, // Information messages which describe normal flow of events
  88. ESP_LOG_DEBUG, // Extra information which is not necessary for normal use (values, pointers, sizes, etc).
  89. ESP_LOG_VERBOSE // Bigger chunks of debugging information, or frequent messages which can potentially flood the output.
  90. } esp_log_level_t;
  91. typedef int (*vprintf_like_t)(const char *, va_list);
  92. /**
  93. * @brief Set log level for given tag
  94. *
  95. * If logging for given component has already been enabled, changes previous setting.
  96. *
  97. * @param tag Tag of the log entries to enable. Must be a non-NULL zero terminated string.
  98. * Value "*" resets log level for all tags to the given value.
  99. *
  100. * @param level Selects log level to enable. Only logs at this and lower levels will be shown.
  101. */
  102. void esp_log_level_set(const char* tag, esp_log_level_t level);
  103. /**
  104. * @brief Set function used to output log entries
  105. *
  106. * By default, log output goes to UART0. This function can be used to redirect log
  107. * output to some other destination, such as file or network.
  108. *
  109. * @param func Function used for output. Must have same signature as vprintf.
  110. */
  111. void esp_log_set_vprintf(vprintf_like_t func);
  112. /**
  113. * @brief Write message into the log
  114. *
  115. * This function is not intended to be used directly. Instead, use one of
  116. * ESP_LOGE, ESP_LOGW, ESP_LOGI, ESP_LOGD, ESP_LOGV macros.
  117. *
  118. * This function or these macros should not be used from an interrupt.
  119. */
  120. void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4)));
  121. /**
  122. * @brief Function which returns timestamp to be used in log output
  123. *
  124. * This function is used in expansion of ESP_LOGx macros.
  125. * In the 2nd stage bootloader, and at early application startup stage
  126. * this function uses CPU cycle counter as time source. Later when
  127. * FreeRTOS scheduler start running, it switches to FreeRTOS tick count.
  128. *
  129. * For now, we ignore millisecond counter overflow.
  130. *
  131. * @return timestamp, in milliseconds
  132. */
  133. uint32_t esp_log_timestamp();
  134. #if CONFIG_LOG_COLORS
  135. #define LOG_COLOR_BLACK "30"
  136. #define LOG_COLOR_RED "31"
  137. #define LOG_COLOR_GREEN "32"
  138. #define LOG_COLOR_BROWN "33"
  139. #define LOG_COLOR_BLUE "34"
  140. #define LOG_COLOR_PURPLE "35"
  141. #define LOG_COLOR_CYAN "36"
  142. #define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
  143. #define LOG_BOLD(COLOR) "\033[1;" COLOR "m"
  144. #define LOG_RESET_COLOR "\033[0m"
  145. #define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
  146. #define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
  147. #define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
  148. #define LOG_COLOR_D
  149. #define LOG_COLOR_V
  150. #else //CONFIG_LOG_COLORS
  151. #define LOG_COLOR_E
  152. #define LOG_COLOR_W
  153. #define LOG_COLOR_I
  154. #define LOG_COLOR_D
  155. #define LOG_COLOR_V
  156. #define LOG_RESET_COLOR
  157. #endif //CONFIG_LOG_COLORS
  158. #define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%d) %s: " format LOG_RESET_COLOR "\n"
  159. #ifndef LOG_LOCAL_LEVEL
  160. #ifndef BOOTLOADER_BUILD
  161. #define LOG_LOCAL_LEVEL ((esp_log_level_t) CONFIG_LOG_DEFAULT_LEVEL)
  162. #else
  163. #define LOG_LOCAL_LEVEL ((esp_log_level_t) CONFIG_LOG_BOOTLOADER_LEVEL)
  164. #endif
  165. #endif
  166. #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__); }
  167. #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__); }
  168. #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__); }
  169. #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__); }
  170. #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__); }
  171. #ifndef BOOTLOADER_BUILD
  172. #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__); }
  173. #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__); }
  174. #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__); }
  175. #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__); }
  176. #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__); }
  177. #else
  178. #define ESP_LOGE( tag, format, ... ) ESP_EARLY_LOGE(tag, format, ##__VA_ARGS__)
  179. #define ESP_LOGW( tag, format, ... ) ESP_EARLY_LOGW(tag, format, ##__VA_ARGS__)
  180. #define ESP_LOGI( tag, format, ... ) ESP_EARLY_LOGI(tag, format, ##__VA_ARGS__)
  181. #define ESP_LOGD( tag, format, ... ) ESP_EARLY_LOGD(tag, format, ##__VA_ARGS__)
  182. #define ESP_LOGV( tag, format, ... ) ESP_EARLY_LOGV(tag, format, ##__VA_ARGS__)
  183. #endif // BOOTLOADER_BUILD
  184. #ifdef __cplusplus
  185. }
  186. #endif
  187. #endif /* __ESP_LOG_H__ */