JXCT Soil Sensor 7-in-1 v3.4.9 (June 2025)
Professional IoT soil monitoring system with ESP32, Modbus RTU, MQTT, and advanced compensation algorithms
Загрузка...
Поиск...
Не найдено
debug_optimized.h
См. документацию.
1#pragma once
2
8
9#include "jxct_constants.h"
10
11// ============================================================================
12// УСЛОВНАЯ КОМПИЛЯЦИЯ DEBUG СООБЩЕНИЙ
13// ============================================================================
14
15#ifdef DEBUG_BUILD
16#define DEBUG_ENABLED true
17#else
18#define DEBUG_ENABLED false
19#endif
20
21// ============================================================================
22// ОПТИМИЗИРОВАННЫЕ DEBUG МАКРОСЫ
23// ============================================================================
24
25// Общие отладочные макросы
26#if DEBUG_ENABLED
27#define DEBUG_PRINT(x) Serial.print(x)
28#define DEBUG_PRINTLN(x) Serial.println(x)
29#define DEBUG_PRINTF(fmt, ...) Serial.printf(fmt, ##__VA_ARGS__)
30#else
31#define DEBUG_PRINT(x) \
32 do \
33 { \
34 } while (0)
35#define DEBUG_PRINTLN(x) \
36 do \
37 { \
38 } while (0)
39#define DEBUG_PRINTF(fmt, ...) \
40 do \
41 { \
42 } while (0)
43#endif
44
45// Модульные отладочные макросы (включаются отдельно)
46#if DEBUG_ENABLED && DEBUG_MODBUS_ENABLED
47#define DEBUG_MODBUS_PRINT(x) \
48 Serial.print("[MODBUS] "); \
49 Serial.print(x)
50#define DEBUG_MODBUS_PRINTLN(x) \
51 Serial.print("[MODBUS] "); \
52 Serial.println(x)
53#define DEBUG_MODBUS_PRINTF(fmt, ...) Serial.printf("[MODBUS] " fmt, ##__VA_ARGS__)
54#else
55#define DEBUG_MODBUS_PRINT(x) \
56 do \
57 { \
58 } while (0)
59#define DEBUG_MODBUS_PRINTLN(x) \
60 do \
61 { \
62 } while (0)
63#define DEBUG_MODBUS_PRINTF(fmt, ...) \
64 do \
65 { \
66 } while (0)
67#endif
68
69#if DEBUG_ENABLED && DEBUG_MQTT_ENABLED
70#define DEBUG_MQTT_PRINT(x) \
71 Serial.print("[MQTT] "); \
72 Serial.print(x)
73#define DEBUG_MQTT_PRINTLN(x) \
74 Serial.print("[MQTT] "); \
75 Serial.println(x)
76#define DEBUG_MQTT_PRINTF(fmt, ...) Serial.printf("[MQTT] " fmt, ##__VA_ARGS__)
77#else
78#define DEBUG_MQTT_PRINT(x) \
79 do \
80 { \
81 } while (0)
82#define DEBUG_MQTT_PRINTLN(x) \
83 do \
84 { \
85 } while (0)
86#define DEBUG_MQTT_PRINTF(fmt, ...) \
87 do \
88 { \
89 } while (0)
90#endif
91
92#if DEBUG_ENABLED && DEBUG_WIFI_ENABLED
93#define DEBUG_WIFI_PRINT(x) \
94 Serial.print("[WIFI] "); \
95 Serial.print(x)
96#define DEBUG_WIFI_PRINTLN(x) \
97 Serial.print("[WIFI] "); \
98 Serial.println(x)
99#define DEBUG_WIFI_PRINTF(fmt, ...) Serial.printf("[WIFI] " fmt, ##__VA_ARGS__)
100#else
101#define DEBUG_WIFI_PRINT(x) \
102 do \
103 { \
104 } while (0)
105#define DEBUG_WIFI_PRINTLN(x) \
106 do \
107 { \
108 } while (0)
109#define DEBUG_WIFI_PRINTF(fmt, ...) \
110 do \
111 { \
112 } while (0)
113#endif
114
115// ============================================================================
116// ПРОИЗВОДИТЕЛЬНЫЕ ОТЛАДОЧНЫЕ ФУНКЦИИ
117// ============================================================================
118
125inline void debugConditionalPrint(int level, const char* format, ...)
126{
127#if DEBUG_ENABLED
128 extern int currentLogLevel;
129 if (currentLogLevel >= level)
130 {
131 va_list args;
132 va_start(args, format);
133 Serial.printf(format, args);
134 va_end(args);
135 }
136#endif
137}
138
145inline void debugPrintHexBuffer(const char* prefix, const uint8_t* buffer, size_t length)
146{
147#if DEBUG_ENABLED && DEBUG_MODBUS_ENABLED
148 extern int currentLogLevel;
149 if (currentLogLevel >= LOG_LEVEL_DEBUG && length > 0)
150 {
151 Serial.print(prefix);
152 for (size_t i = 0; i < length; i++)
153 {
154 if (buffer[i] < 0x10) Serial.print("0");
155 Serial.print(buffer[i], HEX);
156 Serial.print(" ");
157 }
158 Serial.println();
159 }
160#endif
161}
162
169inline void debugPrintStatus(const char* module, bool status, const char* details = nullptr)
170{
171#if DEBUG_ENABLED
172 Serial.printf("[%s] %s", module, status ? "✅" : "❌");
173 if (details)
174 {
175 Serial.printf(" - %s", details);
176 }
177 Serial.println();
178#endif
179}
180
181// ============================================================================
182// МАКРОСЫ ДЛЯ ЗАМЕНЫ СТАРЫХ DEBUG ВЫЗОВОВ
183// ============================================================================
184
185// Замена для избыточных DEBUG_PRINTF в mqtt_client.cpp
186#define DEBUG_MQTT_DELTA(fmt, ...) DEBUG_MQTT_PRINTF("[DELTA] " fmt "\n", ##__VA_ARGS__)
187#define DEBUG_MQTT_DNS(fmt, ...) DEBUG_MQTT_PRINTF("[DNS] " fmt "\n", ##__VA_ARGS__)
188#define DEBUG_MQTT_HA(fmt, ...) DEBUG_MQTT_PRINTF("[HA] " fmt "\n", ##__VA_ARGS__)
189
190// Замена для избыточных DEBUG в modbus_sensor.cpp
191#define DEBUG_MODBUS_TX() DEBUG_MODBUS_PRINTLN("TX режим")
192#define DEBUG_MODBUS_RX() DEBUG_MODBUS_PRINTLN("RX режим")
193#define DEBUG_MODBUS_MOVING_AVG(fmt, ...) DEBUG_MODBUS_PRINTF("[MOVING_AVG] " fmt "\n", ##__VA_ARGS__)
194
195// Замена для main.cpp
196#define DEBUG_MAIN_BATCH(msg) DEBUG_PRINTLN("[BATCH] " msg)
197#define DEBUG_MAIN_BUTTON(msg) DEBUG_PRINTLN("[BUTTON] " msg)
198
199// ============================================================================
200// СТАТИСТИКА ОТЛАДКИ (для профилирования)
201// ============================================================================
202
203#if DEBUG_ENABLED
204struct DebugStats
205{
206 unsigned long total_messages;
207 unsigned long modbus_messages;
208 unsigned long mqtt_messages;
209 unsigned long wifi_messages;
210 unsigned long start_time;
211};
212
213extern DebugStats debug_stats;
214
215inline void debugStatsInit()
216{
217 debug_stats = {0, 0, 0, 0, millis()};
218}
219
220inline void debugStatsIncrement(const char* category)
221{
222 debug_stats.total_messages++;
223 if (strstr(category, "MODBUS"))
224 debug_stats.modbus_messages++;
225 else if (strstr(category, "MQTT"))
226 debug_stats.mqtt_messages++;
227 else if (strstr(category, "WIFI"))
228 debug_stats.wifi_messages++;
229}
230
231inline void debugStatsPrint()
232{
233 unsigned long uptime = millis() - debug_stats.start_time;
234 Serial.printf("\n=== DEBUG СТАТИСТИКА ===\n");
235 Serial.printf("Время работы: %lu мс\n", uptime);
236 Serial.printf("Всего сообщений: %lu\n", debug_stats.total_messages);
237 Serial.printf("MODBUS: %lu, MQTT: %lu, WIFI: %lu\n", debug_stats.modbus_messages, debug_stats.mqtt_messages,
238 debug_stats.wifi_messages);
239 Serial.printf("Частота: %.2f сообщений/сек\n", debug_stats.total_messages * 1000.0 / uptime);
240 Serial.printf("========================\n\n");
241}
242#else
243inline void debugStatsInit() {}
244inline void debugStatsIncrement(const char* category) {}
245inline void debugStatsPrint() {}
246#endif
247
248// ============================================================================
249// МИГРАЦИОННЫЕ МАКРОСЫ (для постепенного перехода)
250// ============================================================================
251
252// Эти макросы помогают постепенно мигрировать с старых DEBUG_PRINTF на новые
253#define MIGRATE_DEBUG_PRINTF(fmt, ...) DEBUG_PRINTF(fmt, ##__VA_ARGS__)
254#define MIGRATE_DEBUG_PRINTLN(msg) DEBUG_PRINTLN(msg)
255
256// Макросы для критически важных отладочных сообщений (всегда включены)
257#define CRITICAL_DEBUG_PRINT(x) \
258 Serial.print("[CRITICAL] "); \
259 Serial.print(x)
260#define CRITICAL_DEBUG_PRINTLN(x) \
261 Serial.print("[CRITICAL] "); \
262 Serial.println(x)
263#define CRITICAL_DEBUG_PRINTF(fmt, ...) Serial.printf("[CRITICAL] " fmt, ##__VA_ARGS__)
void debugPrintHexBuffer(const char *prefix, const uint8_t *buffer, size_t length)
Отладочная печать буфера в HEX формате (оптимизированная)
Определения debug_optimized.h:145
void debugConditionalPrint(int level, const char *format,...)
Условная отладочная печать с проверкой уровня логирования
Определения debug_optimized.h:125
void debugPrintStatus(const char *module, bool status, const char *details=nullptr)
Отладочная печать состояния системы (компактная)
Определения debug_optimized.h:169
void debugStatsPrint()
Определения debug_optimized.h:245
void debugStatsIncrement(const char *category)
Определения debug_optimized.h:244
void debugStatsInit()
Определения debug_optimized.h:243
Централизованные константы системы JXCT.
constexpr int LOG_LEVEL_DEBUG
Определения jxct_constants.h:179
LogLevel currentLogLevel
Определения logger.cpp:11