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
This commit is contained in:
buxtronix 2020-06-27 08:13:23 +10:00 committed by GitHub
parent 82d5d50d61
commit 148f5d9418
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 10 deletions

View file

@ -103,7 +103,17 @@ void HOT Logger::log_message_(int level, const char *tag, int offset) {
const char *msg = this->tx_buffer_ + offset; const char *msg = this->tx_buffer_ + offset;
if (this->baud_rate_ > 0) if (this->baud_rate_ > 0)
this->hw_serial_->println(msg); 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); 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) Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size, UARTSelection uart)

View file

@ -53,16 +53,6 @@ int HOT esp_idf_log_vprintf_(const char *format, va_list args) { // NOLINT
if (log == nullptr) if (log == nullptr)
return 0; 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); log->log_vprintf_(ESPHOME_LOG_LEVEL, "esp-idf", 0, format, args);
#endif #endif
return 0; return 0;