115 lines
3.4 KiB
C++
115 lines
3.4 KiB
C++
#include "application.h"
|
||
#include "system_info.h"
|
||
#include "trace_ctx.h"
|
||
|
||
#include <esp_log.h>
|
||
#include <esp_mac.h>
|
||
#include <nvs_flash.h>
|
||
#include <esp_event.h>
|
||
|
||
#include <cstdarg>
|
||
#include <cstdio>
|
||
#include <cstring>
|
||
#include <ctime>
|
||
#include <sys/time.h>
|
||
|
||
// MAC 缓存。app_main 早期写一次,之后只读,无需锁。
|
||
// 18 = "xx:xx:xx:xx:xx:xx" + '\0'。
|
||
static char g_mac_str[18] = "??:??:??:??:??:??";
|
||
|
||
#ifndef BORLA_MONITORING_LOGS_ONLY
|
||
#define BORLA_MONITORING_LOGS_ONLY 0
|
||
#endif
|
||
|
||
#ifndef BORLA_TRACE_LOGS_ENABLED
|
||
#define BORLA_TRACE_LOGS_ENABLED 0
|
||
#endif
|
||
|
||
static constexpr bool kMonitoringLogsOnly = BORLA_MONITORING_LOGS_ONLY != 0;
|
||
static constexpr bool kTraceLogsEnabled = BORLA_TRACE_LOGS_ENABLED != 0;
|
||
|
||
static void CacheMacAddress()
|
||
{
|
||
uint8_t mac[6] = {0};
|
||
if (esp_read_mac(mac, ESP_MAC_WIFI_STA) == ESP_OK) {
|
||
snprintf(g_mac_str, sizeof(g_mac_str),
|
||
"%02x:%02x:%02x:%02x:%02x:%02x",
|
||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||
}
|
||
}
|
||
|
||
static bool ShouldEmitLogLine(const char* line)
|
||
{
|
||
if (strstr(line, "[TRACE]") != nullptr) return kTraceLogsEnabled;
|
||
if (!kMonitoringLogsOnly) return true;
|
||
|
||
// ESP-IDF renders logs as "I (123) TAG: ..." after our "[time mac] "
|
||
// prefix. Keep warnings/errors even when ordinary INFO/DEBUG is muted.
|
||
const char* p = strchr(line, ']');
|
||
if (!p) return false;
|
||
++p;
|
||
while (*p == ' ') ++p;
|
||
if (p[0] == '\033' && p[1] == '[') {
|
||
while (*p && *p != 'm') ++p;
|
||
if (*p == 'm') ++p;
|
||
}
|
||
while (*p == ' ') ++p;
|
||
char level = *p;
|
||
return level == 'W' || level == 'E';
|
||
}
|
||
|
||
// 自定义日志 vprintf:每条日志前加 [yyyy-mm-dd HH:MM:SS MAC] 前缀。
|
||
// 未 NTP/ServerHello 同步前,时间会显示 1970-01-01(epoch)。
|
||
// MAC 在 app_main 早期就缓存好,全程只读。
|
||
//
|
||
// 拼到单个栈缓冲再 fwrite,避免两次 printf 在多任务并发时被穿插。
|
||
static int LogVprintfWithWallClock(const char* format, va_list args)
|
||
{
|
||
struct timeval tv;
|
||
gettimeofday(&tv, nullptr);
|
||
struct tm tm_info;
|
||
localtime_r(&tv.tv_sec, &tm_info);
|
||
|
||
char ts[24];
|
||
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", &tm_info);
|
||
|
||
char buf[512];
|
||
int n = snprintf(buf, sizeof(buf), "[%s %s] ", ts, g_mac_str);
|
||
if (n < 0) n = 0;
|
||
if (n >= (int)sizeof(buf)) n = sizeof(buf) - 1;
|
||
int m = vsnprintf(buf + n, sizeof(buf) - n, format, args);
|
||
if (m < 0) m = 0;
|
||
int total = n + m;
|
||
if (total >= (int)sizeof(buf)) total = sizeof(buf) - 1;
|
||
if (!ShouldEmitLogLine(buf)) return total;
|
||
return (int)fwrite(buf, 1, total, stdout);
|
||
}
|
||
|
||
static void InitializeNvs()
|
||
{
|
||
esp_err_t ret = nvs_flash_init();
|
||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||
ret = nvs_flash_init();
|
||
}
|
||
ESP_ERROR_CHECK(ret);
|
||
}
|
||
|
||
extern "C" void app_main(void)
|
||
{
|
||
// 先缓存 MAC,再注册自定义 log,确保 vprintf 钩子里能直接读 g_mac_str。
|
||
CacheMacAddress();
|
||
esp_log_set_vprintf(LogVprintfWithWallClock);
|
||
|
||
// docs/0019 §10.1 · 设备启动即生成一个初始 trace/session。后续 WS 连接成功
|
||
// 时再 RefreshSession,每次对话回合再 RefreshTrace。
|
||
::borla::TraceCtx::RefreshSession();
|
||
::borla::TraceCtx::RefreshTrace("dev");
|
||
|
||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||
InitializeNvs();
|
||
|
||
auto& app = Application::GetInstance();
|
||
app.Start();
|
||
}
|