esp_log.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 BOOTLOADER_BUILD
  19. #include <rom/ets_sys.h>
  20. #endif
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * @brief Log level
  26. *
  27. */
  28. typedef enum {
  29. ESP_LOG_NONE, /*!< No log output */
  30. ESP_LOG_ERROR, /*!< Critical errors, software module can not recover on its own */
  31. ESP_LOG_WARN, /*!< Error conditions from which recovery measures have been taken */
  32. ESP_LOG_INFO, /*!< Information messages which describe normal flow of events */
  33. ESP_LOG_DEBUG, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
  34. ESP_LOG_VERBOSE /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
  35. } esp_log_level_t;
  36. typedef int (*vprintf_like_t)(const char *, va_list);
  37. /**
  38. * @brief Set log level for given tag
  39. *
  40. * If logging for given component has already been enabled, changes previous setting.
  41. *
  42. * @param tag Tag of the log entries to enable. Must be a non-NULL zero terminated string.
  43. * Value "*" resets log level for all tags to the given value.
  44. *
  45. * @param level Selects log level to enable. Only logs at this and lower levels will be shown.
  46. */
  47. void esp_log_level_set(const char* tag, esp_log_level_t level);
  48. /**
  49. * @brief Set function used to output log entries
  50. *
  51. * By default, log output goes to UART0. This function can be used to redirect log
  52. * output to some other destination, such as file or network.
  53. *
  54. * @param func Function used for output. Must have same signature as vprintf.
  55. */
  56. void esp_log_set_vprintf(vprintf_like_t func);
  57. /**
  58. * @brief Function which returns timestamp to be used in log output
  59. *
  60. * This function is used in expansion of ESP_LOGx macros.
  61. * In the 2nd stage bootloader, and at early application startup stage
  62. * this function uses CPU cycle counter as time source. Later when
  63. * FreeRTOS scheduler start running, it switches to FreeRTOS tick count.
  64. *
  65. * For now, we ignore millisecond counter overflow.
  66. *
  67. * @return timestamp, in milliseconds
  68. */
  69. uint32_t esp_log_timestamp(void);
  70. /**
  71. * @brief Write message into the log
  72. *
  73. * This function is not intended to be used directly. Instead, use one of
  74. * ESP_LOGE, ESP_LOGW, ESP_LOGI, ESP_LOGD, ESP_LOGV macros.
  75. *
  76. * This function or these macros should not be used from an interrupt.
  77. */
  78. void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4)));
  79. #if CONFIG_LOG_COLORS
  80. #define LOG_COLOR_BLACK "30"
  81. #define LOG_COLOR_RED "31"
  82. #define LOG_COLOR_GREEN "32"
  83. #define LOG_COLOR_BROWN "33"
  84. #define LOG_COLOR_BLUE "34"
  85. #define LOG_COLOR_PURPLE "35"
  86. #define LOG_COLOR_CYAN "36"
  87. #define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
  88. #define LOG_BOLD(COLOR) "\033[1;" COLOR "m"
  89. #define LOG_RESET_COLOR "\033[0m"
  90. #define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
  91. #define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
  92. #define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
  93. #define LOG_COLOR_D
  94. #define LOG_COLOR_V
  95. #else //CONFIG_LOG_COLORS
  96. #define LOG_COLOR_E
  97. #define LOG_COLOR_W
  98. #define LOG_COLOR_I
  99. #define LOG_COLOR_D
  100. #define LOG_COLOR_V
  101. #define LOG_RESET_COLOR
  102. #endif //CONFIG_LOG_COLORS
  103. #define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%d) %s: " format LOG_RESET_COLOR "\n"
  104. #ifndef LOG_LOCAL_LEVEL
  105. #ifndef BOOTLOADER_BUILD
  106. #define LOG_LOCAL_LEVEL ((esp_log_level_t) CONFIG_LOG_DEFAULT_LEVEL)
  107. #else
  108. #define LOG_LOCAL_LEVEL ((esp_log_level_t) CONFIG_LOG_BOOTLOADER_LEVEL)
  109. #endif
  110. #endif
  111. #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__); }
  112. #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__); }
  113. #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__); }
  114. #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__); }
  115. #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__); }
  116. #ifndef BOOTLOADER_BUILD
  117. #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__); }
  118. #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__); }
  119. #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__); }
  120. #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__); }
  121. #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__); }
  122. #else
  123. #define ESP_LOGE( tag, format, ... ) ESP_EARLY_LOGE(tag, format, ##__VA_ARGS__)
  124. #define ESP_LOGW( tag, format, ... ) ESP_EARLY_LOGW(tag, format, ##__VA_ARGS__)
  125. #define ESP_LOGI( tag, format, ... ) ESP_EARLY_LOGI(tag, format, ##__VA_ARGS__)
  126. #define ESP_LOGD( tag, format, ... ) ESP_EARLY_LOGD(tag, format, ##__VA_ARGS__)
  127. #define ESP_LOGV( tag, format, ... ) ESP_EARLY_LOGV(tag, format, ##__VA_ARGS__)
  128. #endif // BOOTLOADER_BUILD
  129. #ifdef __cplusplus
  130. }
  131. #endif
  132. #endif /* __ESP_LOG_H__ */