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