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