This commit is contained in:
J. Nick Koston
2025-12-30 13:41:29 -10:00
parent c13bbd300d
commit c716983d5c
2 changed files with 19 additions and 25 deletions

View File

@@ -1,41 +1,24 @@
#include "esphome/core/gpio.h"
#include "esphome/core/log.h"
#include <algorithm>
#include <cstring>
namespace esphome {
static void log_pin_with_prefix(const char *tag, const char *prefix, GPIOPin *pin) {
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);
esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, "%s%.*s", prefix, (int) len, buffer);
}
#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;
// Copy prefix from flash to stack
static constexpr size_t LOG_PIN_PREFIX_MAX_LEN = 32;
char prefix_buf[LOG_PIN_PREFIX_MAX_LEN];
strncpy_P(prefix_buf, reinterpret_cast<const char *>(prefix), sizeof(prefix_buf) - 1);
prefix_buf[sizeof(prefix_buf) - 1] = '\0';
log_pin_with_prefix(tag, prefix_buf, pin);
log_pin_with_prefix_(tag, prefix_buf, pin);
}
#else
void log_pin(const char *tag, const char *prefix, GPIOPin *pin) {
if (pin == nullptr)
return;
log_pin_with_prefix(tag, prefix, pin);
log_pin_with_prefix_(tag, prefix, pin);
}
#endif
} // namespace esphome

View File

@@ -5,20 +5,16 @@
#include <string>
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
/// Maximum buffer size for dump_summary output
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
@@ -148,4 +144,19 @@ inline size_t GPIOPin::dump_summary(char *buffer, size_t len) const {
// Remove before 2026.7.0
inline std::string GPIOPin::dump_summary() const { return {}; }
// Inline helper for log_pin - allows compiler to inline into log_pin in gpio.cpp
inline void log_pin_with_prefix_(const char *tag, const char *prefix, GPIOPin *pin) {
char buffer[GPIO_SUMMARY_MAX_LEN];
size_t len = pin->dump_summary(buffer, sizeof(buffer));
len = std::min(len, sizeof(buffer) - 1);
esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, "%s%.*s", prefix, (int) len, buffer);
}
// log_pin function declarations - implementation in gpio.cpp
#ifdef USE_ESP8266
void log_pin(const char *tag, const __FlashStringHelper *prefix, GPIOPin *pin);
#else
void log_pin(const char *tag, const char *prefix, GPIOPin *pin);
#endif
} // namespace esphome