ESPHome  2023.11.6
logger.cpp
Go to the documentation of this file.
1 #include "logger.h"
2 #include <cinttypes>
3 
4 #ifdef USE_ESP_IDF
5 #include <driver/uart.h>
6 
7 #if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) || \
8  defined(USE_ESP32_VARIANT_ESP32H2)
9 #include <driver/usb_serial_jtag.h>
10 #include <esp_vfs_dev.h>
11 #include <esp_vfs_usb_serial_jtag.h>
12 #endif
13 
14 #include "freertos/FreeRTOS.h"
15 #include "esp_idf_version.h"
16 
17 #include <cstdint>
18 #include <cstdio>
19 #include <fcntl.h>
20 
21 #endif // USE_ESP_IDF
22 
23 #if defined(USE_ESP32_FRAMEWORK_ARDUINO) || defined(USE_ESP_IDF)
24 #include <esp_log.h>
25 #endif // USE_ESP32_FRAMEWORK_ARDUINO || USE_ESP_IDF
26 #include "esphome/core/hal.h"
27 #include "esphome/core/log.h"
28 
29 namespace esphome {
30 namespace logger {
31 
32 static const char *const TAG = "logger";
33 
34 static const char *const LOG_LEVEL_COLORS[] = {
35  "", // NONE
36  ESPHOME_LOG_BOLD(ESPHOME_LOG_COLOR_RED), // ERROR
37  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_YELLOW), // WARNING
38  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_GREEN), // INFO
39  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_MAGENTA), // CONFIG
40  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_CYAN), // DEBUG
41  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_GRAY), // VERBOSE
42  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_WHITE), // VERY_VERBOSE
43 };
44 static const char *const LOG_LEVEL_LETTERS[] = {
45  "", // NONE
46  "E", // ERROR
47  "W", // WARNING
48  "I", // INFO
49  "C", // CONFIG
50  "D", // DEBUG
51  "V", // VERBOSE
52  "VV", // VERY_VERBOSE
53 };
54 
55 void Logger::write_header_(int level, const char *tag, int line) {
56  if (level < 0)
57  level = 0;
58  if (level > 7)
59  level = 7;
60 
61  const char *color = LOG_LEVEL_COLORS[level];
62  const char *letter = LOG_LEVEL_LETTERS[level];
63  this->printf_to_buffer_("%s[%s][%s:%03u]: ", color, letter, tag, line);
64 }
65 
66 void HOT Logger::log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) { // NOLINT
67  if (level > this->level_for(tag) || recursion_guard_)
68  return;
69 
70  recursion_guard_ = true;
71  this->reset_buffer_();
72  this->write_header_(level, tag, line);
73  this->vprintf_to_buffer_(format, args);
74  this->write_footer_();
75  this->log_message_(level, tag);
76  recursion_guard_ = false;
77 }
78 #ifdef USE_STORE_LOG_STR_IN_FLASH
79 void Logger::log_vprintf_(int level, const char *tag, int line, const __FlashStringHelper *format,
80  va_list args) { // NOLINT
81  if (level > this->level_for(tag) || recursion_guard_)
82  return;
83 
84  recursion_guard_ = true;
85  this->reset_buffer_();
86  // copy format string
87  auto *format_pgm_p = reinterpret_cast<const uint8_t *>(format);
88  size_t len = 0;
89  char ch = '.';
90  while (!this->is_buffer_full_() && ch != '\0') {
91  this->tx_buffer_[this->tx_buffer_at_++] = ch = (char) progmem_read_byte(format_pgm_p++);
92  }
93  // Buffer full form copying format
94  if (this->is_buffer_full_())
95  return;
96 
97  // length of format string, includes null terminator
98  uint32_t offset = this->tx_buffer_at_;
99 
100  // now apply vsnprintf
101  this->write_header_(level, tag, line);
102  this->vprintf_to_buffer_(this->tx_buffer_, args);
103  this->write_footer_();
104  this->log_message_(level, tag, offset);
105  recursion_guard_ = false;
106 }
107 #endif
108 
109 #ifdef USE_ESP_IDF
111  uart_config_t uart_config{};
112  uart_config.baud_rate = (int) baud_rate_;
113  uart_config.data_bits = UART_DATA_8_BITS;
114  uart_config.parity = UART_PARITY_DISABLE;
115  uart_config.stop_bits = UART_STOP_BITS_1;
116  uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
117 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
118  uart_config.source_clk = UART_SCLK_DEFAULT;
119 #endif
120  uart_param_config(this->uart_num_, &uart_config);
121  const int uart_buffer_size = tx_buffer_size_;
122  // Install UART driver using an event queue here
123  uart_driver_install(this->uart_num_, uart_buffer_size, uart_buffer_size, 10, nullptr, 0);
124 }
125 
126 #if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
128 #endif
129 
130 #if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) || \
131  defined(USE_ESP32_VARIANT_ESP32H2)
133  setvbuf(stdin, NULL, _IONBF, 0); // Disable buffering on stdin
134 
135  // Minicom, screen, idf_monitor send CR when ENTER key is pressed
136  esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
137  // Move the caret to the beginning of the next line on '\n'
138  esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
139 
140  // Enable non-blocking mode on stdin and stdout
141  fcntl(fileno(stdout), F_SETFL, 0);
142  fcntl(fileno(stdin), F_SETFL, 0);
143 
144  usb_serial_jtag_driver_config_t usb_serial_jtag_config{};
145  usb_serial_jtag_config.rx_buffer_size = 512;
146  usb_serial_jtag_config.tx_buffer_size = 512;
147 
148  esp_err_t ret = ESP_OK;
149  // Install USB-SERIAL-JTAG driver for interrupt-driven reads and writes
150  ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
151  if (ret != ESP_OK) {
152  return;
153  }
154 
155  // Tell vfs to use usb-serial-jtag driver
156  esp_vfs_usb_serial_jtag_use_driver();
157 }
158 #endif
159 #endif
160 
161 int HOT Logger::level_for(const char *tag) {
162  // Uses std::vector<> for low memory footprint, though the vector
163  // could be sorted to minimize lookup times. This feature isn't used that
164  // much anyway so it doesn't matter too much.
165  for (auto &it : this->log_levels_) {
166  if (it.tag == tag) {
167  return it.level;
168  }
169  }
170  return ESPHOME_LOG_LEVEL;
171 }
172 void HOT Logger::log_message_(int level, const char *tag, int offset) {
173  // remove trailing newline
174  if (this->tx_buffer_[this->tx_buffer_at_ - 1] == '\n') {
175  this->tx_buffer_at_--;
176  }
177  // make sure null terminator is present
178  this->set_null_terminator_();
179 
180  const char *msg = this->tx_buffer_ + offset;
181  if (this->baud_rate_ > 0) {
182 #ifdef USE_ARDUINO
183  this->hw_serial_->println(msg);
184 #endif // USE_ARDUINO
185 #ifdef USE_ESP_IDF
186  if (
187 #if defined(USE_ESP32_VARIANT_ESP32S2)
189 #elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32H2)
191 #elif defined(USE_ESP32_VARIANT_ESP32S3)
193 #else
194  /* DISABLES CODE */ (false) // NOLINT
195 #endif
196  ) {
197  puts(msg);
198  } else {
199  uart_write_bytes(this->uart_num_, msg, strlen(msg));
200  uart_write_bytes(this->uart_num_, "\n", 1);
201  }
202 #endif
203  }
204 
205 #ifdef USE_ESP32
206  // Suppress network-logging if memory constrained, but still log to serial
207  // ports. In some configurations (eg BLE enabled) there may be some transient
208  // memory exhaustion, and trying to log when OOM can lead to a crash. Skipping
209  // here usually allows the stack to recover instead.
210  // See issue #1234 for analysis.
211  if (xPortGetFreeHeapSize() < 2048)
212  return;
213 #endif
214 #ifdef USE_HOST
215  puts(msg);
216 #endif
217 
218  this->log_callback_.call(level, tag, msg);
219 }
220 
221 Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size) : baud_rate_(baud_rate), tx_buffer_size_(tx_buffer_size) {
222  // add 1 to buffer size for null terminator
223  this->tx_buffer_ = new char[this->tx_buffer_size_ + 1]; // NOLINT
224 }
225 
226 #ifndef USE_LIBRETINY
228  if (this->baud_rate_ > 0) {
229 #ifdef USE_ARDUINO
230  switch (this->uart_) {
232 #ifdef USE_ESP8266
234 #endif
235 #ifdef USE_RP2040
236  this->hw_serial_ = &Serial1;
237  Serial1.begin(this->baud_rate_);
238 #else
239  this->hw_serial_ = &Serial;
240  Serial.begin(this->baud_rate_);
241 #endif
242 #ifdef USE_ESP8266
243  if (this->uart_ == UART_SELECTION_UART0_SWAP) {
244  Serial.swap();
245  }
246  Serial.setDebugOutput(ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE);
247 #endif
248  break;
250 #ifdef USE_RP2040
251  this->hw_serial_ = &Serial2;
252  Serial2.begin(this->baud_rate_);
253 #else
254  this->hw_serial_ = &Serial1;
255  Serial1.begin(this->baud_rate_);
256 #endif
257 #ifdef USE_ESP8266
258  Serial1.setDebugOutput(ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE);
259 #endif
260  break;
261 #if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
262  !defined(USE_ESP32_VARIANT_ESP32S2) && !defined(USE_ESP32_VARIANT_ESP32S3)
264  this->hw_serial_ = &Serial2;
265  Serial2.begin(this->baud_rate_);
266  break;
267 #endif
268 #ifdef USE_RP2040
270  this->hw_serial_ = &Serial;
271  Serial.begin(this->baud_rate_);
272  break;
273 #endif
274  }
275 #endif // USE_ARDUINO
276 #ifdef USE_ESP_IDF
277  this->uart_num_ = UART_NUM_0;
278  switch (this->uart_) {
280  this->uart_num_ = UART_NUM_0;
281  break;
283  this->uart_num_ = UART_NUM_1;
284  break;
285 #if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
286  !defined(USE_ESP32_VARIANT_ESP32S2) && !defined(USE_ESP32_VARIANT_ESP32S3) && !defined(USE_ESP32_VARIANT_ESP32H2)
288  this->uart_num_ = UART_NUM_2;
289  break;
290 #endif // !USE_ESP32_VARIANT_ESP32C3 && !USE_ESP32_VARIANT_ESP32S2 && !USE_ESP32_VARIANT_ESP32S3 &&
291  // !USE_ESP32_VARIANT_ESP32H2
292 #if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
294  this->uart_num_ = -1;
295  this->init_usb_cdc_();
296  break;
297 #endif // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3
298 #if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) || \
299  defined(USE_ESP32_VARIANT_ESP32H2)
301  this->uart_num_ = -1;
302  this->init_usb_serial_jtag_();
303  break;
304 #endif // USE_ESP32_VARIANT_ESP32C3 || USE_ESP32_VARIANT_ESP32C6 || USE_ESP32_VARIANT_ESP32S3 ||
305  // USE_ESP32_VARIANT_ESP32H2
306  }
307  if (this->uart_num_ >= 0) {
308  this->init_uart_();
309  }
310 #endif // USE_ESP_IDF
311  }
312 #ifdef USE_ESP8266
313  else {
314  uart_set_debug(UART_NO);
315  }
316 #endif // USE_ESP8266
317 
318  global_logger = this;
319 #if defined(USE_ESP_IDF) || defined(USE_ESP32_FRAMEWORK_ARDUINO)
320  esp_log_set_vprintf(esp_idf_log_vprintf_);
321  if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
322  esp_log_level_set("*", ESP_LOG_VERBOSE);
323  }
324 #endif // USE_ESP_IDF || USE_ESP32_FRAMEWORK_ARDUINO
325 
326  ESP_LOGI(TAG, "Log initialized");
327 }
328 #else // USE_LIBRETINY
329 void Logger::pre_setup() {
330  if (this->baud_rate_ > 0) {
331  switch (this->uart_) {
332 #if LT_HW_UART0
334  this->hw_serial_ = &Serial0;
335  Serial0.begin(this->baud_rate_);
336  break;
337 #endif
338 #if LT_HW_UART1
340  this->hw_serial_ = &Serial1;
341  Serial1.begin(this->baud_rate_);
342  break;
343 #endif
344 #if LT_HW_UART2
346  this->hw_serial_ = &Serial2;
347  Serial2.begin(this->baud_rate_);
348  break;
349 #endif
350  default:
351  this->hw_serial_ = &Serial;
352  Serial.begin(this->baud_rate_);
353  if (this->uart_ != UART_SELECTION_DEFAULT) {
354  ESP_LOGW(TAG, " The chosen logger UART port is not available on this board."
355  "The default port was used instead.");
356  }
357  break;
358  }
359 
360  // change lt_log() port to match default Serial
361  if (this->uart_ == UART_SELECTION_DEFAULT) {
362  this->uart_ = (UARTSelection) (LT_UART_DEFAULT_SERIAL + 1);
363  lt_log_set_port(LT_UART_DEFAULT_SERIAL);
364  } else {
365  lt_log_set_port(this->uart_ - 1);
366  }
367  }
368 
369  global_logger = this;
370  ESP_LOGI(TAG, "Log initialized");
371 }
372 #endif // USE_LIBRETINY
373 
374 void Logger::set_baud_rate(uint32_t baud_rate) { this->baud_rate_ = baud_rate; }
375 void Logger::set_log_level(const std::string &tag, int log_level) {
376  this->log_levels_.push_back(LogLevelOverride{tag, log_level});
377 }
378 
379 #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY)
380 UARTSelection Logger::get_uart() const { return this->uart_; }
381 #endif
382 
383 void Logger::add_on_log_callback(std::function<void(int, const char *, const char *)> &&callback) {
384  this->log_callback_.add(std::move(callback));
385 }
386 float Logger::get_setup_priority() const { return setup_priority::BUS + 500.0f; }
387 const char *const LOG_LEVELS[] = {"NONE", "ERROR", "WARN", "INFO", "CONFIG", "DEBUG", "VERBOSE", "VERY_VERBOSE"};
388 #ifdef USE_ESP32
389 const char *const UART_SELECTIONS[] = {
390  "UART0", "UART1",
391 #if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
392  !defined(USE_ESP32_VARIANT_ESP32S2) && !defined(USE_ESP32_VARIANT_ESP32S3) && !defined(USE_ESP32_VARIANT_ESP32H2)
393  "UART2",
394 #endif // !USE_ESP32_VARIANT_ESP32C3 && !USE_ESP32_VARINT_ESP32C6 && !USE_ESP32_VARIANT_ESP32S2 &&
395  // !USE_ESP32_VARIANT_ESP32S3 && !USE_ESP32_VARIANT_ESP32H2
396 #if defined(USE_ESP_IDF)
397 #if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
398  "USB_CDC",
399 #endif // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3
400 #if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S3)
401  "USB_SERIAL_JTAG",
402 #endif // USE_ESP32_VARIANT_ESP32C3 || USE_ESP32_VARIANT_ESP32S3
403 #endif // USE_ESP_IDF
404 };
405 #endif // USE_ESP32
406 #ifdef USE_ESP8266
407 const char *const UART_SELECTIONS[] = {"UART0", "UART1", "UART0_SWAP"};
408 #endif // USE_ESP8266
409 #ifdef USE_RP2040
410 const char *const UART_SELECTIONS[] = {"UART0", "UART1", "USB_CDC"};
411 #endif // USE_RP2040
412 #ifdef USE_LIBRETINY
413 const char *const UART_SELECTIONS[] = {"DEFAULT", "UART0", "UART1", "UART2"};
414 #endif // USE_LIBRETINY
416  ESP_LOGCONFIG(TAG, "Logger:");
417  ESP_LOGCONFIG(TAG, " Level: %s", LOG_LEVELS[ESPHOME_LOG_LEVEL]);
418  ESP_LOGCONFIG(TAG, " Log Baud Rate: %" PRIu32, this->baud_rate_);
419 #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY)
420  ESP_LOGCONFIG(TAG, " Hardware UART: %s", UART_SELECTIONS[this->uart_]);
421 #endif
422 
423  for (auto &it : this->log_levels_) {
424  ESP_LOGCONFIG(TAG, " Level for '%s': %s", it.tag.c_str(), LOG_LEVELS[it.level]);
425  }
426 }
427 void Logger::write_footer_() { this->write_to_buffer_(ESPHOME_LOG_RESET_COLOR, strlen(ESPHOME_LOG_RESET_COLOR)); }
428 
429 Logger *global_logger = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
430 
431 } // namespace logger
432 } // namespace esphome
void set_baud_rate(uint32_t baud_rate)
Manually set the baud rate for serial, set to 0 to disable.
Definition: logger.cpp:374
void add_on_log_callback(std::function< void(int, const char *, const char *)> &&callback)
Register a callback that will be called for every log message sent.
Definition: logger.cpp:383
UARTSelection
Enum for logging UART selection.
Definition: logger.h:33
int level_for(const char *tag)
Definition: logger.cpp:161
void log_vprintf_(int level, const char *tag, int line, const char *format, va_list args)
Definition: logger.cpp:66
void vprintf_to_buffer_(const char *format, va_list args)
Definition: logger.h:140
void dump_config() override
Definition: logger.cpp:415
float get_setup_priority() const override
Definition: logger.cpp:386
Logger(uint32_t baud_rate, size_t tx_buffer_size)
Definition: logger.cpp:221
void write_header_(int level, const char *tag, int line)
Definition: logger.cpp:55
Logger * global_logger
Definition: logger.cpp:429
uart_port_t uart_num_
Definition: logger.h:176
const float BUS
For communication buses like i2c/spi.
Definition: component.cpp:15
UARTSelection get_uart() const
Get the UART used by the logger.
Definition: logger.cpp:380
void set_null_terminator_()
Definition: logger.h:127
void printf_to_buffer_(const char *format,...)
Definition: logger.h:155
void write_to_buffer_(char value)
Definition: logger.h:131
const char *const LOG_LEVELS[]
Definition: logger.cpp:387
UARTSelection uart_
Definition: logger.h:167
void pre_setup()
Set up this component.
Definition: logger.cpp:227
uint8_t progmem_read_byte(const uint8_t *addr)
Definition: core.cpp:55
std::string size_t len
Definition: helpers.h:292
const char *const UART_SELECTIONS[]
Definition: logger.cpp:389
void log_message_(int level, const char *tag, int offset=0)
Definition: logger.cpp:172
void init_usb_serial_jtag_()
Definition: logger.cpp:132
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< LogLevelOverride > log_levels_
Definition: logger.h:182
void set_log_level(const std::string &tag, int log_level)
Set the log level of the specified tag.
Definition: logger.cpp:375
bool recursion_guard_
Prevents recursive log calls, if true a log message is already being processed.
Definition: logger.h:185
CallbackManager< void(int, const char *, const char *)> log_callback_
Definition: logger.h:183
int HOT esp_idf_log_vprintf_(const char *format, va_list args)
Definition: log.cpp:50
bool is_buffer_full_() const
Definition: logger.h:124