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