From 148f5d94180473df7e83937b63b46504d71d7db6 Mon Sep 17 00:00:00 2001 From: buxtronix Date: Sat, 27 Jun 2020 08:13:23 +1000 Subject: [PATCH] ESP32: Conditionally log on services to avoid OOM crashes (#1098) Also removed new line formatting tidy as it requires a bit of memory which might not be available. * Use suitably scoped char for log newline stripping * fix formatting * Better fix for OOM logging crash * Limit to ESP32 only * format changes --- esphome/components/logger/logger.cpp | 10 ++++++++++ esphome/core/log.cpp | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index bc6951c9b9..140b8f26c1 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -103,7 +103,17 @@ void HOT Logger::log_message_(int level, const char *tag, int offset) { const char *msg = this->tx_buffer_ + offset; if (this->baud_rate_ > 0) this->hw_serial_->println(msg); +#ifdef ARDUINO_ARCH_ESP32 + // Suppress network-logging if memory constrained, but still log to serial + // ports. In some configurations (eg BLE enabled) there may be some transient + // memory exhaustion, and trying to log when OOM can lead to a crash. Skipping + // here usually allows the stack to recover instead. + // See issue #1234 for analysis. + if (xPortGetFreeHeapSize() > 2048) + this->log_callback_.call(level, tag, msg); +#else this->log_callback_.call(level, tag, msg); +#endif } Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size, UARTSelection uart) diff --git a/esphome/core/log.cpp b/esphome/core/log.cpp index 15d49c0038..9b49a4c6ba 100644 --- a/esphome/core/log.cpp +++ b/esphome/core/log.cpp @@ -53,16 +53,6 @@ int HOT esp_idf_log_vprintf_(const char *format, va_list args) { // NOLINT if (log == nullptr) return 0; - size_t len = strlen(format); - if (format[len - 1] == '\n') { - // Remove trailing newline from format - // Use locally stored - static std::string FORMAT_COPY; - FORMAT_COPY.clear(); - FORMAT_COPY.insert(0, format, len - 1); - format = FORMAT_COPY.c_str(); - } - log->log_vprintf_(ESPHOME_LOG_LEVEL, "esp-idf", 0, format, args); #endif return 0;