diff --git a/esphome/core/gpio.cpp b/esphome/core/gpio.cpp index bce233c566..5dce88a915 100644 --- a/esphome/core/gpio.cpp +++ b/esphome/core/gpio.cpp @@ -2,9 +2,30 @@ #include "esphome/core/log.h" #include +#include namespace esphome { +#ifdef USE_ESP8266 + +static constexpr size_t LOG_PIN_PREFIX_MAX_LEN = 32; + +void log_pin(const char *tag, const __FlashStringHelper *prefix, GPIOPin *pin) { + if (pin == nullptr) + return; + char buffer[GPIO_SUMMARY_MAX_LEN]; + size_t len = pin->dump_summary(buffer, sizeof(buffer)); + // Clamp to actual buffer size (snprintf returns would-be length) + len = std::min(len, sizeof(buffer) - 1); + // Copy prefix from flash to stack, then format + char prefix_buf[LOG_PIN_PREFIX_MAX_LEN]; + strncpy_P(prefix_buf, reinterpret_cast(prefix), sizeof(prefix_buf) - 1); + prefix_buf[sizeof(prefix_buf) - 1] = '\0'; + esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, "%s%.*s", prefix_buf, (int) len, buffer); +} + +#else + void log_pin(const char *tag, const char *prefix, GPIOPin *pin) { if (pin == nullptr) return; @@ -15,4 +36,6 @@ void log_pin(const char *tag, const char *prefix, GPIOPin *pin) { esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, "%s%.*s", prefix, (int) len, buffer); } +#endif + } // namespace esphome diff --git a/esphome/core/gpio.h b/esphome/core/gpio.h index fe53802abf..bcbd4a3762 100644 --- a/esphome/core/gpio.h +++ b/esphome/core/gpio.h @@ -14,9 +14,13 @@ inline constexpr size_t GPIO_SUMMARY_MAX_LEN = 48; class GPIOPin; // Forward declaration /// Log a pin summary to the config log +#ifdef USE_ESP8266 +void log_pin(const char *tag, const __FlashStringHelper *prefix, GPIOPin *pin); +#define LOG_PIN(prefix, pin) log_pin(TAG, F(prefix), pin) +#else void log_pin(const char *tag, const char *prefix, GPIOPin *pin); - #define LOG_PIN(prefix, pin) log_pin(TAG, prefix, pin) +#endif // put GPIO flags in a namespace to not pollute esphome namespace namespace gpio {