ESPHome  2024.4.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  this->printf_to_buffer_("%s[%s][%s:%03u]: ", color, letter, tag, line);
43 }
44 
45 void HOT Logger::log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) { // NOLINT
46  if (level > this->level_for(tag) || recursion_guard_)
47  return;
48 
49  recursion_guard_ = true;
50  this->reset_buffer_();
51  this->write_header_(level, tag, line);
52  this->vprintf_to_buffer_(format, args);
53  this->write_footer_();
54  this->log_message_(level, tag);
55  recursion_guard_ = false;
56 }
57 #ifdef USE_STORE_LOG_STR_IN_FLASH
58 void Logger::log_vprintf_(int level, const char *tag, int line, const __FlashStringHelper *format,
59  va_list args) { // NOLINT
60  if (level > this->level_for(tag) || recursion_guard_)
61  return;
62 
63  recursion_guard_ = true;
64  this->reset_buffer_();
65  // copy format string
66  auto *format_pgm_p = reinterpret_cast<const uint8_t *>(format);
67  size_t len = 0;
68  char ch = '.';
69  while (!this->is_buffer_full_() && ch != '\0') {
70  this->tx_buffer_[this->tx_buffer_at_++] = ch = (char) progmem_read_byte(format_pgm_p++);
71  }
72  // Buffer full form copying format
73  if (this->is_buffer_full_())
74  return;
75 
76  // length of format string, includes null terminator
77  uint32_t offset = this->tx_buffer_at_;
78 
79  // now apply vsnprintf
80  this->write_header_(level, tag, line);
81  this->vprintf_to_buffer_(this->tx_buffer_, args);
82  this->write_footer_();
83  this->log_message_(level, tag, offset);
84  recursion_guard_ = false;
85 }
86 #endif
87 
88 int HOT Logger::level_for(const char *tag) {
89  // Uses std::vector<> for low memory footprint, though the vector
90  // could be sorted to minimize lookup times. This feature isn't used that
91  // much anyway so it doesn't matter too much.
92  for (auto &it : this->log_levels_) {
93  if (it.tag == tag) {
94  return it.level;
95  }
96  }
97  return ESPHOME_LOG_LEVEL;
98 }
99 
100 void HOT Logger::log_message_(int level, const char *tag, int offset) {
101  // remove trailing newline
102  if (this->tx_buffer_[this->tx_buffer_at_ - 1] == '\n') {
103  this->tx_buffer_at_--;
104  }
105  // make sure null terminator is present
106  this->set_null_terminator_();
107 
108  const char *msg = this->tx_buffer_ + offset;
109 
110  if (this->baud_rate_ > 0) {
111  this->write_msg_(msg);
112  }
113 
114 #ifdef USE_ESP32
115  // Suppress network-logging if memory constrained, but still log to serial
116  // ports. In some configurations (eg BLE enabled) there may be some transient
117  // memory exhaustion, and trying to log when OOM can lead to a crash. Skipping
118  // here usually allows the stack to recover instead.
119  // See issue #1234 for analysis.
120  if (xPortGetFreeHeapSize() < 2048)
121  return;
122 #endif
123 
124  this->log_callback_.call(level, tag, msg);
125 }
126 
127 Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size) : baud_rate_(baud_rate), tx_buffer_size_(tx_buffer_size) {
128  // add 1 to buffer size for null terminator
129  this->tx_buffer_ = new char[this->tx_buffer_size_ + 1]; // NOLINT
130 }
131 
132 #ifdef USE_LOGGER_USB_CDC
133 void Logger::loop() {
134 #ifdef USE_ARDUINO
135  if (this->uart_ != UART_SELECTION_USB_CDC) {
136  return;
137  }
138  static bool opened = false;
139  if (opened == Serial) {
140  return;
141  }
142  if (false == opened) {
144  }
145  opened = !opened;
146 #endif
147 }
148 #endif
149 
150 void Logger::set_baud_rate(uint32_t baud_rate) { this->baud_rate_ = baud_rate; }
151 void Logger::set_log_level(const std::string &tag, int log_level) {
152  this->log_levels_.push_back(LogLevelOverride{tag, log_level});
153 }
154 
155 #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY)
156 UARTSelection Logger::get_uart() const { return this->uart_; }
157 #endif
158 
159 void Logger::add_on_log_callback(std::function<void(int, const char *, const char *)> &&callback) {
160  this->log_callback_.add(std::move(callback));
161 }
162 float Logger::get_setup_priority() const { return setup_priority::BUS + 500.0f; }
163 const char *const LOG_LEVELS[] = {"NONE", "ERROR", "WARN", "INFO", "CONFIG", "DEBUG", "VERBOSE", "VERY_VERBOSE"};
164 
166  ESP_LOGCONFIG(TAG, "Logger:");
167  ESP_LOGCONFIG(TAG, " Level: %s", LOG_LEVELS[ESPHOME_LOG_LEVEL]);
168 #ifndef USE_HOST
169  ESP_LOGCONFIG(TAG, " Log Baud Rate: %" PRIu32, this->baud_rate_);
170  ESP_LOGCONFIG(TAG, " Hardware UART: %s", get_uart_selection_());
171 #endif
172 
173  for (auto &it : this->log_levels_) {
174  ESP_LOGCONFIG(TAG, " Level for '%s': %s", it.tag.c_str(), LOG_LEVELS[it.level]);
175  }
176 }
177 void Logger::write_footer_() { this->write_to_buffer_(ESPHOME_LOG_RESET_COLOR, strlen(ESPHOME_LOG_RESET_COLOR)); }
178 
179 Logger *global_logger = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
180 
181 } // namespace logger
182 } // 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:150
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:159
UARTSelection
Enum for logging UART selection.
Definition: logger.h:33
int level_for(const char *tag)
Definition: logger.cpp:88
void log_vprintf_(int level, const char *tag, int line, const char *format, va_list args)
Definition: logger.cpp:45
void vprintf_to_buffer_(const char *format, va_list args)
Definition: logger.h:120
void dump_config() override
Definition: logger.cpp:165
float get_setup_priority() const override
Definition: logger.cpp:162
Logger(uint32_t baud_rate, size_t tx_buffer_size)
Definition: logger.cpp:127
void write_header_(int level, const char *tag, int line)
Definition: logger.cpp:34
Logger * global_logger
Definition: logger.cpp:179
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:156
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:163
UARTSelection uart_
Definition: logger.h:151
void loop() override
Definition: logger.cpp:133
uint8_t progmem_read_byte(const uint8_t *addr)
Definition: core.cpp:55
std::string size_t len
Definition: helpers.h:292
void log_message_(int level, const char *tag, int offset=0)
Definition: logger.cpp:100
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:151
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