mirror of
https://github.com/esphome/esphome.git
synced 2026-06-24 13:45:15 +00:00
[esp32] Defer preferences nvs_open warning until after logger init
ESP32Preferences::open() runs from app_main() before the generated setup() calls Logger::pre_setup(), so global_logger is null. The ESP_LOGW on nvs_open failure would dereference null in release builds (and assert in ESPHOME_DEBUG). Stash the err in a static and emit the warning lazily from the first make_preference() call, which runs after pre_setup() at EARLY_INIT priority.
This commit is contained in:
@@ -18,6 +18,12 @@ struct NVSData {
|
|||||||
|
|
||||||
static std::vector<NVSData> s_pending_save; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
static std::vector<NVSData> s_pending_save; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
|
// open() runs from app_main() before the logger is initialized, so any failure
|
||||||
|
// must be deferred until after global_logger is set. This is emitted from the
|
||||||
|
// first make_preference() call, which runs from the generated setup() after
|
||||||
|
// log->pre_setup() has run at EARLY_INIT priority.
|
||||||
|
static esp_err_t s_open_err = ESP_OK; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
bool ESP32PreferenceBackend::save(const uint8_t *data, size_t len) {
|
bool ESP32PreferenceBackend::save(const uint8_t *data, size_t len) {
|
||||||
// try find in pending saves and update that
|
// try find in pending saves and update that
|
||||||
for (auto &obj : s_pending_save) {
|
for (auto &obj : s_pending_save) {
|
||||||
@@ -75,7 +81,7 @@ void ESP32Preferences::open() {
|
|||||||
if (err == 0)
|
if (err == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ESP_LOGW(TAG, "nvs_open failed: %s - erasing NVS", esp_err_to_name(err));
|
s_open_err = err;
|
||||||
nvs_flash_deinit();
|
nvs_flash_deinit();
|
||||||
nvs_flash_erase();
|
nvs_flash_erase();
|
||||||
nvs_flash_init();
|
nvs_flash_init();
|
||||||
@@ -87,6 +93,10 @@ void ESP32Preferences::open() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ESPPreferenceObject ESP32Preferences::make_preference(size_t length, uint32_t type) {
|
ESPPreferenceObject ESP32Preferences::make_preference(size_t length, uint32_t type) {
|
||||||
|
if (s_open_err != ESP_OK) {
|
||||||
|
ESP_LOGW(TAG, "nvs_open failed: %s - erased NVS", esp_err_to_name(s_open_err));
|
||||||
|
s_open_err = ESP_OK;
|
||||||
|
}
|
||||||
auto *pref = new ESP32PreferenceBackend(); // NOLINT(cppcoreguidelines-owning-memory)
|
auto *pref = new ESP32PreferenceBackend(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||||
pref->nvs_handle = this->nvs_handle;
|
pref->nvs_handle = this->nvs_handle;
|
||||||
pref->key = type;
|
pref->key = type;
|
||||||
|
|||||||
Reference in New Issue
Block a user