Skip to content

Commit

Permalink
print task name if logger is called from other than main thread (#6630)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszduda23 committed May 5, 2024
1 parent f158420 commit 8796a4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
21 changes: 20 additions & 1 deletion esphome/components/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,23 @@ void Logger::write_header_(int level, const char *tag, int line) {

const char *color = LOG_LEVEL_COLORS[level];
const char *letter = LOG_LEVEL_LETTERS[level];
this->printf_to_buffer_("%s[%s][%s:%03u]: ", color, letter, tag, line);
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
TaskHandle_t current_task = xTaskGetCurrentTaskHandle();
#else
void *current_task = nullptr;
#endif
if (current_task == main_task_) {
this->printf_to_buffer_("%s[%s][%s:%03u]: ", color, letter, tag, line);
} else {
const char *thread_name = "";
#if defined(USE_ESP32)
thread_name = pcTaskGetName(current_task);
#elif defined(USE_LIBRETINY)
thread_name = pcTaskGetTaskName(current_task);
#endif
this->printf_to_buffer_("%s[%s][%s:%03u]%s[%s]%s: ", color, letter, tag, line,
ESPHOME_LOG_BOLD(ESPHOME_LOG_COLOR_RED), thread_name, color);
}
}

void HOT Logger::log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) { // NOLINT
Expand Down Expand Up @@ -127,6 +143,9 @@ void HOT Logger::log_message_(int level, const char *tag, int offset) {
Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size) : baud_rate_(baud_rate), tx_buffer_size_(tx_buffer_size) {
// add 1 to buffer size for null terminator
this->tx_buffer_ = new char[this->tx_buffer_size_ + 1]; // NOLINT
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
this->main_task_ = xTaskGetCurrentTaskHandle();
#endif
}

#ifdef USE_LOGGER_USB_CDC
Expand Down
1 change: 1 addition & 0 deletions esphome/components/logger/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class Logger : public Component {
CallbackManager<void(int, const char *, const char *)> log_callback_{};
/// Prevents recursive log calls, if true a log message is already being processed.
bool recursion_guard_ = false;
void *main_task_ = nullptr;
};

extern Logger *global_logger; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
Expand Down

0 comments on commit 8796a4c

Please sign in to comment.