ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
logger.cpp
Go to the documentation of this file.
1#include "logger.h"
2#include <cinttypes>
3
5#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
8
9namespace esphome::logger {
10
11static const char *const TAG = "logger";
12
13#if defined(USE_ESP32) || defined(USE_HOST) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
14// Implementation for multi-threaded platforms (ESP32 with FreeRTOS, Host with pthreads, LibreTiny with FreeRTOS,
15// Zephyr) Main thread/task always uses direct buffer access for console output and callbacks
16//
17// For non-main threads/tasks:
18// - WITH task log buffer: Prefer sending to ring buffer for async processing
19// - Avoids allocating stack memory for console output in normal operation
20// - Prevents console corruption from concurrent writes by multiple threads
21// - Messages are serialized through main loop for proper console output
22// - Fallback to emergency console logging only if ring buffer is full
23// - WITHOUT task log buffer: Only emergency console output, no callbacks
24//
25// Optimized for the common case: 99.9% of logs come from the main thread
26void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const char *format, va_list args) { // NOLINT
27 if (level > this->level_for(tag))
28 return;
29
30#if defined(USE_ESP32) || defined(USE_LIBRETINY)
31 // Get task handle once - used for both main task check and passing to non-main thread handler
32 TaskHandle_t current_task = xTaskGetCurrentTaskHandle();
33 const bool is_main_task = (current_task == this->main_task_);
34#elif (USE_ZEPHYR)
35 k_tid_t current_task = k_current_get();
36 const bool is_main_task = (current_task == this->main_task_);
37#else // USE_HOST
38 const bool is_main_task = pthread_equal(pthread_self(), this->main_thread_);
39#endif
40
41 // Fast path: main thread, no recursion (99.9% of all logs)
42 // Pass nullptr for thread_name since we already know this is the main task
43 if (is_main_task && !this->main_task_recursion_guard_) [[likely]] {
45 return;
46 }
47
48 // Main task with recursion - silently drop to prevent infinite loop
49 if (is_main_task) {
50 return;
51 }
52
53 // Non-main thread handling (~0.1% of logs)
54 // Resolve thread name once and pass it through the logging chain.
55 // ESP32/LibreTiny: use TaskHandle_t overload to avoid redundant xTaskGetCurrentTaskHandle()
56 // (we already have the handle from the main task check above).
57 // Host: pass a stack buffer for pthread_getname_np to write into.
58#if defined(USE_ESP32) || defined(USE_LIBRETINY)
59 const char *thread_name = get_thread_name_(current_task);
60#elif defined(USE_ZEPHYR)
61 char thread_name_buf[MAX_POINTER_REPRESENTATION];
62 const char *thread_name = get_thread_name_(thread_name_buf, current_task);
63#else // USE_HOST
64 char thread_name_buf[THREAD_NAME_BUF_SIZE];
65 const char *thread_name = this->get_thread_name_(thread_name_buf);
66#endif
67 this->log_vprintf_non_main_thread_(level, tag, line, format, args, thread_name);
68}
69
70// Handles non-main thread logging only
71// Kept separate from hot path to improve instruction cache performance
72void Logger::log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args,
73 const char *thread_name) {
74 // Check if already in recursion for this non-main thread/task
75 if (this->is_non_main_task_recursive_()) {
76 return;
77 }
78
79 // RAII guard - automatically resets on any return path
80 auto guard = this->make_non_main_task_guard_();
81
82 bool message_sent = false;
83#ifdef USE_ESPHOME_TASK_LOG_BUFFER
84 // For non-main threads/tasks, queue the message for callbacks
85 message_sent =
86 this->log_buffer_.send_message_thread_safe(level, tag, static_cast<uint16_t>(line), thread_name, format, args);
87 if (message_sent) {
88 // Enable logger loop to process the buffered message
89 // This is safe to call from any context including ISRs
91 }
92#endif
93 // Emergency console logging for non-main threads when ring buffer is full or disabled
94 // This is a fallback mechanism to ensure critical log messages are visible
95 // Note: This may cause interleaved/corrupted console output if multiple threads
96 // log simultaneously, but it's better than losing important messages entirely
97#ifdef USE_HOST
98 if (!message_sent)
99#else
100 if (!message_sent && this->baud_rate_ > 0) // If logging is enabled, write to console
101#endif
102 {
103#ifdef USE_HOST
104 // Host always has console output - no baud_rate check needed
105 static const size_t MAX_CONSOLE_LOG_MSG_SIZE = 512;
106#else
107 // Maximum size for console log messages (includes null terminator)
108 static const size_t MAX_CONSOLE_LOG_MSG_SIZE = 144;
109#endif
110 char console_buffer[MAX_CONSOLE_LOG_MSG_SIZE]; // MUST be stack allocated for thread safety
111 LogBuffer buf{console_buffer, MAX_CONSOLE_LOG_MSG_SIZE};
112 this->format_log_to_buffer_with_terminator_(level, tag, line, format, args, buf, thread_name);
113 this->write_to_console_(buf);
114 }
115
116 // RAII guard automatically resets on return
117}
118#else
119// Implementation for single-task platforms (ESP8266, RP2040)
120// Logging calls are NOT thread-safe: global_recursion_guard_ is a plain bool and tx_buffer_ has no locking.
121// Not a problem in practice yet since Zephyr has no API support (logs are console-only).
122void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const char *format, va_list args) { // NOLINT
123 if (level > this->level_for(tag) || global_recursion_guard_)
124 return;
125 // Other single-task platforms don't have thread names, so pass nullptr
127}
128#endif // USE_ESP32 || USE_HOST || USE_LIBRETINY || USE_ZEPHYR
129
130#ifdef USE_STORE_LOG_STR_IN_FLASH
131// Implementation for ESP8266 with flash string support.
132// Note: USE_STORE_LOG_STR_IN_FLASH is only defined for ESP8266.
133//
134// This function handles format strings stored in flash memory (PROGMEM) to save RAM.
135// Uses vsnprintf_P to read the format string directly from flash without copying to RAM.
136//
137void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format,
138 va_list args) { // NOLINT
139 if (level > this->level_for(tag) || global_recursion_guard_)
140 return;
141
143}
144#endif // USE_STORE_LOG_STR_IN_FLASH
145
146inline uint8_t Logger::level_for(const char *tag) {
147#ifdef USE_LOGGER_RUNTIME_TAG_LEVELS
148 auto it = this->log_levels_.find(tag);
149 if (it != this->log_levels_.end())
150 return it->second;
151#endif
152 return this->current_level_;
153}
154
155Logger::Logger(uint32_t baud_rate) : baud_rate_(baud_rate) {
156#if defined(USE_ESP32) || defined(USE_LIBRETINY)
157 this->main_task_ = xTaskGetCurrentTaskHandle();
158#elif defined(USE_ZEPHYR)
159 this->main_task_ = k_current_get();
160#elif defined(USE_HOST)
161 this->main_thread_ = pthread_self();
162#endif
163}
164
165#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || (defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
167 this->process_messages_();
168#if defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
169 this->cdc_loop_();
170#endif
171}
172#endif
173
175#ifdef USE_ESPHOME_TASK_LOG_BUFFER
176 // Process any buffered messages when available
177 if (this->log_buffer_.has_messages()) {
179 uint16_t text_length;
180 while (this->log_buffer_.borrow_message_main_loop(message, text_length)) {
181 const char *thread_name = message->thread_name[0] != '\0' ? message->thread_name : nullptr;
182 LogBuffer buf{this->tx_buffer_, ESPHOME_LOGGER_TX_BUFFER_SIZE};
183 this->format_buffered_message_and_notify_(message->level, message->tag, message->line, thread_name,
184 message->text_data(), text_length, buf);
185 // Release the message to allow other tasks to use it as soon as possible
188 }
189 }
190#if !(defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
191 else {
192 // No messages to process, disable loop if appropriate
193 // This reduces overhead when there's no async logging activity
195 }
196#endif
197#endif // USE_ESPHOME_TASK_LOG_BUFFER
198}
199
200void Logger::set_baud_rate(uint32_t baud_rate) { this->baud_rate_ = baud_rate; }
201#ifdef USE_LOGGER_RUNTIME_TAG_LEVELS
202void Logger::set_log_level(const char *tag, uint8_t log_level) { this->log_levels_[tag] = log_level; }
203#endif
204
205#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
206UARTSelection Logger::get_uart() const { return this->uart_; }
207#endif
208
209float Logger::get_setup_priority() const { return setup_priority::BUS + 500.0f; }
210
211// Log level strings - packed into flash on ESP8266, indexed by log level (0-7)
212PROGMEM_STRING_TABLE(LogLevelStrings, "NONE", "ERROR", "WARN", "INFO", "CONFIG", "DEBUG", "VERBOSE", "VERY_VERBOSE");
213
214static const LogString *get_log_level_str(uint8_t level) {
215 return LogLevelStrings::get_log_str(level, LogLevelStrings::LAST_INDEX);
216}
217
219 ESP_LOGCONFIG(TAG,
220 "Logger:\n"
221 " Max Level: %s\n"
222 " Initial Level: %s",
223 LOG_STR_ARG(get_log_level_str(ESPHOME_LOG_LEVEL)),
224 LOG_STR_ARG(get_log_level_str(this->current_level_)));
225#ifndef USE_HOST
226 ESP_LOGCONFIG(TAG,
227 " Log Baud Rate: %" PRIu32 "\n"
228 " Hardware UART: %s",
229 this->baud_rate_, LOG_STR_ARG(get_uart_selection_()));
230#endif
231#ifdef USE_ESPHOME_TASK_LOG_BUFFER
232#ifdef USE_HOST
233 ESP_LOGCONFIG(TAG, " Task Log Buffer Slots: %u", static_cast<unsigned int>(this->log_buffer_.size()));
234#else
235 ESP_LOGCONFIG(TAG, " Task Log Buffer Size: %u bytes", static_cast<unsigned int>(this->log_buffer_.size()));
236#endif
237#endif
238
239#ifdef USE_LOGGER_RUNTIME_TAG_LEVELS
240 for (auto &it : this->log_levels_) {
241 ESP_LOGCONFIG(TAG, " Level for '%s': %s", it.first, LOG_STR_ARG(get_log_level_str(it.second)));
242 }
243#endif
244#ifdef USE_ZEPHYR
245 dump_crash_();
246#endif
247 // Warn users that VERBOSE/VERY_VERBOSE logging impacts performance.
248 // Only the compiled log level matters — all log calls up to this level
249 // are in the binary and will be formatted (vsnprintf) and block UART.
250#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
251 ESP_LOGW(TAG, "VERY_VERBOSE logging is active — significant performance impact, short-term debugging only\n"
252 " May cause connection instability. Set log level to DEBUG or lower for long-term use.");
253#elif ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
254 ESP_LOGI(TAG, "VERBOSE logging is active — performance impact, short-term debugging only\n"
255 " Set log level to DEBUG or lower for long-term use.");
256#endif
257}
258
259void Logger::set_log_level(uint8_t level) {
260 if (level > ESPHOME_LOG_LEVEL) {
261 level = ESPHOME_LOG_LEVEL;
262 ESP_LOGW(TAG, "Cannot set log level higher than pre-compiled %s",
263 LOG_STR_ARG(get_log_level_str(ESPHOME_LOG_LEVEL)));
264 }
265 this->current_level_ = level;
266#ifdef USE_LOGGER_LEVEL_LISTENERS
267 for (auto *listener : this->level_listeners_)
268 listener->on_log_level_change(level);
269#endif
270}
271
272Logger *global_logger = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
273
274} // namespace esphome::logger
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
Logger component for all ESPHome logging.
Definition logger.h:144
UARTSelection uart_
Definition logger.h:355
const char *HOT get_thread_name_()
Definition logger.h:392
void dump_config() override
Definition logger.cpp:218
bool HOT is_non_main_task_recursive_() const
Definition logger.h:446
NonMainTaskRecursionGuard make_non_main_task_guard_()
Definition logger.h:449
const LogString * get_uart_selection_()
uint8_t level_for(const char *tag)
Definition logger.cpp:146
void loop() override
Definition logger.cpp:166
void log_vprintf_(uint8_t level, const char *tag, int line, const char *format, va_list args)
Definition logger.cpp:26
void HOT log_message_to_buffer_and_send_(bool &recursion_guard, uint8_t level, const char *tag, int line, FormatType format, va_list args, const char *thread_name)
Definition logger.h:282
char tx_buffer_[ESPHOME_LOGGER_TX_BUFFER_SIZE+1]
Definition logger.h:370
void HOT format_buffered_message_and_notify_(uint8_t level, const char *tag, uint16_t line, const char *thread_name, const char *text, uint16_t text_length, LogBuffer &buf)
Definition logger.h:301
float get_setup_priority() const override
Definition logger.cpp:209
std::map< const char *, uint8_t, CStrCompare > log_levels_
Definition logger.h:343
UARTSelection get_uart() const
Get the UART used by the logger.
Definition logger.cpp:206
pthread_t main_thread_
Definition logger.h:327
std::vector< LoggerLevelListener * > level_listeners_
Definition logger.h:350
void disable_loop_when_buffer_empty_()
Definition logger.h:472
void HOT format_log_to_buffer_with_terminator_(uint8_t level, const char *tag, int line, const char *format, va_list args, LogBuffer &buf, const char *thread_name)
Definition logger.h:241
void HOT write_log_buffer_to_console_(LogBuffer &buf)
Definition logger.h:273
void set_log_level(uint8_t level)
Set the default log level for this logger.
Definition logger.cpp:259
void set_baud_rate(uint32_t baud_rate)
Manually set the baud rate for serial, set to 0 to disable.
Definition logger.cpp:200
void HOT write_to_console_(LogBuffer &buf)
Definition logger.h:267
void log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args, const char *thread_name)
Definition logger.cpp:72
logger::TaskLogBuffer log_buffer_
Definition logger.h:372
bool main_task_recursion_guard_
Definition logger.h:361
Logger(uint32_t baud_rate)
Definition logger.cpp:155
bool borrow_message_main_loop(LogMessage *&message, uint16_t &text_length)
bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name, const char *format, va_list args)
const char * message
Definition component.cpp:35
UARTSelection
Enum for logging UART selection.
Definition logger.h:104
Logger * global_logger
Definition logger.cpp:272
PROGMEM_STRING_TABLE(LogLevelStrings, "NONE", "ERROR", "WARN", "INFO", "CONFIG", "DEBUG", "VERBOSE", "VERY_VERBOSE")
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:36
const char int line
Definition log.h:74
const char * tag
Definition log.h:74
const char int const __FlashStringHelper * format
Definition log.h:74
const char int const __FlashStringHelper va_list args
Definition log.h:74
static void uint32_t