ESPHome  2022.12.8
log.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <cstdarg>
5 #include <string>
6 
7 #ifdef USE_STORE_LOG_STR_IN_FLASH
8 #include "WString.h"
9 #include "esphome/core/defines.h" // for USE_ARDUINO_VERSION_CODE
10 #endif
11 
12 // Include ESP-IDF/Arduino based logging methods here so they don't undefine ours later
13 #if defined(USE_ESP32_FRAMEWORK_ARDUINO) || defined(USE_ESP_IDF)
14 #include <esp_err.h>
15 #include <esp_log.h>
16 #endif
17 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
18 #include <esp32-hal-log.h>
19 #endif
20 
21 namespace esphome {
22 
23 #define ESPHOME_LOG_LEVEL_NONE 0
24 #define ESPHOME_LOG_LEVEL_ERROR 1
25 #define ESPHOME_LOG_LEVEL_WARN 2
26 #define ESPHOME_LOG_LEVEL_INFO 3
27 #define ESPHOME_LOG_LEVEL_CONFIG 4
28 #define ESPHOME_LOG_LEVEL_DEBUG 5
29 #define ESPHOME_LOG_LEVEL_VERBOSE 6
30 #define ESPHOME_LOG_LEVEL_VERY_VERBOSE 7
31 
32 #ifndef ESPHOME_LOG_LEVEL
33 #define ESPHOME_LOG_LEVEL ESPHOME_LOG_LEVEL_NONE
34 #endif
35 
36 #define ESPHOME_LOG_COLOR_BLACK "30"
37 #define ESPHOME_LOG_COLOR_RED "31" // ERROR
38 #define ESPHOME_LOG_COLOR_GREEN "32" // INFO
39 #define ESPHOME_LOG_COLOR_YELLOW "33" // WARNING
40 #define ESPHOME_LOG_COLOR_BLUE "34"
41 #define ESPHOME_LOG_COLOR_MAGENTA "35" // CONFIG
42 #define ESPHOME_LOG_COLOR_CYAN "36" // DEBUG
43 #define ESPHOME_LOG_COLOR_GRAY "37" // VERBOSE
44 #define ESPHOME_LOG_COLOR_WHITE "38"
45 #define ESPHOME_LOG_SECRET_BEGIN "\033[5m"
46 #define ESPHOME_LOG_SECRET_END "\033[6m"
47 #define LOG_SECRET(x) ESPHOME_LOG_SECRET_BEGIN x ESPHOME_LOG_SECRET_END
48 
49 #define ESPHOME_LOG_COLOR(COLOR) "\033[0;" COLOR "m"
50 #define ESPHOME_LOG_BOLD(COLOR) "\033[1;" COLOR "m"
51 #define ESPHOME_LOG_RESET_COLOR "\033[0m"
52 
53 void esp_log_printf_(int level, const char *tag, int line, const char *format, ...) // NOLINT
54  __attribute__((format(printf, 4, 5)));
55 #ifdef USE_STORE_LOG_STR_IN_FLASH
56 void esp_log_printf_(int level, const char *tag, int line, const __FlashStringHelper *format, ...);
57 #endif
58 void esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args); // NOLINT
59 #ifdef USE_STORE_LOG_STR_IN_FLASH
60 void esp_log_vprintf_(int level, const char *tag, int line, const __FlashStringHelper *format, va_list args);
61 #endif
62 #if defined(USE_ESP32_FRAMEWORK_ARDUINO) || defined(USE_ESP_IDF)
63 int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
64 #endif
65 
66 #ifdef USE_STORE_LOG_STR_IN_FLASH
67 #define ESPHOME_LOG_FORMAT(format) F(format)
68 #else
69 #define ESPHOME_LOG_FORMAT(format) format
70 #endif
71 
72 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
73 #define esph_log_vv(tag, format, ...) \
74  esp_log_printf_(ESPHOME_LOG_LEVEL_VERY_VERBOSE, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
75 
76 #define ESPHOME_LOG_HAS_VERY_VERBOSE
77 #else
78 #define esph_log_vv(tag, format, ...)
79 #endif
80 
81 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
82 #define esph_log_v(tag, format, ...) \
83  esp_log_printf_(ESPHOME_LOG_LEVEL_VERBOSE, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
84 
85 #define ESPHOME_LOG_HAS_VERBOSE
86 #else
87 #define esph_log_v(tag, format, ...)
88 #endif
89 
90 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
91 #define esph_log_d(tag, format, ...) \
92  esp_log_printf_(ESPHOME_LOG_LEVEL_DEBUG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
93 #define esph_log_config(tag, format, ...) \
94  esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
95 
96 #define ESPHOME_LOG_HAS_DEBUG
97 #define ESPHOME_LOG_HAS_CONFIG
98 #else
99 #define esph_log_d(tag, format, ...)
100 #define esph_log_config(tag, format, ...)
101 #endif
102 
103 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_INFO
104 #define esph_log_i(tag, format, ...) \
105  esp_log_printf_(ESPHOME_LOG_LEVEL_INFO, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
106 
107 #define ESPHOME_LOG_HAS_INFO
108 #else
109 #define esph_log_i(tag, format, ...)
110 #endif
111 
112 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
113 #define esph_log_w(tag, format, ...) \
114  esp_log_printf_(ESPHOME_LOG_LEVEL_WARN, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
115 
116 #define ESPHOME_LOG_HAS_WARN
117 #else
118 #define esph_log_w(tag, format, ...)
119 #endif
120 
121 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
122 #define esph_log_e(tag, format, ...) \
123  esp_log_printf_(ESPHOME_LOG_LEVEL_ERROR, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
124 
125 #define ESPHOME_LOG_HAS_ERROR
126 #else
127 #define esph_log_e(tag, format, ...)
128 #endif
129 
130 #ifdef ESP_LOGE
131 #undef ESP_LOGE
132 #endif
133 #ifdef ESP_LOGW
134 #undef ESP_LOGW
135 #endif
136 #ifdef ESP_LOGI
137 #undef ESP_LOGI
138 #endif
139 #ifdef ESP_LOGD
140 #undef ESP_LOGD
141 #endif
142 #ifdef ESP_LOGV
143 #undef ESP_LOGV
144 #endif
145 
146 #define ESP_LOGE(tag, ...) esph_log_e(tag, __VA_ARGS__)
147 #define LOG_E(tag, ...) ESP_LOGE(tag, __VA__ARGS__)
148 #define ESP_LOGW(tag, ...) esph_log_w(tag, __VA_ARGS__)
149 #define LOG_W(tag, ...) ESP_LOGW(tag, __VA__ARGS__)
150 #define ESP_LOGI(tag, ...) esph_log_i(tag, __VA_ARGS__)
151 #define LOG_I(tag, ...) ESP_LOGI(tag, __VA__ARGS__)
152 #define ESP_LOGD(tag, ...) esph_log_d(tag, __VA_ARGS__)
153 #define LOG_D(tag, ...) ESP_LOGD(tag, __VA__ARGS__)
154 #define ESP_LOGCONFIG(tag, ...) esph_log_config(tag, __VA_ARGS__)
155 #define LOG_CONFIG(tag, ...) ESP_LOGCONFIG(tag, __VA__ARGS__)
156 #define ESP_LOGV(tag, ...) esph_log_v(tag, __VA_ARGS__)
157 #define LOG_V(tag, ...) ESP_LOGV(tag, __VA__ARGS__)
158 #define ESP_LOGVV(tag, ...) esph_log_vv(tag, __VA_ARGS__)
159 #define LOG_VV(tag, ...) ESP_LOGVV(tag, __VA__ARGS__)
160 
161 #define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
162 #define BYTE_TO_BINARY(byte) \
163  ((byte) &0x80 ? '1' : '0'), ((byte) &0x40 ? '1' : '0'), ((byte) &0x20 ? '1' : '0'), ((byte) &0x10 ? '1' : '0'), \
164  ((byte) &0x08 ? '1' : '0'), ((byte) &0x04 ? '1' : '0'), ((byte) &0x02 ? '1' : '0'), ((byte) &0x01 ? '1' : '0')
165 #define YESNO(b) ((b) ? "YES" : "NO")
166 #define ONOFF(b) ((b) ? "ON" : "OFF")
167 #define TRUEFALSE(b) ((b) ? "TRUE" : "FALSE")
168 
169 // Helper class that identifies strings that may be stored in flash storage (similar to Arduino's __FlashStringHelper)
170 struct LogString;
171 
172 #ifdef USE_STORE_LOG_STR_IN_FLASH
173 
174 #include <pgmspace.h>
175 
176 #if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 0)
177 #define LOG_STR_ARG(s) ((PGM_P)(s))
178 #else
179 // Pre-Arduino 2.5, we can't pass a PSTR() to printf(). Emulate support by copying the message to a
180 // local buffer first. String length is limited to 63 characters.
181 // https://github.com/esp8266/Arduino/commit/6280e98b0360f85fdac2b8f10707fffb4f6e6e31
182 #define LOG_STR_ARG(s) \
183  ({ \
184  char __buf[64]; \
185  __buf[63] = '\0'; \
186  strncpy_P(__buf, (PGM_P)(s), 63); \
187  __buf; \
188  })
189 #endif
190 
191 #define LOG_STR(s) (reinterpret_cast<const LogString *>(PSTR(s)))
192 #define LOG_STR_LITERAL(s) LOG_STR_ARG(LOG_STR(s))
193 
194 #else // !USE_STORE_LOG_STR_IN_FLASH
195 
196 #define LOG_STR(s) (reinterpret_cast<const LogString *>(s))
197 #define LOG_STR_ARG(s) (reinterpret_cast<const char *>(s))
198 #define LOG_STR_LITERAL(s) (s)
199 
200 #endif
201 
202 } // namespace esphome
void HOT esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args)
Definition: log.cpp:26
enum esphome::EntityCategory __attribute__
void HOT esp_log_printf_(int level, const char *tag, int line, const char *format,...)
Definition: log.cpp:11
Definition: a4988.cpp:4
int HOT esp_idf_log_vprintf_(const char *format, va_list args)
Definition: log.cpp:50