ESPHome  2024.11.2
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 
56  // log when free heap space has halved
57  uint32_t new_free_heap = get_free_heap_();
58  if (new_free_heap < this->free_heap_ / 2) {
59  this->free_heap_ = new_free_heap;
60  ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
61  this->status_momentary_warning("heap", 1000);
62  }
63 
64 #ifdef USE_SENSOR
65  // calculate loop time - from last call to this one
66  if (this->loop_time_sensor_ != nullptr) {
67  uint32_t now = millis();
68  uint32_t loop_time = now - this->last_loop_timetag_;
69  this->max_loop_time_ = std::max(this->max_loop_time_, loop_time);
70  this->last_loop_timetag_ = now;
71  }
72 #endif // USE_SENSOR
73 }
74 
76 #ifdef USE_SENSOR
77  if (this->free_sensor_ != nullptr) {
79  }
80 
81  if (this->loop_time_sensor_ != nullptr) {
83  this->max_loop_time_ = 0;
84  }
85 
86 #endif // USE_SENSOR
88 }
89 
91 
92 } // namespace debug
93 } // namespace esphome
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