Files
bolar_1/main/trace_ctx.h

55 lines
1.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* TraceCtx — Borla 设备侧追踪上下文(docs/0019 §10.1, §11.3).
3 个 id:
session_id 连接级 · 每次 WS 连接刷新
trace_id 回合级 · 每次设备主动发起对话回合刷新
request_id 每次 HTTP/OTA 调用刷新
每次 BORLA_LOGI/D/W/E 打日志时都自动在前缀里带 [trace_id],
与 server / backend grep 同一 trace_id 拼端到端链路。
*/
#pragma once
#include <cstddef>
#include <cstdint>
namespace borla {
class TraceCtx {
public:
// 长度上限 · prefix + '-' + 16hex + '\0' = 18 + 余量
static constexpr size_t kIdBufSize = 96;
// 取当前 id(线程安全 · 内部用原子指针交换)
static const char* TraceId(); // "ws-xxxx" / "dev-xxxx" / "-"
static const char* SessionId(); // "s-xxxx" / "-"
static const char* RequestId(); // "r-xxxx" / "-"
// 重新生成 id · 用在事件起点
static void RefreshTrace(const char* prefix = "dev");
static void RefreshSession();
static const char* NewRequestId(); // 返回最新 request_id · 用于 HTTP header
// 显式设置(从 server 下行的 trace_id / session_id 透传)
static void SetTrace(const char* trace_id);
static void SetSession(const char* session_id);
private:
static char trace_id_[kIdBufSize];
static char session_id_[kIdBufSize];
static char request_id_[kIdBufSize];
};
} // namespace borla
// 标准日志宏 · 替代裸 ESP_LOGI/D/W/E
#define BORLA_LOGI(tag, fmt, ...) \
ESP_LOGI(tag, "[%s] " fmt, ::borla::TraceCtx::TraceId(), ##__VA_ARGS__)
#define BORLA_LOGD(tag, fmt, ...) \
ESP_LOGD(tag, "[%s] " fmt, ::borla::TraceCtx::TraceId(), ##__VA_ARGS__)
#define BORLA_LOGW(tag, fmt, ...) \
ESP_LOGW(tag, "[%s] " fmt, ::borla::TraceCtx::TraceId(), ##__VA_ARGS__)
#define BORLA_LOGE(tag, fmt, ...) \
ESP_LOGE(tag, "[%s] " fmt, ::borla::TraceCtx::TraceId(), ##__VA_ARGS__)