mirror of
https://github.com/esphome/esphome.git
synced 2026-10-05 18:41:32 +00:00
[log] Detect early log calls before logger init and optimize hot path (#14538)
This commit is contained in:
+43
-17
@@ -1,6 +1,7 @@
|
||||
#include "log.h"
|
||||
#include "defines.h"
|
||||
#include "helpers.h"
|
||||
#include <cstdio>
|
||||
|
||||
#ifdef USE_LOGGER
|
||||
#include "esphome/components/logger/logger.h"
|
||||
@@ -8,40 +9,63 @@
|
||||
|
||||
namespace esphome {
|
||||
|
||||
#ifdef ESPHOME_DEBUG
|
||||
static void early_log_printf_(const char *tag, int line, const char *format, va_list args) {
|
||||
fprintf(stderr, "LOG BEFORE LOGGER INIT [%s:%d]: ", tag, line);
|
||||
vfprintf(stderr, format, args);
|
||||
fputc('\n', stderr);
|
||||
assert(false && "log called before Logger::pre_setup()"); // NOLINT
|
||||
}
|
||||
#endif
|
||||
|
||||
void HOT esp_log_printf_(int level, const char *tag, int line, const char *format, ...) { // NOLINT
|
||||
#ifdef USE_LOGGER
|
||||
#ifdef ESPHOME_DEBUG
|
||||
if (logger::global_logger == nullptr) {
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
early_log_printf_(tag, line, format, arg);
|
||||
va_end(arg);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
esp_log_vprintf_(level, tag, line, format, arg);
|
||||
logger::global_logger->log_vprintf_(static_cast<uint8_t>(level), tag, line, format, arg);
|
||||
va_end(arg);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_STORE_LOG_STR_IN_FLASH
|
||||
void HOT esp_log_printf_(int level, const char *tag, int line, const __FlashStringHelper *format, ...) {
|
||||
#ifdef USE_LOGGER
|
||||
ESPHOME_DEBUG_ASSERT(logger::global_logger != nullptr);
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
esp_log_vprintf_(level, tag, line, format, arg);
|
||||
logger::global_logger->log_vprintf_(static_cast<uint8_t>(level), tag, line, format, arg);
|
||||
va_end(arg);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
void HOT esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) { // NOLINT
|
||||
#ifdef USE_LOGGER
|
||||
auto *log = logger::global_logger;
|
||||
if (log == nullptr)
|
||||
#ifdef ESPHOME_DEBUG
|
||||
if (logger::global_logger == nullptr) {
|
||||
early_log_printf_(tag, line, format, args);
|
||||
return;
|
||||
|
||||
log->log_vprintf_(static_cast<uint8_t>(level), tag, line, format, args);
|
||||
}
|
||||
#endif
|
||||
logger::global_logger->log_vprintf_(static_cast<uint8_t>(level), tag, line, format, args);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_STORE_LOG_STR_IN_FLASH
|
||||
void HOT esp_log_vprintf_(int level, const char *tag, int line, const __FlashStringHelper *format,
|
||||
va_list args) { // NOLINT
|
||||
// Remove before 2026.9.0
|
||||
void HOT esp_log_vprintf_(int level, const char *tag, int line, const __FlashStringHelper *format, va_list args) {
|
||||
#ifdef USE_LOGGER
|
||||
auto *log = logger::global_logger;
|
||||
if (log == nullptr)
|
||||
return;
|
||||
|
||||
log->log_vprintf_(static_cast<uint8_t>(level), tag, line, format, args);
|
||||
ESPHOME_DEBUG_ASSERT(logger::global_logger != nullptr);
|
||||
logger::global_logger->log_vprintf_(static_cast<uint8_t>(level), tag, line, format, args);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -49,11 +73,13 @@ void HOT esp_log_vprintf_(int level, const char *tag, int line, const __FlashStr
|
||||
#ifdef USE_ESP32
|
||||
int HOT esp_idf_log_vprintf_(const char *format, va_list args) { // NOLINT
|
||||
#ifdef USE_LOGGER
|
||||
auto *log = logger::global_logger;
|
||||
if (log == nullptr)
|
||||
#ifdef ESPHOME_DEBUG
|
||||
if (logger::global_logger == nullptr) {
|
||||
early_log_printf_("esp-idf", 0, format, args);
|
||||
return 0;
|
||||
|
||||
log->log_vprintf_(ESPHOME_LOG_LEVEL, "esp-idf", 0, format, args);
|
||||
}
|
||||
#endif
|
||||
logger::global_logger->log_vprintf_(ESPHOME_LOG_LEVEL, "esp-idf", 0, format, args);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user