ESPHome  2024.12.2
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  // Uses std::vector<> for low memory footprint, though the vector
106  // could be sorted to minimize lookup times. This feature isn't used that
107  // much anyway so it doesn't matter too much.
108  for (auto &it : this->log_levels_) {
109  if (it.tag == tag) {
110  return it.level;
111  }
112  }
113  return ESPHOME_LOG_LEVEL;
114 }
115 
116 void HOT Logger::log_message_(int level, const char *tag, int offset) {
117  // remove trailing newline
118  if (this->tx_buffer_[this->tx_buffer_at_ - 1] == '\n') {
119  this->tx_buffer_at_--;
120  }
121  // make sure null terminator is present
122  this->set_null_terminator_();
123 
124  const char *msg = this->tx_buffer_ + offset;
125 
126  if (this->baud_rate_ > 0) {
127  this->write_msg_(msg);
128  }
129 
130 #ifdef USE_ESP32
131  // Suppress network-logging if memory constrained, but still log to serial
132  // ports. In some configurations (eg BLE enabled) there may be some transient
133  // memory exhaustion, and trying to log when OOM can lead to a crash. Skipping
134  // here usually allows the stack to recover instead.
135  // See issue #1234 for analysis.
136  if (xPortGetFreeHeapSize() < 2048)
137  return;
138 #endif
139 
140  this->log_callback_.call(level, tag, msg);
141 }
142 
143 Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size) : baud_rate_(baud_rate), tx_buffer_size_(tx_buffer_size) {
144  // add 1 to buffer size for null terminator
145  this->tx_buffer_ = new char[this->tx_buffer_size_ + 1]; // NOLINT
146 #if defined(USE_ESP32) || defined(USE_LIBRETINY)
147  this->main_task_ = xTaskGetCurrentTaskHandle();
148 #endif
149 }
150 
151 #ifdef USE_LOGGER_USB_CDC
152 void Logger::loop() {
153 #ifdef USE_ARDUINO
154  if (this->uart_ != UART_SELECTION_USB_CDC) {
155  return;
156  }
157  static bool opened = false;
158  if (opened == Serial) {
159  return;
160  }
161  if (false == opened) {
163  }
164  opened = !opened;
165 #endif
166 }
167 #endif
168 
169 void Logger::set_baud_rate(uint32_t baud_rate) { this->baud_rate_ = baud_rate; }
170 void Logger::set_log_level(const std::string &tag, int log_level) {
171  this->log_levels_.push_back(LogLevelOverride{tag, log_level});
172 }
173 
174 #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY)
175 UARTSelection Logger::get_uart() const { return this->uart_; }
176 #endif
177 
178 void Logger::add_on_log_callback(std::function<void(int, const char *, const char *)> &&callback) {
179  this->log_callback_.add(std::move(callback));
180 }
181 float Logger::get_setup_priority() const { return setup_priority::BUS + 500.0f; }
182 const char *const LOG_LEVELS[] = {"NONE", "ERROR", "WARN", "INFO", "CONFIG", "DEBUG", "VERBOSE", "VERY_VERBOSE"};
183 
185  ESP_LOGCONFIG(TAG, "Logger:");
186  ESP_LOGCONFIG(TAG, " Level: %s", LOG_LEVELS[ESPHOME_LOG_LEVEL]);
187 #ifndef USE_HOST
188  ESP_LOGCONFIG(TAG, " Log Baud Rate: %" PRIu32, this->baud_rate_);
189  ESP_LOGCONFIG(TAG, " Hardware UART: %s", get_uart_selection_());
190 #endif
191 
192  for (auto &it : this->log_levels_) {
193  ESP_LOGCONFIG(TAG, " Level for '%s': %s", it.tag.c_str(), LOG_LEVELS[it.level]);
194  }
195 }
196 void Logger::write_footer_() { this->write_to_buffer_(ESPHOME_LOG_RESET_COLOR, strlen(ESPHOME_LOG_RESET_COLOR)); }
197 
198 Logger *global_logger = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
199 
200 } // namespace logger
201 } // 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:169
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:178
UARTSelection
Enum for logging UART selection.
Definition: logger.h:33
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:120
void dump_config() override
Definition: logger.cpp:184
float get_setup_priority() const override
Definition: logger.cpp:181
Logger(uint32_t baud_rate, size_t tx_buffer_size)
Definition: logger.cpp:143
void write_header_(int level, const char *tag, int line)
Definition: logger.cpp:34
Logger * global_logger
Definition: logger.cpp:198
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:175
void set_null_terminator_()
Definition: logger.h:107
const char * get_uart_selection_()
void printf_to_buffer_(const char *format,...)
Definition: logger.h:135
void write_to_buffer_(char value)
Definition: logger.h:111
Application App
Global storage of Application pointer - only one Application can exist.
const char *const LOG_LEVELS[]
Definition: logger.cpp:182
UARTSelection uart_
Definition: logger.h:151
void loop() override
Definition: logger.cpp:152
uint8_t progmem_read_byte(const uint8_t *addr)
Definition: core.cpp:55
std::string size_t len
Definition: helpers.h:293
void log_message_(int level, const char *tag, int offset=0)
Definition: logger.cpp:116
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< LogLevelOverride > log_levels_
Definition: logger.h:166
void set_log_level(const std::string &tag, int log_level)
Set the log level of the specified tag.
Definition: logger.cpp:170
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:169
CallbackManager< void(int, const char *, const char *)> log_callback_
Definition: logger.h:167
bool is_buffer_full_() const
Definition: logger.h:104