2026-05-31 03:20:26 +08:00
|
|
|
|
/*
|
2026-05-05 04:48:31 +08:00
|
|
|
|
* debug_log.h - 调试日志输出
|
2026-04-27 00:42:26 +08:00
|
|
|
|
*
|
2026-05-05 04:48:31 +08:00
|
|
|
|
* 当前日志会并行写入:
|
|
|
|
|
|
* - UART0 串口
|
|
|
|
|
|
* - UDP 单播(独立调试接收端可选)
|
|
|
|
|
|
* - 环形缓冲区,供 HTTP /logs 拉取
|
|
|
|
|
|
*
|
|
|
|
|
|
* 注意:当前 tools/WebUI 日志面板走的是 HTTP /logs,
|
|
|
|
|
|
* 不是直接监听 UDP 6666。
|
2026-04-28 01:06:17 +08:00
|
|
|
|
*
|
|
|
|
|
|
* UART0: TX=GPIO21, RX=GPIO20, 115200 波特率
|
2026-04-27 00:42:26 +08:00
|
|
|
|
*/
|
|
|
|
|
|
#ifndef DEBUG_LOG_H
|
|
|
|
|
|
#define DEBUG_LOG_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
#include "esp_err.h"
|
2026-05-31 03:20:26 +08:00
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
|
|
#include "freertos/semphr.h"
|
2026-04-27 00:42:26 +08:00
|
|
|
|
#include "app_config.h"
|
2026-05-31 03:20:26 +08:00
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
#include <netinet/in.h>
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
|
|
|
|
|
// 调试日志配置(DEBUG_LOG_IP/PORT/ENABLED 已在 app_config.h 中定义)
|
|
|
|
|
|
|
2026-04-28 01:06:17 +08:00
|
|
|
|
// UART0 调试配置
|
|
|
|
|
|
#define UART_DEBUG_TX_GPIO 21
|
|
|
|
|
|
#define UART_DEBUG_RX_GPIO 20
|
|
|
|
|
|
#define UART_DEBUG_BAUD_RATE 115200
|
|
|
|
|
|
|
2026-04-27 00:42:26 +08:00
|
|
|
|
// 日志级别
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
|
DEBUG_LOG_NONE = 0,
|
|
|
|
|
|
DEBUG_LOG_ERR = 1, // 错误
|
|
|
|
|
|
DEBUG_LOG_WARN = 2, // 警告
|
|
|
|
|
|
DEBUG_LOG_INFO = 3, // 信息
|
|
|
|
|
|
DEBUG_LOG_DBG = 4, // 调试
|
|
|
|
|
|
DEBUG_LOG_ALL = 5 // 全部
|
|
|
|
|
|
} debug_log_level_t;
|
|
|
|
|
|
|
2026-05-05 04:48:31 +08:00
|
|
|
|
/**
|
2026-05-31 03:20:26 +08:00
|
|
|
|
* @brief 调试日志类(单例)
|
|
|
|
|
|
*
|
|
|
|
|
|
* 封装 UART0、UDP 和环形缓冲区三路日志输出通道,
|
|
|
|
|
|
* 所有内部状态(socket、缓冲区、互斥锁等)均为私有成员。
|
2026-05-05 04:48:31 +08:00
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
class DebugLog {
|
|
|
|
|
|
public:
|
2026-06-15 01:14:29 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取 DebugLog 单例实例。
|
|
|
|
|
|
* @return DebugLog 引用。
|
|
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
static DebugLog& instance();
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief 初始化日志系统,包括 UART 输出、UDP 目标和内部缓冲区。 */
|
|
|
|
|
|
esp_err_t init();
|
2026-04-28 01:06:17 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief 初始化 UART0 调试串口。 */
|
|
|
|
|
|
esp_err_t uartInit();
|
2026-04-28 01:06:17 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief 通过 UART0 直接发送一段原始字节数据。 */
|
|
|
|
|
|
esp_err_t uartSend(const uint8_t *data, size_t len);
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief 发送原始日志数据到底层输出通道。 */
|
|
|
|
|
|
esp_err_t send(const uint8_t *data, size_t len);
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief 按日志级别与标签格式化输出一条日志。 */
|
|
|
|
|
|
void log(debug_log_level_t level, const char *tag, const char *fmt, ...);
|
2026-05-05 04:48:31 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief 获取当前环形日志缓冲区中的日志条数。 */
|
|
|
|
|
|
int ringCount();
|
2026-05-05 03:28:44 +08:00
|
|
|
|
|
2026-06-20 00:46:04 +08:00
|
|
|
|
/** @brief 弹出一条最旧日志并从环形缓冲区移除。 */
|
|
|
|
|
|
bool ringPopOldest(char *out, size_t out_size, int *out_len);
|
2026-05-31 03:20:26 +08:00
|
|
|
|
char* ringDrainJson(int *out_len);
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief 当前日志级别(运行时可改)。 */
|
|
|
|
|
|
debug_log_level_t logLevel = DEBUG_LOG_INFO;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
DebugLog() = default;
|
|
|
|
|
|
~DebugLog() = default;
|
|
|
|
|
|
DebugLog(const DebugLog&) = delete;
|
|
|
|
|
|
DebugLog& operator=(const DebugLog&) = delete;
|
|
|
|
|
|
|
|
|
|
|
|
int m_udpSock = -1;
|
|
|
|
|
|
struct sockaddr_in m_debugAddr;
|
|
|
|
|
|
bool m_initialized = false;
|
|
|
|
|
|
bool m_uartInitialized = false;
|
|
|
|
|
|
|
|
|
|
|
|
static constexpr int LOG_RING_SLOTS = 64;
|
|
|
|
|
|
static constexpr int LOG_RING_SIZE = 256;
|
|
|
|
|
|
char m_logRing[LOG_RING_SLOTS][LOG_RING_SIZE];
|
|
|
|
|
|
int m_logRingLens[LOG_RING_SLOTS];
|
|
|
|
|
|
int m_logRingHead = 0;
|
|
|
|
|
|
int m_logRingCount = 0;
|
|
|
|
|
|
SemaphoreHandle_t m_logRingMutex = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
esp_err_t ensureUdpSocket();
|
|
|
|
|
|
void ringPush(const char *data, int len);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 便捷宏 — 直接调用单例方法
|
|
|
|
|
|
#define DEBUG_ERR(tag, fmt, ...) DebugLog::instance().log(DEBUG_LOG_ERR, tag, fmt, ##__VA_ARGS__)
|
|
|
|
|
|
#define DEBUG_WARN(tag, fmt, ...) DebugLog::instance().log(DEBUG_LOG_WARN, tag, fmt, ##__VA_ARGS__)
|
|
|
|
|
|
#define DEBUG_INFO(tag, fmt, ...) DebugLog::instance().log(DEBUG_LOG_INFO, tag, fmt, ##__VA_ARGS__)
|
|
|
|
|
|
#define DEBUG_DBG(tag, fmt, ...) DebugLog::instance().log(DEBUG_LOG_DBG, tag, fmt, ##__VA_ARGS__)
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-06-20 00:46:04 +08:00
|
|
|
|
#endif // DEBUG_LOG_H
|