ESPHome  2023.8.3
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 ESP_LOGW(tag, ...) esph_log_w(tag, __VA_ARGS__)
148 #define ESP_LOGI(tag, ...) esph_log_i(tag, __VA_ARGS__)
149 #define ESP_LOGD(tag, ...) esph_log_d(tag, __VA_ARGS__)
150 #define ESP_LOGCONFIG(tag, ...) esph_log_config(tag, __VA_ARGS__)
151 #define ESP_LOGV(tag, ...) esph_log_v(tag, __VA_ARGS__)
152 #define ESP_LOGVV(tag, ...) esph_log_vv(tag, __VA_ARGS__)
153 
154 #define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
155 #define BYTE_TO_BINARY(byte) \
156  ((byte) &0x80 ? '1' : '0'), ((byte) &0x40 ? '1' : '0'), ((byte) &0x20 ? '1' : '0'), ((byte) &0x10 ? '1' : '0'), \
157  ((byte) &0x08 ? '1' : '0'), ((byte) &0x04 ? '1' : '0'), ((byte) &0x02 ? '1' : '0'), ((byte) &0x01 ? '1' : '0')
158 #define YESNO(b) ((b) ? "YES" : "NO")
159 #define ONOFF(b) ((b) ? "ON" : "OFF")
160 #define TRUEFALSE(b) ((b) ? "TRUE" : "FALSE")
161 
162 // Helper class that identifies strings that may be stored in flash storage (similar to Arduino's __FlashStringHelper)
163 struct LogString;
164 
165 #ifdef USE_STORE_LOG_STR_IN_FLASH
166 
167 #include <pgmspace.h>
168 
169 #if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 0)
170 #define LOG_STR_ARG(s) ((PGM_P) (s))
171 #else
172 // Pre-Arduino 2.5, we can't pass a PSTR() to printf(). Emulate support by copying the message to a
173 // local buffer first. String length is limited to 63 characters.
174 // https://github.com/esp8266/Arduino/commit/6280e98b0360f85fdac2b8f10707fffb4f6e6e31
175 #define LOG_STR_ARG(s) \
176  ({ \
177  char __buf[64]; \
178  __buf[63] = '\0'; \
179  strncpy_P(__buf, (PGM_P) (s), 63); \
180  __buf; \
181  })
182 #endif
183 
184 #define LOG_STR(s) (reinterpret_cast<const LogString *>(PSTR(s)))
185 #define LOG_STR_LITERAL(s) LOG_STR_ARG(LOG_STR(s))
186 
187 #else // !USE_STORE_LOG_STR_IN_FLASH
188 
189 #define LOG_STR(s) (reinterpret_cast<const LogString *>(s))
190 #define LOG_STR_ARG(s) (reinterpret_cast<const char *>(s))
191 #define LOG_STR_LITERAL(s) (s)
192 
193 #endif
194 
195 } // 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
int HOT esp_idf_log_vprintf_(const char *format, va_list args)
Definition: log.cpp:50