ESPHome  2025.2.0
logger.cpp
Go to the documentation of this file.
1 #include "logger.h"
2 #include <cinttypes>
3 
4 #include "esphome/core/hal.h"
5 #include "esphome/core/log.h"
7 
8 namespace esphome {
9 namespace logger {
10 
11 static const char *const TAG = "logger";
12 
13 static const char *const LOG_LEVEL_COLORS[] = {
14  "", // NONE
15  ESPHOME_LOG_BOLD(ESPHOME_LOG_COLOR_RED), // ERROR
16  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_YELLOW), // WARNING
17  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_GREEN), // INFO
18  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_MAGENTA), // CONFIG
19  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_CYAN), // DEBUG
20  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_GRAY), // VERBOSE
21  ESPHOME_LOG_COLOR(ESPHOME_LOG_COLOR_WHITE), // VERY_VERBOSE
22 };
23 static const char *const LOG_LEVEL_LETTERS[] = {
24  "", // NONE
25  "E", // ERROR
26  "W", // WARNING
27  "I", // INFO
28  "C", // CONFIG
29  "D", // DEBUG
30  "V", // VERBOSE
31  "VV", // VERY_VERBOSE
32 };
33 
34 void Logger::write_header_(int level, const char *tag, int line) {
35  if (level < 0)
36  level = 0;
37  if (level > 7)
38  level = 7;
39 
40  const char *color = LOG_LEVEL_COLORS[level];
41  const char *letter = LOG_LEVEL_LETTERS[level];
42 #if defined(USE_ESP32) || defined(USE_LIBRETINY)
43  TaskHandle_t current_task = xTaskGetCurrentTaskHandle();
44 #else
45  void *current_task = nullptr;
46 #endif
47  if (current_task == main_task_) {
48  this->printf_to_buffer_("%s[%s][%s:%03u]: ", color, letter, tag, line);
49  } else {
50  const char *thread_name = ""; // NOLINT(clang-analyzer-deadcode.DeadStores)
51 #if defined(USE_ESP32)
52  thread_name = pcTaskGetName(current_task);
53 #elif defined(USE_LIBRETINY)
54  thread_name = pcTaskGetTaskName(current_task);
55 #endif
56  this->printf_to_buffer_("%s[%s][%s:%03u]%s[%s]%s: ", color, letter, tag, line,
57  ESPHOME_LOG_BOLD(ESPHOME_LOG_COLOR_RED), thread_name, color);
58  }
59 }
60 
61 void HOT Logger::log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) { // NOLINT
62  if (level > this->level_for(tag) || recursion_guard_)
63  return;
64 
65  recursion_guard_ = true;
66  this->reset_buffer_();
67  this->write_header_(level, tag, line);
68  this->vprintf_to_buffer_(format, args);
69  this->write_footer_();
70  this->log_message_(level, tag);
71  recursion_guard_ = false;
72 }
73 #ifdef USE_STORE_LOG_STR_IN_FLASH
74 void Logger::log_vprintf_(int level, const char *tag, int line, const __FlashStringHelper *format,
75  va_list args) { // NOLINT
76  if (level > this->level_for(tag) || recursion_guard_)
77  return;
78 
79  recursion_guard_ = true;
80  this->reset_buffer_();
81  // copy format string
82  auto *format_pgm_p = reinterpret_cast<const uint8_t *>(format);
83  size_t len = 0;
84  char ch = '.';
85  while (!this->is_buffer_full_() && ch != '\0') {
86  this->tx_buffer_[this->tx_buffer_at_++] = ch = (char) progmem_read_byte(format_pgm_p++);
87  }
88  // Buffer full form copying format
89  if (this->is_buffer_full_())
90  return;
91 
92  // length of format string, includes null terminator
93  uint32_t offset = this->tx_buffer_at_;
94 
95  // now apply vsnprintf
96  this->write_header_(level, tag, line);
97  this->vprintf_to_buffer_(this->tx_buffer_, args);
98  this->write_footer_();
99  this->log_message_(level, tag, offset);
100  recursion_guard_ = false;
101 }
102 #endif
103 
104 int HOT Logger::level_for(const char *tag) {
105  if (this->log_levels_.count(tag) != 0)
106  return this->log_levels_[tag];
107  return this->current_level_;
108 }
109 
110 void HOT Logger::log_message_(int level, const char *tag, int offset) {
111  // remove trailing newline
112  if (this->tx_buffer_[this->tx_buffer_at_ - 1] == '\n') {
113  this->tx_buffer_at_--;
114  }
115  // make sure null terminator is present
116  this->set_null_terminator_();
117 
118  const char *msg = this->tx_buffer_ + offset;
119 
120  if (this->baud_rate_ > 0) {
121  this->write_msg_(msg);
122  }
123 
124 #ifdef USE_ESP32
125  // Suppress network-logging if memory constrained, but still log to serial
126  // ports. In some configurations (eg BLE enabled) there may be some transient
127  // memory exhaustion, and trying to log when OOM can lead to a crash. Skipping
128  // here usually allows the stack to recover instead.
129  // See issue #1234 for analysis.
130  if (xPortGetFreeHeapSize() < 2048)
131  return;
132 #endif
133 
134  this->log_callback_.call(level, tag, msg);
135 }
136 
137 Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size) : baud_rate_(baud_rate), tx_buffer_size_(tx_buffer_size) {
138  // add 1 to buffer size for null terminator
139  this->tx_buffer_ = new char[this->tx_buffer_size_ + 1]; // NOLINT
140 #if defined(USE_ESP32) || defined(USE_LIBRETINY)
141  this->main_task_ = xTaskGetCurrentTaskHandle();
142 #endif
143 }
144 
145 #ifdef USE_LOGGER_USB_CDC
146 void Logger::loop() {
147 #ifdef USE_ARDUINO
148  if (this->uart_ != UART_SELECTION_USB_CDC) {
149  return;
150  }
151  static bool opened = false;
152  if (opened == Serial) {
153  return;
154  }
155  if (false == opened) {
157  }
158  opened = !opened;
159 #endif
160 }
161 #endif
162 
163 void Logger::set_baud_rate(uint32_t baud_rate) { this->baud_rate_ = baud_rate; }
164 void Logger::set_log_level(const std::string &tag, int log_level) { this->log_levels_[tag] = log_level; }
165 
166 #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY)
167 UARTSelection Logger::get_uart() const { return this->uart_; }
168 #endif
169 
170 void Logger::add_on_log_callback(std::function<void(int, const char *, const char *)> &&callback) {
171  this->log_callback_.add(std::move(callback));
172 }
173 float Logger::get_setup_priority() const { return setup_priority::BUS + 500.0f; }
174 const char *const LOG_LEVELS[] = {"NONE", "ERROR", "WARN", "INFO", "CONFIG", "DEBUG", "VERBOSE", "VERY_VERBOSE"};
175 
177  ESP_LOGCONFIG(TAG, "Logger:");
178  ESP_LOGCONFIG(TAG, " Max Level: %s", LOG_LEVELS[ESPHOME_LOG_LEVEL]);
179  ESP_LOGCONFIG(TAG, " Initial Level: %s", LOG_LEVELS[this->current_level_]);
180 #ifndef USE_HOST
181  ESP_LOGCONFIG(TAG, " Log Baud Rate: %" PRIu32, this->baud_rate_);
182  ESP_LOGCONFIG(TAG, " Hardware UART: %s", get_uart_selection_());
183 #endif
184 
185  for (auto &it : this->log_levels_) {
186  ESP_LOGCONFIG(TAG, " Level for '%s': %s", it.first.c_str(), LOG_LEVELS[it.second]);
187  }
188 }
189 void Logger::write_footer_() { this->write_to_buffer_(ESPHOME_LOG_RESET_COLOR, strlen(ESPHOME_LOG_RESET_COLOR)); }
190 
191 void Logger::set_log_level(int level) {
192  if (level > ESPHOME_LOG_LEVEL) {
193  level = ESPHOME_LOG_LEVEL;
194  ESP_LOGW(TAG, "Cannot set log level higher than pre-compiled %s", LOG_LEVELS[ESPHOME_LOG_LEVEL]);
195  }
196  this->current_level_ = level;
197  this->level_callback_.call(level);
198 }
199 
200 Logger *global_logger = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
201 
202 } // namespace logger
203 } // 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:163
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:170
CallbackManager< void(int)> level_callback_
Definition: logger.h:175
UARTSelection
Enum for logging UART selection.
Definition: logger.h:34
int level_for(const char *tag)
Definition: logger.cpp:104
void log_vprintf_(int level, const char *tag, int line, const char *format, va_list args)
Definition: logger.cpp:61
void vprintf_to_buffer_(const char *format, va_list args)
Definition: logger.h:127
void dump_config() override
Definition: logger.cpp:176
std::map< std::string, int > log_levels_
Definition: logger.h:169
float get_setup_priority() const override
Definition: logger.cpp:173
Logger(uint32_t baud_rate, size_t tx_buffer_size)
Definition: logger.cpp:137
void write_header_(int level, const char *tag, int line)
Definition: logger.cpp:34
Logger * global_logger
Definition: logger.cpp:200
const float BUS
For communication buses like i2c/spi.
Definition: component.cpp:16
UARTSelection get_uart() const
Get the UART used by the logger.
Definition: logger.cpp:167
void set_null_terminator_()
Definition: logger.h:114
const char * get_uart_selection_()
void printf_to_buffer_(const char *format,...)
Definition: logger.h:142
void write_to_buffer_(char value)
Definition: logger.h:118
Application App
Global storage of Application pointer - only one Application can exist.
const char *const LOG_LEVELS[]
Definition: logger.cpp:174
UARTSelection uart_
Definition: logger.h:158
void loop() override
Definition: logger.cpp:146
uint8_t progmem_read_byte(const uint8_t *addr)
Definition: core.cpp:55
std::string size_t len
Definition: helpers.h:301
void log_message_(int level, const char *tag, int offset=0)
Definition: logger.cpp:110
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void write_msg_(const char *msg)
bool recursion_guard_
Prevents recursive log calls, if true a log message is already being processed.
Definition: logger.h:173
CallbackManager< void(int, const char *, const char *)> log_callback_
Definition: logger.h:170
bool is_buffer_full_() const
Definition: logger.h:111
void set_log_level(int level)
Set the default log level for this logger.
Definition: logger.cpp:191