esp_common_log.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Gunar Schorcht
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
20 #ifndef ESP_COMMON_LOG_H
21 #define ESP_COMMON_LOG_H
22 
23 #ifndef DOXYGEN
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #include <stdio.h>
30 #include <stdint.h>
31 
32 #include "log.h"
33 
34 extern uint32_t system_get_time_ms (void);
35 extern int ets_printf(const char *fmt, ...);
36 
37 #if MODULE_ESP_LOG_COLORED
38 
39 #define LOG_RESET_COLOR "\033[0m"
40 #define LOG_COLOR_E "\033[1;31m"
41 #define LOG_COLOR_W "\033[1;33m"
42 #define LOG_COLOR_I "\033[1m"
43 #define LOG_COLOR_D "\033[0;32m"
44 #define LOG_COLOR_V
45 
46 #else /* MODULE_ESP_LOG_COLORED */
47 
48 #define LOG_RESET_COLOR
49 #define LOG_COLOR_E
50 #define LOG_COLOR_W
51 #define LOG_COLOR_I
52 #define LOG_COLOR_D
53 #define LOG_COLOR_V
54 
55 #endif /* MODULE_ESP_LOG_COLORED */
56 
57 #if MODULE_ESP_LOG_TAGGED
58 
59 #define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%d) [%s] " format LOG_RESET_COLOR
60 
61 #define LOG_TAG(level, letter, tag, format, ...) \
62  do { \
63  if ((unsigned)level <= (unsigned)LOG_LEVEL) { \
64  printf(LOG_FORMAT(letter, format), system_get_time_ms(), tag, ##__VA_ARGS__); \
65  fflush(stdout); \
66  } \
67  } while(0)
68 
69 #define LOG_TAG_EARLY(level, letter, tag, format, ...) \
70  do { \
71  if (LOG_LEVEL >= level) { \
72  ets_printf(LOG_FORMAT(letter, format), system_get_time_ms(), tag, ##__VA_ARGS__); \
73  } \
74  } while(0)
75 
76 #else /* MODULE_ESP_LOG_TAGGED */
77 
78 #define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter format LOG_RESET_COLOR
79 
80 #define LOG_TAG(level, letter, tag, format, ...) \
81  do { \
82  (void)tag; \
83  if ((unsigned)level <= (unsigned)LOG_LEVEL) { \
84  printf(LOG_FORMAT(letter, format), ##__VA_ARGS__); \
85  fflush(stdout); \
86  } \
87  } while (0U)
88 
89 #define LOG_TAG_EARLY(level, letter, tag, format, ...) \
90  do { \
91  (void)tag; \
92  if ((unsigned)level <= (unsigned)LOG_LEVEL) { \
93  ets_printf(LOG_FORMAT(letter, format), ##__VA_ARGS__); \
94  } \
95  } while (0U)
96 
97 #endif /* MODULE_ESP_LOG_TAGGED */
98 
103 #ifndef MODULE_LOG_PRINTFNOFORMAT
104 #undef LOG_ERROR
105 #undef LOG_INFO
106 #undef LOG_WARNING
107 #undef LOG_DEBUG
108 #define LOG_ERROR(format, ...) LOG_TAG(LOG_ERROR , E, __func__, format, ##__VA_ARGS__)
109 #define LOG_WARNING(format, ...) LOG_TAG(LOG_WARNING, W, __func__, format, ##__VA_ARGS__)
110 #define LOG_INFO(format, ...) LOG_TAG(LOG_INFO , I, __func__, format, ##__VA_ARGS__)
111 #define LOG_DEBUG(format, ...) LOG_TAG(LOG_DEBUG , D, __func__, format, ##__VA_ARGS__)
112 #endif
113 
115 #define LOG_TAG_ERROR(tag, format, ...) LOG_TAG(LOG_ERROR , E, tag, format, ##__VA_ARGS__)
116 #define LOG_TAG_WARNING(tag, format, ...) LOG_TAG(LOG_WARNING, W, tag, format, ##__VA_ARGS__)
117 #define LOG_TAG_INFO(tag, format, ...) LOG_TAG(LOG_INFO , I, tag, format, ##__VA_ARGS__)
118 #define LOG_TAG_DEBUG(tag, format, ...) LOG_TAG(LOG_DEBUG , D, tag, format, ##__VA_ARGS__)
119 #define LOG_TAG_ALL(tag, format, ...) LOG_TAG(LOG_ALL , V, tag, format, ##__VA_ARGS__)
120 
122 #define ESP_EARLY_LOGE(tag, format, ...) LOG_TAG_EARLY(LOG_ERROR , E, tag, format "\n", ##__VA_ARGS__)
123 #define ESP_EARLY_LOGW(tag, format, ...) LOG_TAG_EARLY(LOG_WARNING, W, tag, format "\n", ##__VA_ARGS__)
124 #define ESP_EARLY_LOGI(tag, format, ...) LOG_TAG_EARLY(LOG_INFO , I, tag, format "\n", ##__VA_ARGS__)
125 #define ESP_EARLY_LOGD(tag, format, ...) LOG_TAG_EARLY(LOG_DEBUG, D, tag, format "\n", ##__VA_ARGS__)
126 #define ESP_EARLY_LOGV(tag, format, ...) LOG_TAG_EARLY(LOG_ALL , V, tag, format "\n", ##__VA_ARGS__)
127 #define ESP_LOGE(tag, format, ...) LOG_TAG(LOG_ERROR , E, tag, format "\n", ##__VA_ARGS__)
128 #define ESP_LOGW(tag, format, ...) LOG_TAG(LOG_WARNING, W, tag, format "\n", ##__VA_ARGS__)
129 #define ESP_LOGI(tag, format, ...) LOG_TAG(LOG_INFO , I, tag, format "\n", ##__VA_ARGS__)
130 #define ESP_LOGD(tag, format, ...) LOG_TAG(LOG_DEBUG, D, tag, format "\n", ##__VA_ARGS__)
131 #define ESP_LOGV(tag, format, ...) LOG_TAG(LOG_ALL , V, tag, format "\n", ##__VA_ARGS__)
132 
133 #ifdef __cplusplus
134 }
135 #endif
136 
137 #endif /* DOXYGEN */
138 
139 #endif /* ESP_COMMON_LOG_H */
log.h
System logging header.
system_get_time_ms
uint32_t system_get_time_ms(void)
Time since boot in ms (32bit version)