ESPHome  2024.10.2
logger.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdarg>
4 #include <vector>
7 #include "esphome/core/defines.h"
8 #include "esphome/core/helpers.h"
9 
10 #ifdef USE_ARDUINO
11 #if defined(USE_ESP8266) || defined(USE_ESP32)
12 #include <HardwareSerial.h>
13 #endif // USE_ESP8266 || USE_ESP32
14 #ifdef USE_RP2040
15 #include <HardwareSerial.h>
16 #include <SerialUSB.h>
17 #endif // USE_RP2040
18 #endif // USE_ARDUINO
19 
20 #ifdef USE_ESP_IDF
21 #include <driver/uart.h>
22 #endif // USE_ESP_IDF
23 
24 namespace esphome {
25 
26 namespace logger {
27 
28 #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY)
29 
34 #ifdef USE_LIBRETINY
37 #else
39 #endif
41 #if defined(USE_LIBRETINY) || defined(USE_ESP32_VARIANT_ESP32)
43 #endif
44 #ifdef USE_LOGGER_USB_CDC
46 #endif
47 #ifdef USE_LOGGER_USB_SERIAL_JTAG
49 #endif
50 #ifdef USE_ESP8266
52 #endif // USE_ESP8266
53 };
54 #endif // USE_ESP32 || USE_ESP8266 || USE_RP2040 || USE_LIBRETINY
55 
56 class Logger : public Component {
57  public:
58  explicit Logger(uint32_t baud_rate, size_t tx_buffer_size);
59 #ifdef USE_LOGGER_USB_CDC
60  void loop() override;
61 #endif
62  void set_baud_rate(uint32_t baud_rate);
64  uint32_t get_baud_rate() const { return baud_rate_; }
65 #ifdef USE_ARDUINO
66  Stream *get_hw_serial() const { return hw_serial_; }
67 #endif
68 #ifdef USE_ESP_IDF
69  uart_port_t get_uart_num() const { return uart_num_; }
70 #endif
71 #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY)
72  void set_uart_selection(UARTSelection uart_selection) { uart_ = uart_selection; }
74  UARTSelection get_uart() const;
75 #endif
76 
78  void set_log_level(const std::string &tag, int log_level);
79 
80  // ========== INTERNAL METHODS ==========
81  // (In most use cases you won't need these)
83  void pre_setup();
84  void dump_config() override;
85 
86  int level_for(const char *tag);
87 
89  void add_on_log_callback(std::function<void(int, const char *, const char *)> &&callback);
90 
91  float get_setup_priority() const override;
92 
93  void log_vprintf_(int level, const char *tag, int line, const char *format, va_list args); // NOLINT
94 #ifdef USE_STORE_LOG_STR_IN_FLASH
95  void log_vprintf_(int level, const char *tag, int line, const __FlashStringHelper *format, va_list args); // NOLINT
96 #endif
97 
98  protected:
99  void write_header_(int level, const char *tag, int line);
100  void write_footer_();
101  void log_message_(int level, const char *tag, int offset = 0);
102  void write_msg_(const char *msg);
103 
104  inline bool is_buffer_full_() const { return this->tx_buffer_at_ >= this->tx_buffer_size_; }
105  inline int buffer_remaining_capacity_() const { return this->tx_buffer_size_ - this->tx_buffer_at_; }
106  inline void reset_buffer_() { this->tx_buffer_at_ = 0; }
107  inline void set_null_terminator_() {
108  // does not increment buffer_at
109  this->tx_buffer_[this->tx_buffer_at_] = '\0';
110  }
111  inline void write_to_buffer_(char value) {
112  if (!this->is_buffer_full_())
113  this->tx_buffer_[this->tx_buffer_at_++] = value;
114  }
115  inline void write_to_buffer_(const char *value, int length) {
116  for (int i = 0; i < length && !this->is_buffer_full_(); i++) {
117  this->tx_buffer_[this->tx_buffer_at_++] = value[i];
118  }
119  }
120  inline void vprintf_to_buffer_(const char *format, va_list args) {
121  if (this->is_buffer_full_())
122  return;
123  int remaining = this->buffer_remaining_capacity_();
124  int ret = vsnprintf(this->tx_buffer_ + this->tx_buffer_at_, remaining, format, args);
125  if (ret < 0) {
126  // Encoding error, do not increment buffer_at
127  return;
128  }
129  if (ret >= remaining) {
130  // output was too long, truncated
131  ret = remaining;
132  }
133  this->tx_buffer_at_ += ret;
134  }
135  inline void printf_to_buffer_(const char *format, ...) {
136  va_list arg;
137  va_start(arg, format);
138  this->vprintf_to_buffer_(format, arg);
139  va_end(arg);
140  }
141 
142 #ifndef USE_HOST
143  const char *get_uart_selection_();
144 #endif
145 
146  uint32_t baud_rate_;
147  char *tx_buffer_{nullptr};
150 #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040)
152 #endif
153 #ifdef USE_LIBRETINY
155 #endif
156 #ifdef USE_ARDUINO
157  Stream *hw_serial_{nullptr};
158 #endif
159 #ifdef USE_ESP_IDF
160  uart_port_t uart_num_;
161 #endif
163  std::string tag;
164  int level;
165  };
166  std::vector<LogLevelOverride> log_levels_;
169  bool recursion_guard_ = false;
170  void *main_task_ = nullptr;
171 };
172 
173 extern Logger *global_logger; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
174 
175 class LoggerMessageTrigger : public Trigger<int, const char *, const char *> {
176  public:
177  explicit LoggerMessageTrigger(Logger *parent, int level) {
178  this->level_ = level;
179  parent->add_on_log_callback([this](int level, const char *tag, const char *message) {
180  if (level <= this->level_) {
181  this->trigger(level, tag, message);
182  }
183  });
184  }
185 
186  protected:
187  int level_;
188 };
189 
190 } // namespace logger
191 
192 } // 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
void set_uart_selection(UARTSelection uart_selection)
Definition: logger.h:72
int level_for(const char *tag)
Definition: logger.cpp:104
LoggerMessageTrigger(Logger *parent, int level)
Definition: logger.h:177
void log_vprintf_(int level, const char *tag, int line, const char *format, va_list args)
Definition: logger.cpp:61
int buffer_remaining_capacity_() const
Definition: logger.h:105
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
void write_to_buffer_(const char *value, int length)
Definition: logger.h:115
uart_port_t uart_num_
Definition: logger.h:160
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
UARTSelection uart_
Definition: logger.h:151
void loop() override
Definition: logger.cpp:152
void pre_setup()
Set up this component.
Stream * get_hw_serial() const
Definition: logger.h:66
uint32_t get_baud_rate() const
Definition: logger.h:64
uint16_t length
Definition: tt21100.cpp:12
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
uart_port_t get_uart_num() const
Definition: logger.h:69
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