ESPHome  2025.2.0
debug_component.cpp
Go to the documentation of this file.
1 #include "debug_component.h"
2 
3 #include <algorithm>
4 #include "esphome/core/log.h"
5 #include "esphome/core/hal.h"
6 #include "esphome/core/helpers.h"
7 #include "esphome/core/version.h"
8 #include <cinttypes>
9 #include <climits>
10 
11 namespace esphome {
12 namespace debug {
13 
14 static const char *const TAG = "debug";
15 
17 #ifndef ESPHOME_LOG_HAS_DEBUG
18  return; // Can't log below if debug logging is disabled
19 #endif
20 
21  ESP_LOGCONFIG(TAG, "Debug component:");
22 #ifdef USE_TEXT_SENSOR
23  LOG_TEXT_SENSOR(" ", "Device info", this->device_info_);
24 #endif // USE_TEXT_SENSOR
25 #ifdef USE_SENSOR
26  LOG_SENSOR(" ", "Free space on heap", this->free_sensor_);
27  LOG_SENSOR(" ", "Largest free heap block", this->block_sensor_);
28 #if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 2)
29  LOG_SENSOR(" ", "Heap fragmentation", this->fragmentation_sensor_);
30 #endif // defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 2)
31 #endif // USE_SENSOR
32 
33  std::string device_info;
34  device_info.reserve(256);
35  ESP_LOGD(TAG, "ESPHome version %s", ESPHOME_VERSION);
36  device_info += ESPHOME_VERSION;
37 
38  this->free_heap_ = get_free_heap_();
39  ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
40 
41  get_device_info_(device_info);
42 
43 #ifdef USE_TEXT_SENSOR
44  if (this->device_info_ != nullptr) {
45  if (device_info.length() > 255)
46  device_info.resize(255);
47  this->device_info_->publish_state(device_info);
48  }
49  if (this->reset_reason_ != nullptr) {
51  }
52 #endif // USE_TEXT_SENSOR
53 
54 #ifdef USE_ESP32
55  this->log_partition_info_(); // Log partition information for ESP32
56 #endif // USE_ESP32
57 }
58 
60  // log when free heap space has halved
61  uint32_t new_free_heap = get_free_heap_();
62  if (new_free_heap < this->free_heap_ / 2) {
63  this->free_heap_ = new_free_heap;
64  ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
65  this->status_momentary_warning("heap", 1000);
66  }
67 
68 #ifdef USE_SENSOR
69  // calculate loop time - from last call to this one
70  if (this->loop_time_sensor_ != nullptr) {
71  uint32_t now = millis();
72  uint32_t loop_time = now - this->last_loop_timetag_;
73  this->max_loop_time_ = std::max(this->max_loop_time_, loop_time);
74  this->last_loop_timetag_ = now;
75  }
76 #endif // USE_SENSOR
77 }
78 
80 #ifdef USE_SENSOR
81  if (this->free_sensor_ != nullptr) {
83  }
84 
85  if (this->loop_time_sensor_ != nullptr) {
87  this->max_loop_time_ = 0;
88  }
89 
90 #endif // USE_SENSOR
92 }
93 
95 
96 } // namespace debug
97 } // namespace esphome
void log_partition_info_()
Logs information about the device&#39;s partition table.
Definition: debug_esp32.cpp:32
void get_device_info_(std::string &device_info)
void status_momentary_warning(const std::string &name, uint32_t length=5000)
Definition: component.cpp:178
text_sensor::TextSensor * device_info_
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:28
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
sensor::Sensor * loop_time_sensor_
float get_setup_priority() const override
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
sensor::Sensor * fragmentation_sensor_
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
text_sensor::TextSensor * reset_reason_
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7