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 #include <inttypes.h>
32 
33 #include "log.h"
34 
35 extern uint32_t system_get_time_ms (void);
36 extern int ets_printf(const char *fmt, ...);
37 
38 #if MODULE_ESP_LOG_COLORED
39 
40 #define LOG_RESET_COLOR "\033[0m"
41 #define LOG_COLOR_E "\033[1;31m"
42 #define LOG_COLOR_W "\033[1;33m"
43 #define LOG_COLOR_I "\033[1m"
44 #define LOG_COLOR_D "\033[0;32m"
45 #define LOG_COLOR_V
46 
47 #else /* MODULE_ESP_LOG_COLORED */
48 
49 #define LOG_RESET_COLOR
50 #define LOG_COLOR_E
51 #define LOG_COLOR_W
52 #define LOG_COLOR_I
53 #define LOG_COLOR_D
54 #define LOG_COLOR_V
55 
56 #endif /* MODULE_ESP_LOG_COLORED */
57 
58 #if MODULE_ESP_LOG_TAGGED
59 
60 #define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%" PRIu32 ") [%s] " format LOG_RESET_COLOR
61 
62 #define LOG_TAG(level, letter, tag, format, ...) \
63  do { \
64  if ((unsigned)level <= (unsigned)LOG_LEVEL) { \
65  printf(LOG_FORMAT(letter, format), system_get_time_ms(), tag, ##__VA_ARGS__); \
66  fflush(stdout); \
67  } \
68  } while (0)
69 
70 #define LOG_TAG_EARLY(level, letter, tag, format, ...) \
71  do { \
72  if (LOG_LEVEL >= level) { \
73  ets_printf(LOG_FORMAT(letter, format), system_get_time_ms(), tag, ##__VA_ARGS__); \
74  } \
75  } while (0)
76 
77 #else /* MODULE_ESP_LOG_TAGGED */
78 
79 #define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter format LOG_RESET_COLOR
80 
81 #define LOG_TAG(level, letter, tag, format, ...) \
82  do { \
83  (void)tag; \
84  if ((unsigned)level <= (unsigned)LOG_LEVEL) { \
85  printf(LOG_FORMAT(letter, format), ##__VA_ARGS__); \
86  fflush(stdout); \
87  } \
88  } while (0U)
89 
90 #define LOG_TAG_EARLY(level, letter, tag, format, ...) \
91  do { \
92  (void)tag; \
93  if ((unsigned)level <= (unsigned)LOG_LEVEL) { \
94  ets_printf(LOG_FORMAT(letter, format), ##__VA_ARGS__); \
95  } \
96  } while (0U)
97 
98 #endif /* MODULE_ESP_LOG_TAGGED */
99 
104 #ifndef MODULE_LOG_PRINTFNOFORMAT
105 #undef LOG_ERROR
106 #undef LOG_INFO
107 #undef LOG_WARNING
108 #undef LOG_DEBUG
109 #define LOG_ERROR(format, ...) LOG_TAG(LOG_ERROR, E, __func__, format, ##__VA_ARGS__)
110 #define LOG_WARNING(format, ...) LOG_TAG(LOG_WARNING, W, __func__, format, ##__VA_ARGS__)
111 #define LOG_INFO(format, ...) LOG_TAG(LOG_INFO, I, __func__, format, ##__VA_ARGS__)
112 #define LOG_DEBUG(format, ...) LOG_TAG(LOG_DEBUG, D, __func__, format, ##__VA_ARGS__)
113 #endif
114 
116 #define LOG_TAG_ERROR(tag, format, ...) LOG_TAG(LOG_ERROR, E, tag, format, ##__VA_ARGS__)
117 #define LOG_TAG_WARNING(tag, format, ...) LOG_TAG(LOG_WARNING, W, tag, format, ##__VA_ARGS__)
118 #define LOG_TAG_INFO(tag, format, ...) LOG_TAG(LOG_INFO, I, tag, format, ##__VA_ARGS__)
119 #define LOG_TAG_DEBUG(tag, format, ...) LOG_TAG(LOG_DEBUG, D, tag, format, ##__VA_ARGS__)
120 #define LOG_TAG_ALL(tag, format, ...) LOG_TAG(LOG_ALL, V, tag, format, ##__VA_ARGS__)
121 
123 #define ESP_EARLY_LOGE(tag, format, ...) LOG_TAG_EARLY(LOG_ERROR, E, tag, format "\n", ##__VA_ARGS__)
124 #define ESP_EARLY_LOGW(tag, format, ...) LOG_TAG_EARLY(LOG_WARNING, W, tag, format "\n", ##__VA_ARGS__)
125 #define ESP_EARLY_LOGI(tag, format, ...) LOG_TAG_EARLY(LOG_INFO, I, tag, format "\n", ##__VA_ARGS__)
126 #define ESP_EARLY_LOGD(tag, format, ...) LOG_TAG_EARLY(LOG_DEBUG, D, tag, format "\n", ##__VA_ARGS__)
127 #define ESP_EARLY_LOGV(tag, format, ...) LOG_TAG_EARLY(LOG_ALL, V, tag, format "\n", ##__VA_ARGS__)
128 
129 #ifdef CPU_ESP8266
130 #define ESP_LOGE(tag, format, ...) LOG_TAG(LOG_ERROR, E, tag, format "\n", ##__VA_ARGS__)
131 #define ESP_LOGW(tag, format, ...) LOG_TAG(LOG_WARNING, W, tag, format "\n", ##__VA_ARGS__)
132 #define ESP_LOGI(tag, format, ...) LOG_TAG(LOG_INFO, I, tag, format "\n", ##__VA_ARGS__)
133 #define ESP_LOGD(tag, format, ...) LOG_TAG(LOG_DEBUG, D, tag, format "\n", ##__VA_ARGS__)
134 #define ESP_LOGV(tag, format, ...) LOG_TAG(LOG_ALL, V, tag, format "\n", ##__VA_ARGS__)
135 #endif
136 
137 #ifdef __cplusplus
138 }
139 #endif
140 
141 #endif /* DOXYGEN */
142 
143 #endif /* ESP_COMMON_LOG_H */
Adds include for missing inttype definitions.
stdio wrapper to extend the C libs stdio
uint32_t system_get_time_ms(void)
Time since boot in ms (32bit version)