Store strings only used for logging in flash (#2274)

Co-authored-by: Otto winter <otto@otto-winter.com>
This commit is contained in:
Oxan van Leeuwen 2021-09-13 09:48:52 +02:00 committed by GitHub
parent e18dfdd656
commit d594a6fcbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 241 additions and 251 deletions

View file

@ -10,7 +10,7 @@ namespace binary_sensor {
#define LOG_BINARY_SENSOR(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, (obj)->get_name().c_str()); \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
if (!(obj)->get_device_class().empty()) { \
ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
} \

View file

@ -9,8 +9,8 @@ void ClimateCall::perform() {
ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
this->validate_();
if (this->mode_.has_value()) {
const char *mode_s = climate_mode_to_string(*this->mode_);
ESP_LOGD(TAG, " Mode: %s", mode_s);
const LogString *mode_s = climate_mode_to_string(*this->mode_);
ESP_LOGD(TAG, " Mode: %s", LOG_STR_ARG(mode_s));
}
if (this->custom_fan_mode_.has_value()) {
this->fan_mode_.reset();
@ -18,8 +18,8 @@ void ClimateCall::perform() {
}
if (this->fan_mode_.has_value()) {
this->custom_fan_mode_.reset();
const char *fan_mode_s = climate_fan_mode_to_string(*this->fan_mode_);
ESP_LOGD(TAG, " Fan: %s", fan_mode_s);
const LogString *fan_mode_s = climate_fan_mode_to_string(*this->fan_mode_);
ESP_LOGD(TAG, " Fan: %s", LOG_STR_ARG(fan_mode_s));
}
if (this->custom_preset_.has_value()) {
this->preset_.reset();
@ -27,12 +27,12 @@ void ClimateCall::perform() {
}
if (this->preset_.has_value()) {
this->custom_preset_.reset();
const char *preset_s = climate_preset_to_string(*this->preset_);
ESP_LOGD(TAG, " Preset: %s", preset_s);
const LogString *preset_s = climate_preset_to_string(*this->preset_);
ESP_LOGD(TAG, " Preset: %s", LOG_STR_ARG(preset_s));
}
if (this->swing_mode_.has_value()) {
const char *swing_mode_s = climate_swing_mode_to_string(*this->swing_mode_);
ESP_LOGD(TAG, " Swing: %s", swing_mode_s);
const LogString *swing_mode_s = climate_swing_mode_to_string(*this->swing_mode_);
ESP_LOGD(TAG, " Swing: %s", LOG_STR_ARG(swing_mode_s));
}
if (this->target_temperature_.has_value()) {
ESP_LOGD(TAG, " Target Temperature: %.2f", *this->target_temperature_);
@ -50,7 +50,7 @@ void ClimateCall::validate_() {
if (this->mode_.has_value()) {
auto mode = *this->mode_;
if (!traits.supports_mode(mode)) {
ESP_LOGW(TAG, " Mode %s is not supported by this device!", climate_mode_to_string(mode));
ESP_LOGW(TAG, " Mode %s is not supported by this device!", LOG_STR_ARG(climate_mode_to_string(mode)));
this->mode_.reset();
}
}
@ -63,7 +63,8 @@ void ClimateCall::validate_() {
} else if (this->fan_mode_.has_value()) {
auto fan_mode = *this->fan_mode_;
if (!traits.supports_fan_mode(fan_mode)) {
ESP_LOGW(TAG, " Fan Mode %s is not supported by this device!", climate_fan_mode_to_string(fan_mode));
ESP_LOGW(TAG, " Fan Mode %s is not supported by this device!",
LOG_STR_ARG(climate_fan_mode_to_string(fan_mode)));
this->fan_mode_.reset();
}
}
@ -76,14 +77,15 @@ void ClimateCall::validate_() {
} else if (this->preset_.has_value()) {
auto preset = *this->preset_;
if (!traits.supports_preset(preset)) {
ESP_LOGW(TAG, " Preset %s is not supported by this device!", climate_preset_to_string(preset));
ESP_LOGW(TAG, " Preset %s is not supported by this device!", LOG_STR_ARG(climate_preset_to_string(preset)));
this->preset_.reset();
}
}
if (this->swing_mode_.has_value()) {
auto swing_mode = *this->swing_mode_;
if (!traits.supports_swing_mode(swing_mode)) {
ESP_LOGW(TAG, " Swing Mode %s is not supported by this device!", climate_swing_mode_to_string(swing_mode));
ESP_LOGW(TAG, " Swing Mode %s is not supported by this device!",
LOG_STR_ARG(climate_swing_mode_to_string(swing_mode)));
this->swing_mode_.reset();
}
}
@ -373,24 +375,24 @@ void Climate::publish_state() {
ESP_LOGD(TAG, "'%s' - Sending state:", this->name_.c_str());
auto traits = this->get_traits();
ESP_LOGD(TAG, " Mode: %s", climate_mode_to_string(this->mode));
ESP_LOGD(TAG, " Mode: %s", LOG_STR_ARG(climate_mode_to_string(this->mode)));
if (traits.get_supports_action()) {
ESP_LOGD(TAG, " Action: %s", climate_action_to_string(this->action));
ESP_LOGD(TAG, " Action: %s", LOG_STR_ARG(climate_action_to_string(this->action)));
}
if (traits.get_supports_fan_modes() && this->fan_mode.has_value()) {
ESP_LOGD(TAG, " Fan Mode: %s", climate_fan_mode_to_string(this->fan_mode.value()));
ESP_LOGD(TAG, " Fan Mode: %s", LOG_STR_ARG(climate_fan_mode_to_string(this->fan_mode.value())));
}
if (!traits.get_supported_custom_fan_modes().empty() && this->custom_fan_mode.has_value()) {
ESP_LOGD(TAG, " Custom Fan Mode: %s", this->custom_fan_mode.value().c_str());
}
if (traits.get_supports_presets() && this->preset.has_value()) {
ESP_LOGD(TAG, " Preset: %s", climate_preset_to_string(this->preset.value()));
ESP_LOGD(TAG, " Preset: %s", LOG_STR_ARG(climate_preset_to_string(this->preset.value())));
}
if (!traits.get_supported_custom_presets().empty() && this->custom_preset.has_value()) {
ESP_LOGD(TAG, " Custom Preset: %s", this->custom_preset.value().c_str());
}
if (traits.get_supports_swing_modes()) {
ESP_LOGD(TAG, " Swing Mode: %s", climate_swing_mode_to_string(this->swing_mode));
ESP_LOGD(TAG, " Swing Mode: %s", LOG_STR_ARG(climate_swing_mode_to_string(this->swing_mode)));
}
if (traits.get_supports_current_temperature()) {
ESP_LOGD(TAG, " Current Temperature: %.2f°C", this->current_temperature);
@ -534,12 +536,12 @@ void Climate::dump_traits_(const char *tag) {
if (!traits.get_supported_modes().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported modes:");
for (ClimateMode m : traits.get_supported_modes())
ESP_LOGCONFIG(tag, " - %s", climate_mode_to_string(m));
ESP_LOGCONFIG(tag, " - %s", LOG_STR_ARG(climate_mode_to_string(m)));
}
if (!traits.get_supported_fan_modes().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported fan modes:");
for (ClimateFanMode m : traits.get_supported_fan_modes())
ESP_LOGCONFIG(tag, " - %s", climate_fan_mode_to_string(m));
ESP_LOGCONFIG(tag, " - %s", LOG_STR_ARG(climate_fan_mode_to_string(m)));
}
if (!traits.get_supported_custom_fan_modes().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported custom fan modes:");
@ -549,7 +551,7 @@ void Climate::dump_traits_(const char *tag) {
if (!traits.get_supported_presets().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported presets:");
for (ClimatePreset p : traits.get_supported_presets())
ESP_LOGCONFIG(tag, " - %s", climate_preset_to_string(p));
ESP_LOGCONFIG(tag, " - %s", LOG_STR_ARG(climate_preset_to_string(p)));
}
if (!traits.get_supported_custom_presets().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported custom presets:");
@ -559,7 +561,7 @@ void Climate::dump_traits_(const char *tag) {
if (!traits.get_supported_swing_modes().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported swing modes:");
for (ClimateSwingMode m : traits.get_supported_swing_modes())
ESP_LOGCONFIG(tag, " - %s", climate_swing_mode_to_string(m));
ESP_LOGCONFIG(tag, " - %s", LOG_STR_ARG(climate_swing_mode_to_string(m)));
}
}

View file

@ -12,7 +12,7 @@ namespace climate {
#define LOG_CLIMATE(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, (obj)->get_name().c_str()); \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
}
class Climate;

View file

@ -3,105 +3,105 @@
namespace esphome {
namespace climate {
const char *climate_mode_to_string(ClimateMode mode) {
const LogString *climate_mode_to_string(ClimateMode mode) {
switch (mode) {
case CLIMATE_MODE_OFF:
return "OFF";
case CLIMATE_MODE_AUTO:
return "AUTO";
case CLIMATE_MODE_COOL:
return "COOL";
case CLIMATE_MODE_HEAT:
return "HEAT";
case CLIMATE_MODE_FAN_ONLY:
return "FAN_ONLY";
case CLIMATE_MODE_DRY:
return "DRY";
return LOG_STR("OFF");
case CLIMATE_MODE_HEAT_COOL:
return "HEAT_COOL";
return LOG_STR("HEAT_COOL");
case CLIMATE_MODE_AUTO:
return LOG_STR("AUTO");
case CLIMATE_MODE_COOL:
return LOG_STR("COOL");
case CLIMATE_MODE_HEAT:
return LOG_STR("HEAT");
case CLIMATE_MODE_FAN_ONLY:
return LOG_STR("FAN_ONLY");
case CLIMATE_MODE_DRY:
return LOG_STR("DRY");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
const char *climate_action_to_string(ClimateAction action) {
const LogString *climate_action_to_string(ClimateAction action) {
switch (action) {
case CLIMATE_ACTION_OFF:
return "OFF";
return LOG_STR("OFF");
case CLIMATE_ACTION_COOLING:
return "COOLING";
return LOG_STR("COOLING");
case CLIMATE_ACTION_HEATING:
return "HEATING";
return LOG_STR("HEATING");
case CLIMATE_ACTION_IDLE:
return "IDLE";
return LOG_STR("IDLE");
case CLIMATE_ACTION_DRYING:
return "DRYING";
return LOG_STR("DRYING");
case CLIMATE_ACTION_FAN:
return "FAN";
return LOG_STR("FAN");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
const char *climate_fan_mode_to_string(ClimateFanMode fan_mode) {
const LogString *climate_fan_mode_to_string(ClimateFanMode fan_mode) {
switch (fan_mode) {
case climate::CLIMATE_FAN_ON:
return "ON";
return LOG_STR("ON");
case climate::CLIMATE_FAN_OFF:
return "OFF";
return LOG_STR("OFF");
case climate::CLIMATE_FAN_AUTO:
return "AUTO";
return LOG_STR("AUTO");
case climate::CLIMATE_FAN_LOW:
return "LOW";
return LOG_STR("LOW");
case climate::CLIMATE_FAN_MEDIUM:
return "MEDIUM";
return LOG_STR("MEDIUM");
case climate::CLIMATE_FAN_HIGH:
return "HIGH";
return LOG_STR("HIGH");
case climate::CLIMATE_FAN_MIDDLE:
return "MIDDLE";
return LOG_STR("MIDDLE");
case climate::CLIMATE_FAN_FOCUS:
return "FOCUS";
return LOG_STR("FOCUS");
case climate::CLIMATE_FAN_DIFFUSE:
return "DIFFUSE";
return LOG_STR("DIFFUSE");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
const char *climate_swing_mode_to_string(ClimateSwingMode swing_mode) {
const LogString *climate_swing_mode_to_string(ClimateSwingMode swing_mode) {
switch (swing_mode) {
case climate::CLIMATE_SWING_OFF:
return "OFF";
return LOG_STR("OFF");
case climate::CLIMATE_SWING_BOTH:
return "BOTH";
return LOG_STR("BOTH");
case climate::CLIMATE_SWING_VERTICAL:
return "VERTICAL";
return LOG_STR("VERTICAL");
case climate::CLIMATE_SWING_HORIZONTAL:
return "HORIZONTAL";
return LOG_STR("HORIZONTAL");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
const char *climate_preset_to_string(ClimatePreset preset) {
const LogString *climate_preset_to_string(ClimatePreset preset) {
switch (preset) {
case climate::CLIMATE_PRESET_NONE:
return "NONE";
return LOG_STR("NONE");
case climate::CLIMATE_PRESET_HOME:
return "HOME";
return LOG_STR("HOME");
case climate::CLIMATE_PRESET_ECO:
return "ECO";
return LOG_STR("ECO");
case climate::CLIMATE_PRESET_AWAY:
return "AWAY";
return LOG_STR("AWAY");
case climate::CLIMATE_PRESET_BOOST:
return "BOOST";
return LOG_STR("BOOST");
case climate::CLIMATE_PRESET_COMFORT:
return "COMFORT";
return LOG_STR("COMFORT");
case climate::CLIMATE_PRESET_SLEEP:
return "SLEEP";
return LOG_STR("SLEEP");
case climate::CLIMATE_PRESET_ACTIVITY:
return "ACTIVITY";
return LOG_STR("ACTIVITY");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
#include "esphome/core/log.h"
namespace esphome {
namespace climate {
@ -96,19 +97,19 @@ enum ClimatePreset : uint8_t {
};
/// Convert the given ClimateMode to a human-readable string.
const char *climate_mode_to_string(ClimateMode mode);
const LogString *climate_mode_to_string(ClimateMode mode);
/// Convert the given ClimateAction to a human-readable string.
const char *climate_action_to_string(ClimateAction action);
const LogString *climate_action_to_string(ClimateAction action);
/// Convert the given ClimateFanMode to a human-readable string.
const char *climate_fan_mode_to_string(ClimateFanMode mode);
const LogString *climate_fan_mode_to_string(ClimateFanMode mode);
/// Convert the given ClimateSwingMode to a human-readable string.
const char *climate_swing_mode_to_string(ClimateSwingMode mode);
const LogString *climate_swing_mode_to_string(ClimateSwingMode mode);
/// Convert the given ClimateSwingMode to a human-readable string.
const char *climate_preset_to_string(ClimatePreset preset);
const LogString *climate_preset_to_string(ClimatePreset preset);
} // namespace climate
} // namespace esphome

View file

@ -13,7 +13,7 @@ const extern float COVER_CLOSED;
#define LOG_COVER(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, (obj)->get_name().c_str()); \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
auto traits_ = (obj)->get_traits(); \
if (traits_.get_is_assumed_state()) { \
ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \

View file

@ -47,28 +47,28 @@ void GPIOSwitch::setup() {
void GPIOSwitch::dump_config() {
LOG_SWITCH("", "GPIO Switch", this);
LOG_PIN(" Pin: ", this->pin_);
const char *restore_mode = "";
const LogString *restore_mode = LOG_STR("");
switch (this->restore_mode_) {
case GPIO_SWITCH_RESTORE_DEFAULT_OFF:
restore_mode = "Restore (Defaults to OFF)";
restore_mode = LOG_STR("Restore (Defaults to OFF)");
break;
case GPIO_SWITCH_RESTORE_DEFAULT_ON:
restore_mode = "Restore (Defaults to ON)";
restore_mode = LOG_STR("Restore (Defaults to ON)");
break;
case GPIO_SWITCH_RESTORE_INVERTED_DEFAULT_ON:
restore_mode = "Restore inverted (Defaults to ON)";
restore_mode = LOG_STR("Restore inverted (Defaults to ON)");
break;
case GPIO_SWITCH_RESTORE_INVERTED_DEFAULT_OFF:
restore_mode = "Restore inverted (Defaults to OFF)";
restore_mode = LOG_STR("Restore inverted (Defaults to OFF)");
break;
case GPIO_SWITCH_ALWAYS_OFF:
restore_mode = "Always OFF";
restore_mode = LOG_STR("Always OFF");
break;
case GPIO_SWITCH_ALWAYS_ON:
restore_mode = "Always ON";
restore_mode = LOG_STR("Always ON");
break;
}
ESP_LOGCONFIG(TAG, " Restore Mode: %s", restore_mode);
ESP_LOGCONFIG(TAG, " Restore Mode: %s", LOG_STR_ARG(restore_mode));
if (!this->interlock_.empty()) {
ESP_LOGCONFIG(TAG, " Interlocks:");
for (auto *lock : this->interlock_) {

View file

@ -165,7 +165,7 @@ void HitachiClimate::transmit_state() {
set_power_(false);
break;
default:
ESP_LOGW(TAG, "Unsupported mode: %s", climate_mode_to_string(this->mode));
ESP_LOGW(TAG, "Unsupported mode: %s", LOG_STR_ARG(climate_mode_to_string(this->mode)));
}
set_temp_(static_cast<uint8_t>(this->target_temperature));

View file

@ -166,7 +166,7 @@ void HitachiClimate::transmit_state() {
set_power_(false);
break;
default:
ESP_LOGW(TAG, "Unsupported mode: %s", climate_mode_to_string(this->mode));
ESP_LOGW(TAG, "Unsupported mode: %s", LOG_STR_ARG(climate_mode_to_string(this->mode)));
}
set_temp_(static_cast<uint8_t>(this->target_temperature));

View file

@ -7,24 +7,24 @@ namespace light {
static const char *const TAG = "light";
static const char *color_mode_to_human(ColorMode color_mode) {
static const LogString *color_mode_to_human(ColorMode color_mode) {
if (color_mode == ColorMode::UNKNOWN)
return "Unknown";
return LOG_STR("Unknown");
if (color_mode == ColorMode::WHITE)
return "White";
return LOG_STR("White");
if (color_mode == ColorMode::COLOR_TEMPERATURE)
return "Color temperature";
return LOG_STR("Color temperature");
if (color_mode == ColorMode::COLD_WARM_WHITE)
return "Cold/warm white";
return LOG_STR("Cold/warm white");
if (color_mode == ColorMode::RGB)
return "RGB";
return LOG_STR("RGB");
if (color_mode == ColorMode::RGB_WHITE)
return "RGBW";
return LOG_STR("RGBW");
if (color_mode == ColorMode::RGB_COLD_WARM_WHITE)
return "RGB + cold/warm white";
return LOG_STR("RGB + cold/warm white");
if (color_mode == ColorMode::RGB_COLOR_TEMPERATURE)
return "RGB + color temperature";
return "";
return LOG_STR("RGB + color temperature");
return LOG_STR("");
}
void LightCall::perform() {
@ -37,7 +37,7 @@ void LightCall::perform() {
// Only print color mode when it's being changed
ColorMode current_color_mode = this->parent_->remote_values.get_color_mode();
if (this->color_mode_.value_or(current_color_mode) != current_color_mode) {
ESP_LOGD(TAG, " Color mode: %s", color_mode_to_human(v.get_color_mode()));
ESP_LOGD(TAG, " Color mode: %s", LOG_STR_ARG(color_mode_to_human(v.get_color_mode())));
}
// Only print state when it's being changed
@ -135,7 +135,7 @@ LightColorValues LightCall::validate_() {
// Color mode check
if (this->color_mode_.has_value() && !traits.supports_color_mode(this->color_mode_.value())) {
ESP_LOGW(TAG, "'%s' - This light does not support color mode %s!", name,
color_mode_to_human(this->color_mode_.value()));
LOG_STR_ARG(color_mode_to_human(this->color_mode_.value())));
this->color_mode_.reset();
}
@ -206,7 +206,8 @@ LightColorValues LightCall::validate_() {
if (name_##_.has_value()) { \
auto val = *name_##_; \
if (val < (min) || val > (max)) { \
ESP_LOGW(TAG, "'%s' - %s value %.2f is out of range [%.1f - %.1f]!", name, upper_name, val, (min), (max)); \
ESP_LOGW(TAG, "'%s' - %s value %.2f is out of range [%.1f - %.1f]!", name, LOG_STR_LITERAL(upper_name), val, \
(min), (max)); \
name_##_ = clamp(val, (min), (max)); \
} \
}
@ -387,7 +388,7 @@ ColorMode LightCall::compute_color_mode_() {
// Don't change if the current mode is suitable.
if (suitable_modes.count(current_mode) > 0) {
ESP_LOGI(TAG, "'%s' - Keeping current color mode %s for call without color mode.",
this->parent_->get_name().c_str(), color_mode_to_human(current_mode));
this->parent_->get_name().c_str(), LOG_STR_ARG(color_mode_to_human(current_mode)));
return current_mode;
}
@ -397,7 +398,7 @@ ColorMode LightCall::compute_color_mode_() {
continue;
ESP_LOGI(TAG, "'%s' - Using color mode %s for call without color mode.", this->parent_->get_name().c_str(),
color_mode_to_human(mode));
LOG_STR_ARG(color_mode_to_human(mode)));
return mode;
}
@ -405,7 +406,7 @@ ColorMode LightCall::compute_color_mode_() {
// out whatever we don't support.
auto color_mode = current_mode != ColorMode::UNKNOWN ? current_mode : *supported_modes.begin();
ESP_LOGW(TAG, "'%s' - No color mode suitable for this call supported, defaulting to %s!",
this->parent_->get_name().c_str(), color_mode_to_human(color_mode));
this->parent_->get_name().c_str(), LOG_STR_ARG(color_mode_to_human(color_mode)));
return color_mode;
}
std::set<ColorMode> LightCall::get_suitable_color_modes_() {

View file

@ -221,40 +221,40 @@ void MQTTClientComponent::check_connected() {
void MQTTClientComponent::loop() {
if (this->disconnect_reason_.has_value()) {
const char *reason_s = nullptr;
const LogString *reason_s;
switch (*this->disconnect_reason_) {
case AsyncMqttClientDisconnectReason::TCP_DISCONNECTED:
reason_s = "TCP disconnected";
reason_s = LOG_STR("TCP disconnected");
break;
case AsyncMqttClientDisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION:
reason_s = "Unacceptable Protocol Version";
reason_s = LOG_STR("Unacceptable Protocol Version");
break;
case AsyncMqttClientDisconnectReason::MQTT_IDENTIFIER_REJECTED:
reason_s = "Identifier Rejected";
reason_s = LOG_STR("Identifier Rejected");
break;
case AsyncMqttClientDisconnectReason::MQTT_SERVER_UNAVAILABLE:
reason_s = "Server Unavailable";
reason_s = LOG_STR("Server Unavailable");
break;
case AsyncMqttClientDisconnectReason::MQTT_MALFORMED_CREDENTIALS:
reason_s = "Malformed Credentials";
reason_s = LOG_STR("Malformed Credentials");
break;
case AsyncMqttClientDisconnectReason::MQTT_NOT_AUTHORIZED:
reason_s = "Not Authorized";
reason_s = LOG_STR("Not Authorized");
break;
case AsyncMqttClientDisconnectReason::ESP8266_NOT_ENOUGH_SPACE:
reason_s = "Not Enough Space";
reason_s = LOG_STR("Not Enough Space");
break;
case AsyncMqttClientDisconnectReason::TLS_BAD_FINGERPRINT:
reason_s = "TLS Bad Fingerprint";
reason_s = LOG_STR("TLS Bad Fingerprint");
break;
default:
reason_s = "Unknown";
reason_s = LOG_STR("Unknown");
break;
}
if (!network_is_connected()) {
reason_s = "WiFi disconnected";
reason_s = LOG_STR("WiFi disconnected");
}
ESP_LOGW(TAG, "MQTT Disconnected: %s.", reason_s);
ESP_LOGW(TAG, "MQTT Disconnected: %s.", LOG_STR_ARG(reason_s));
this->disconnect_reason_.reset();
}

View file

@ -8,7 +8,7 @@ namespace number {
#define LOG_NUMBER(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, (obj)->get_name().c_str()); \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
if (!(obj)->traits.get_icon().empty()) { \
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->traits.get_icon().c_str()); \
} \

View file

@ -10,7 +10,7 @@ namespace select {
#define LOG_SELECT(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, (obj)->get_name().c_str()); \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
if (!(obj)->traits.get_icon().empty()) { \
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->traits.get_icon().c_str()); \
} \

View file

@ -6,15 +6,15 @@ namespace sensor {
static const char *const TAG = "sensor";
const char *state_class_to_string(StateClass state_class) {
const LogString *state_class_to_string(StateClass state_class) {
switch (state_class) {
case STATE_CLASS_MEASUREMENT:
return "measurement";
return LOG_STR("measurement");
case STATE_CLASS_TOTAL_INCREASING:
return "total_increasing";
return LOG_STR("total_increasing");
case STATE_CLASS_NONE:
default:
return "";
return LOG_STR("");
}
}

View file

@ -1,5 +1,6 @@
#pragma once
#include "esphome/core/log.h"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "esphome/components/sensor/filter.h"
@ -9,11 +10,11 @@ namespace sensor {
#define LOG_SENSOR(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, (obj)->get_name().c_str()); \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
if (!(obj)->get_device_class().empty()) { \
ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
} \
ESP_LOGCONFIG(TAG, "%s State Class: '%s'", prefix, state_class_to_string((obj)->state_class)); \
ESP_LOGCONFIG(TAG, "%s State Class: '%s'", prefix, LOG_STR_ARG(state_class_to_string((obj)->state_class))); \
ESP_LOGCONFIG(TAG, "%s Unit of Measurement: '%s'", prefix, (obj)->get_unit_of_measurement().c_str()); \
ESP_LOGCONFIG(TAG, "%s Accuracy Decimals: %d", prefix, (obj)->get_accuracy_decimals()); \
if (!(obj)->get_icon().empty()) { \
@ -36,7 +37,7 @@ enum StateClass : uint8_t {
STATE_CLASS_TOTAL_INCREASING = 2,
};
const char *state_class_to_string(StateClass state_class);
const LogString *state_class_to_string(StateClass state_class);
/** Base-class for all sensors.
*

View file

@ -9,7 +9,7 @@ namespace switch_ {
#define LOG_SWITCH(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, (obj)->get_name().c_str()); \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
if (!(obj)->get_icon().empty()) { \
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
} \

View file

@ -8,7 +8,7 @@ namespace text_sensor {
#define LOG_TEXT_SENSOR(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, (obj)->get_name().c_str()); \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
if (!(obj)->get_icon().empty()) { \
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
} \

View file

@ -58,21 +58,21 @@ void UARTDevice::check_uart_settings(uint32_t baud_rate, uint8_t stop_bits, UART
this->parent_->data_bits_);
}
if (this->parent_->parity_ != parity) {
ESP_LOGE(TAG, " Invalid parity: Integration requested parity %s but you have %s!", parity_to_str(parity),
parity_to_str(this->parent_->parity_));
ESP_LOGE(TAG, " Invalid parity: Integration requested parity %s but you have %s!",
LOG_STR_ARG(parity_to_str(parity)), LOG_STR_ARG(parity_to_str(this->parent_->parity_)));
}
}
const char *parity_to_str(UARTParityOptions parity) {
const LogString *parity_to_str(UARTParityOptions parity) {
switch (parity) {
case UART_CONFIG_PARITY_NONE:
return "NONE";
return LOG_STR("NONE");
case UART_CONFIG_PARITY_EVEN:
return "EVEN";
return LOG_STR("EVEN");
case UART_CONFIG_PARITY_ODD:
return "ODD";
return LOG_STR("ODD");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}

View file

@ -4,6 +4,7 @@
#include <HardwareSerial.h>
#include "esphome/core/esphal.h"
#include "esphome/core/component.h"
#include "esphome/core/log.h"
namespace esphome {
namespace uart {
@ -14,7 +15,7 @@ enum UARTParityOptions {
UART_CONFIG_PARITY_ODD,
};
const char *parity_to_str(UARTParityOptions parity);
const LogString *parity_to_str(UARTParityOptions parity);
#ifdef ARDUINO_ARCH_ESP8266
class ESP8266SoftwareSerial {

View file

@ -104,7 +104,7 @@ void UARTComponent::dump_config() {
}
ESP_LOGCONFIG(TAG, " Baud Rate: %u baud", this->baud_rate_);
ESP_LOGCONFIG(TAG, " Data Bits: %u", this->data_bits_);
ESP_LOGCONFIG(TAG, " Parity: %s", parity_to_str(this->parity_));
ESP_LOGCONFIG(TAG, " Parity: %s", LOG_STR_ARG(parity_to_str(this->parity_)));
ESP_LOGCONFIG(TAG, " Stop bits: %u", this->stop_bits_);
if (this->hw_serial_ != nullptr) {
ESP_LOGCONFIG(TAG, " Using hardware serial interface.");

View file

@ -307,7 +307,7 @@ void WiFiComponent::start_connecting(const WiFiAP &ap, bool two) {
this->action_started_ = millis();
}
void print_signal_bars(int8_t rssi, char *buf) {
const LogString *get_signal_bars(int8_t rssi) {
// LOWER ONE QUARTER BLOCK
// Unicode: U+2582, UTF-8: E2 96 82
// LOWER HALF BLOCK
@ -317,36 +317,36 @@ void print_signal_bars(int8_t rssi, char *buf) {
// FULL BLOCK
// Unicode: U+2588, UTF-8: E2 96 88
if (rssi >= -50) {
sprintf(buf, "\033[0;32m" // green
"\xe2\x96\x82"
"\xe2\x96\x84"
"\xe2\x96\x86"
"\xe2\x96\x88"
"\033[0m");
return LOG_STR("\033[0;32m" // green
"\xe2\x96\x82"
"\xe2\x96\x84"
"\xe2\x96\x86"
"\xe2\x96\x88"
"\033[0m");
} else if (rssi >= -65) {
sprintf(buf, "\033[0;33m" // yellow
"\xe2\x96\x82"
"\xe2\x96\x84"
"\xe2\x96\x86"
"\033[0;37m"
"\xe2\x96\x88"
"\033[0m");
return LOG_STR("\033[0;33m" // yellow
"\xe2\x96\x82"
"\xe2\x96\x84"
"\xe2\x96\x86"
"\033[0;37m"
"\xe2\x96\x88"
"\033[0m");
} else if (rssi >= -85) {
sprintf(buf, "\033[0;33m" // yellow
"\xe2\x96\x82"
"\xe2\x96\x84"
"\033[0;37m"
"\xe2\x96\x86"
"\xe2\x96\x88"
"\033[0m");
return LOG_STR("\033[0;33m" // yellow
"\xe2\x96\x82"
"\xe2\x96\x84"
"\033[0;37m"
"\xe2\x96\x86"
"\xe2\x96\x88"
"\033[0m");
} else {
sprintf(buf, "\033[0;31m" // red
"\xe2\x96\x82"
"\033[0;37m"
"\xe2\x96\x84"
"\xe2\x96\x86"
"\xe2\x96\x88"
"\033[0m");
return LOG_STR("\033[0;31m" // red
"\xe2\x96\x82"
"\033[0;37m"
"\xe2\x96\x84"
"\xe2\x96\x86"
"\xe2\x96\x88"
"\033[0m");
}
}
@ -361,10 +361,8 @@ void WiFiComponent::print_connect_params_() {
ESP_LOGCONFIG(TAG, " BSSID: " LOG_SECRET("%02X:%02X:%02X:%02X:%02X:%02X"), bssid[0], bssid[1], bssid[2], bssid[3],
bssid[4], bssid[5]);
ESP_LOGCONFIG(TAG, " Hostname: '%s'", App.get_name().c_str());
char signal_bars[50];
int8_t rssi = WiFi.RSSI();
print_signal_bars(rssi, signal_bars);
ESP_LOGCONFIG(TAG, " Signal strength: %d dB %s", rssi, signal_bars);
ESP_LOGCONFIG(TAG, " Signal strength: %d dB %s", rssi, LOG_STR_ARG(get_signal_bars(rssi)));
if (this->selected_ap_.get_bssid().has_value()) {
ESP_LOGV(TAG, " Priority: %.1f", this->get_sta_priority(*this->selected_ap_.get_bssid()));
}
@ -433,16 +431,15 @@ void WiFiComponent::check_scanning_finished() {
char bssid_s[18];
auto bssid = res.get_bssid();
sprintf(bssid_s, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
char signal_bars[50];
print_signal_bars(res.get_rssi(), signal_bars);
if (res.get_matches()) {
ESP_LOGI(TAG, "- '%s' %s" LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(),
res.get_is_hidden() ? "(HIDDEN) " : "", bssid_s, signal_bars);
res.get_is_hidden() ? "(HIDDEN) " : "", bssid_s, LOG_STR_ARG(get_signal_bars(res.get_rssi())));
ESP_LOGD(TAG, " Channel: %u", res.get_channel());
ESP_LOGD(TAG, " RSSI: %d dB", res.get_rssi());
} else {
ESP_LOGD(TAG, "- " LOG_SECRET("'%s'") " " LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(), bssid_s, signal_bars);
ESP_LOGD(TAG, "- " LOG_SECRET("'%s'") " " LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(), bssid_s,
LOG_STR_ARG(get_signal_bars(res.get_rssi())));
}
}

View file

@ -327,20 +327,20 @@ class WiFiMockClass : public ESP8266WiFiGenericClass {
static void _event_callback(void *event) { ESP8266WiFiGenericClass::_eventCallback(event); } // NOLINT
};
const char *get_auth_mode_str(uint8_t mode) {
const LogString *get_auth_mode_str(uint8_t mode) {
switch (mode) {
case AUTH_OPEN:
return "OPEN";
return LOG_STR("OPEN");
case AUTH_WEP:
return "WEP";
return LOG_STR("WEP");
case AUTH_WPA_PSK:
return "WPA PSK";
return LOG_STR("WPA PSK");
case AUTH_WPA2_PSK:
return "WPA2 PSK";
return LOG_STR("WPA2 PSK");
case AUTH_WPA_WPA2_PSK:
return "WPA/WPA2 PSK";
return LOG_STR("WPA/WPA2 PSK");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
#ifdef ipv4_addr
@ -358,22 +358,22 @@ std::string format_ip_addr(struct ip_addr ip) {
return buf;
}
#endif
const char *get_op_mode_str(uint8_t mode) {
const LogString *get_op_mode_str(uint8_t mode) {
switch (mode) {
case WIFI_OFF:
return "OFF";
return LOG_STR("OFF");
case WIFI_STA:
return "STA";
return LOG_STR("STA");
case WIFI_AP:
return "AP";
return LOG_STR("AP");
case WIFI_AP_STA:
return "AP+STA";
return LOG_STR("AP+STA");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
// Note that this method returns PROGMEM strings, so use LOG_STR_ARG() to access them.
const char *get_disconnect_reason_str(uint8_t reason) {
const LogString *get_disconnect_reason_str(uint8_t reason) {
/* If this were one big switch statement, GCC would generate a lookup table for it. However, the values of the
* REASON_* constants aren't continuous, and GCC will fill in the gap with the default value -- wasting 4 bytes of RAM
* per entry. As there's ~175 default entries, this wastes 700 bytes of RAM.
@ -470,8 +470,8 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
}
case EVENT_STAMODE_AUTHMODE_CHANGE: {
auto it = event->event_info.auth_change;
ESP_LOGV(TAG, "Event: Changed AuthMode old=%s new=%s", get_auth_mode_str(it.old_mode),
get_auth_mode_str(it.new_mode));
ESP_LOGV(TAG, "Event: Changed AuthMode old=%s new=%s", LOG_STR_ARG(get_auth_mode_str(it.old_mode)),
LOG_STR_ARG(get_auth_mode_str(it.new_mode)));
// Mitigate CVE-2020-12638
// https://lbsfilm.at/blog/wpa2-authenticationmode-downgrade-in-espressif-microprocessors
if (it.old_mode != AUTH_OPEN && it.new_mode == AUTH_OPEN) {
@ -511,8 +511,8 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0)
case EVENT_OPMODE_CHANGED: {
auto it = event->event_info.opmode_changed;
ESP_LOGV(TAG, "Event: Changed Mode old=%s new=%s", get_op_mode_str(it.old_opmode),
get_op_mode_str(it.new_opmode));
ESP_LOGV(TAG, "Event: Changed Mode old=%s new=%s", LOG_STR_ARG(get_op_mode_str(it.old_opmode)),
LOG_STR_ARG(get_op_mode_str(it.new_opmode)));
break;
}
case EVENT_SOFTAPMODE_DISTRIBUTE_STA_IP: {

View file

@ -43,81 +43,56 @@ GPIOPin::GPIOPin(uint8_t pin, uint8_t mode, bool inverted)
{
}
const char *GPIOPin::get_pin_mode_name() const {
const LogString *GPIOPin::get_pin_mode_name() const {
const char *mode_s;
switch (this->mode_) {
case INPUT:
mode_s = "INPUT";
break;
return LOG_STR("INPUT");
case OUTPUT:
mode_s = "OUTPUT";
break;
return LOG_STR("OUTPUT");
case INPUT_PULLUP:
mode_s = "INPUT_PULLUP";
break;
return LOG_STR("INPUT_PULLUP");
case OUTPUT_OPEN_DRAIN:
mode_s = "OUTPUT_OPEN_DRAIN";
break;
return LOG_STR("OUTPUT_OPEN_DRAIN");
case SPECIAL:
mode_s = "SPECIAL";
break;
return LOG_STR("SPECIAL");
case FUNCTION_1:
mode_s = "FUNCTION_1";
break;
return LOG_STR("FUNCTION_1");
case FUNCTION_2:
mode_s = "FUNCTION_2";
break;
return LOG_STR("FUNCTION_2");
case FUNCTION_3:
mode_s = "FUNCTION_3";
break;
return LOG_STR("FUNCTION_3");
case FUNCTION_4:
mode_s = "FUNCTION_4";
break;
return LOG_STR("FUNCTION_4");
#ifdef ARDUINO_ARCH_ESP32
case PULLUP:
mode_s = "PULLUP";
break;
return LOG_STR("PULLUP");
case PULLDOWN:
mode_s = "PULLDOWN";
break;
return LOG_STR("PULLDOWN");
case INPUT_PULLDOWN:
mode_s = "INPUT_PULLDOWN";
break;
return LOG_STR("INPUT_PULLDOWN");
case OPEN_DRAIN:
mode_s = "OPEN_DRAIN";
break;
return LOG_STR("OPEN_DRAIN");
case FUNCTION_5:
mode_s = "FUNCTION_5";
break;
return LOG_STR("FUNCTION_5");
case FUNCTION_6:
mode_s = "FUNCTION_6";
break;
return LOG_STR("FUNCTION_6");
case ANALOG:
mode_s = "ANALOG";
break;
return LOG_STR("ANALOG");
#endif
#ifdef ARDUINO_ARCH_ESP8266
case FUNCTION_0:
mode_s = "FUNCTION_0";
break;
return LOG_STR("FUNCTION_0");
case WAKEUP_PULLUP:
mode_s = "WAKEUP_PULLUP";
break;
return LOG_STR("WAKEUP_PULLUP");
case WAKEUP_PULLDOWN:
mode_s = "WAKEUP_PULLDOWN";
break;
return LOG_STR("WAKEUP_PULLDOWN");
case INPUT_PULLDOWN_16:
mode_s = "INPUT_PULLDOWN_16";
break;
return LOG_STR("INPUT_PULLDOWN_16");
#endif
default:
mode_s = "UNKNOWN";
break;
return LOG_STR("UNKNOWN");
}
return mode_s;
}
unsigned char GPIOPin::get_pin() const { return this->pin_; }

View file

@ -25,6 +25,8 @@
#undef abs
#endif
#include "esphome/core/log.h"
namespace esphome {
#define LOG_PIN(prefix, pin) \
@ -32,7 +34,8 @@ namespace esphome {
ESP_LOGCONFIG(TAG, prefix LOG_PIN_PATTERN, LOG_PIN_ARGS(pin)); \
}
#define LOG_PIN_PATTERN "GPIO%u (Mode: %s%s)"
#define LOG_PIN_ARGS(pin) (pin)->get_pin(), (pin)->get_pin_mode_name(), ((pin)->is_inverted() ? ", INVERTED" : "")
#define LOG_PIN_ARGS(pin) \
(pin)->get_pin(), LOG_STR_ARG((pin)->get_pin_mode_name()), ((pin)->is_inverted() ? ", INVERTED" : "")
/// Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
class ISRInternalGPIOPin {
@ -85,7 +88,7 @@ class GPIOPin {
/// Get the GPIO pin number.
uint8_t get_pin() const;
const char *get_pin_mode_name() const;
const LogString *get_pin_mode_name() const;
/// Get the pinMode of this pin.
uint8_t get_mode() const;
/// Return whether this pin shall be treated as inverted. (for example active-low)

View file

@ -165,28 +165,37 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#define ONOFF(b) ((b) ? "ON" : "OFF")
#define TRUEFALSE(b) ((b) ? "TRUE" : "FALSE")
#ifdef USE_STORE_LOG_STR_IN_FLASH
#define LOG_STR(s) PSTR(s)
// Helper class that identifies strings that may be stored in flash storage (similar to Arduino's __FlashStringHelper)
struct LogString;
// From Arduino 2.5 onwards, we can pass a PSTR() to printf(). For previous versions, emulate support
// by copying the message to a local buffer first. String length is limited to 63 characters.
#ifdef USE_STORE_LOG_STR_IN_FLASH
#include <pgmspace.h>
#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 0)
#define LOG_STR_ARG(s) ((PGM_P)(s))
#else
// Pre-Arduino 2.5, we can't pass a PSTR() to printf(). Emulate support by copying the message to a
// local buffer first. String length is limited to 63 characters.
// https://github.com/esp8266/Arduino/commit/6280e98b0360f85fdac2b8f10707fffb4f6e6e31
#include <core_version.h>
#if defined(ARDUINO_ARCH_ESP8266) && ARDUINO_VERSION_CODE < VERSION_CODE(2, 5, 0)
#define LOG_STR_ARG(s) \
({ \
char __buf[64]; \
__buf[63] = '\0'; \
strncpy_P(__buf, s, 63); \
strncpy_P(__buf, (PGM_P)(s), 63); \
__buf; \
})
#else
#define LOG_STR_ARG(s) (s)
#endif
#else
#define LOG_STR(s) (s)
#define LOG_STR_ARG(s) (s)
#define LOG_STR(s) (reinterpret_cast<const LogString *>(PSTR(s)))
#define LOG_STR_LITERAL(s) LOG_STR_ARG(LOG_STR(s))
#else // !USE_STORE_LOG_STR_IN_FLASH
#define LOG_STR(s) (reinterpret_cast<const LogString *>(s))
#define LOG_STR_ARG(s) (reinterpret_cast<const char *>(s))
#define LOG_STR_LITERAL(s) (s)
#endif
} // namespace esphome