/
ellersseer
/
MyCheat
Обзор
Документация
Войти
/
ellersseer
/
MyCheat
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
2
CI/CD
Аналитика
Безопасность
master
include/debug_log.h
142 строки
5 KB
Dmitry Zhiltsov
Initial commit
31 янв 2026, 01:50
31 янв 2026, 01:50
fc17bd5
Код
Авторство
О чём код?
#ifndef DEBUG_LOG_H #define DEBUG_LOG_H #include <Arduino.h> #include "esp_system.h" #include "esp_core_dump.h" #include "rom/rtc.h" // ============================================ // Configuration // ============================================ #define DEBUG_LOG_MAX_EVENTS 16 // Number of events in ring buffer #define DEBUG_LOG_MSG_SIZE 48 // Max message length per event #define DEBUG_LOG_MAGIC 0xDEADBEEF // Validation marker // ============================================ // Event Types // ============================================ enum DebugEventType : uint8_t { EVT_BOOT = 0, EVT_WIFI_CONNECT, EVT_WIFI_DISCONNECT, EVT_MQTT_CONNECT, EVT_MQTT_DISCONNECT, EVT_MQTT_ERROR, EVT_ONEWIRE_ERROR, EVT_LOW_MEMORY, EVT_STACK_WARNING, EVT_TASK_WDT_WARNING, EVT_WEB_REQUEST, EVT_TEMP_UPDATE, EVT_ERROR, EVT_WARNING, EVT_INFO, EVT_CUSTOM }; // ============================================ // Structures (stored in RTC memory) // ============================================ // Single event entry struct DebugEvent { uint32_t timestamp; // millis() when event occurred DebugEventType type; uint8_t data1; // Optional numeric data uint16_t data2; // Optional numeric data char message[DEBUG_LOG_MSG_SIZE]; }; // Boot record - saved before potential crash struct BootRecord { uint32_t bootTime; // When this boot started (always 0, but useful for reference) uint32_t lastAliveTime; // Last time system was known alive uint32_t freeHeap; // Heap at last alive check uint32_t minFreeHeap; // Min heap observed uint16_t stackHwmOnewire; // Stack high water mark uint16_t stackHwmMqtt; uint8_t lastResetReason; // esp_reset_reason_t uint8_t lastResetReasonCpu0; // rtc_get_reset_reason(0) uint8_t lastResetReasonCpu1; // rtc_get_reset_reason(1) uint8_t padding; }; // Main debug structure in RTC memory struct DebugLogData { uint32_t magic; // Validation marker uint32_t bootCount; // Total boot count uint32_t crashCount; // Abnormal reset count uint32_t panicCount; // Panic count uint32_t wdtCount; // Watchdog reset count uint32_t brownoutCount; // Brownout count BootRecord currentBoot; // Current boot info (updated periodically) BootRecord previousBoot; // Previous boot info (copied at startup) // Ring buffer for events DebugEvent events[DEBUG_LOG_MAX_EVENTS]; uint8_t eventHead; // Next write position uint8_t eventCount; // Number of valid events uint8_t reserved[2]; }; // ============================================ // Debug Log Manager Class // ============================================ class DebugLogManager { public: void begin(); // Event logging void logEvent(DebugEventType type, const char* message = nullptr, uint8_t data1 = 0, uint16_t data2 = 0); void logEventF(DebugEventType type, const char* format, ...); // Periodic update (call from main loop or timer) void updateAliveStatus(uint16_t stackHwmOnewire = 0, uint16_t stackHwmMqtt = 0); // Getters uint32_t getBootCount() const; uint32_t getCrashCount() const; const BootRecord& getPreviousBoot() const; const BootRecord& getCurrentBoot() const; // Reset reason helpers const char* getResetReasonString(esp_reset_reason_t reason) const; const char* getCpuResetReasonString(RESET_REASON reason) const; esp_reset_reason_t getLastResetReason() const; bool wasAbnormalReset() const; // JSON output String getDebugJson() const; String getEventsJson() const; // Core dump bool hasCoreDump() const; String getCoreDumpSummary() const; void eraseCoreDump(); // Reset statistics void resetStats(); private: void initializeData(); void copyPreviousBoot(); void incrementCounters(); const char* eventTypeToString(DebugEventType type) const; }; // Global instance extern DebugLogManager debugLog; // ============================================ // Convenience Macros // ============================================ #define DEBUG_EVENT(type, msg) debugLog.logEvent(type, msg) #define DEBUG_EVENTF(type, fmt, ...) debugLog.logEventF(type, fmt, ##__VA_ARGS__) #define DEBUG_INFO(msg) debugLog.logEvent(EVT_INFO, msg) #define DEBUG_WARNING(msg) debugLog.logEvent(EVT_WARNING, msg) #define DEBUG_ERROR(msg) debugLog.logEvent(EVT_ERROR, msg) #endif // DEBUG_LOG_H