bolar_2 backup
This commit is contained in:
177
main/diagnostics/device_log_formatter.cc
Normal file
177
main/diagnostics/device_log_formatter.cc
Normal file
@@ -0,0 +1,177 @@
|
||||
#include "diagnostics/device_log_formatter.h"
|
||||
|
||||
#include "diagnostics/trace_context.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_log_timestamp.h>
|
||||
#include <esp_mac.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cinttypes>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <sys/time.h>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr char kStandardHeaderEnd[] = ") %s: ";
|
||||
char g_device_mac[18] = "MAC-UNAVAILABLE";
|
||||
bool g_initialized = false;
|
||||
bool g_mac_available = false;
|
||||
bool g_at_line_start = true;
|
||||
std::atomic<bool> g_clock_synchronized{false};
|
||||
|
||||
void FormatTimestamp(char* output, size_t capacity)
|
||||
{
|
||||
if (output == nullptr || capacity == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
timeval now{};
|
||||
gettimeofday(&now, nullptr);
|
||||
if (g_clock_synchronized.load(std::memory_order_acquire) &&
|
||||
now.tv_sec >= 1700000000LL) {
|
||||
tm local{};
|
||||
const time_t seconds = static_cast<time_t>(now.tv_sec);
|
||||
if (localtime_r(&seconds, &local) != nullptr) {
|
||||
const size_t calendar_size = std::strftime(
|
||||
output,
|
||||
capacity,
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
&local);
|
||||
if (calendar_size != 0 &&
|
||||
capacity >= calendar_size + 5) {
|
||||
const unsigned milliseconds =
|
||||
static_cast<unsigned>(now.tv_usec / 1000) % 1000;
|
||||
output[calendar_size] = '.';
|
||||
output[calendar_size + 1] = static_cast<char>(
|
||||
'0' + milliseconds / 100);
|
||||
output[calendar_size + 2] = static_cast<char>(
|
||||
'0' + (milliseconds / 10) % 10);
|
||||
output[calendar_size + 3] = static_cast<char>(
|
||||
'0' + milliseconds % 10);
|
||||
output[calendar_size + 4] = '\0';
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::snprintf(
|
||||
output,
|
||||
capacity,
|
||||
"TIME-UNSYNCED+%" PRIu32 "ms",
|
||||
esp_log_timestamp());
|
||||
}
|
||||
|
||||
int SumPrinted(int first, int second)
|
||||
{
|
||||
if (first < 0 || second < 0) {
|
||||
return -1;
|
||||
}
|
||||
return first + second;
|
||||
}
|
||||
|
||||
bool FormatEndsLine(const char* format)
|
||||
{
|
||||
const size_t size = std::strlen(format);
|
||||
return size != 0 && format[size - 1] == '\n';
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool DeviceLogFormatter::Initialize()
|
||||
{
|
||||
if (g_initialized) {
|
||||
return g_mac_available;
|
||||
}
|
||||
|
||||
uint8_t mac[6]{};
|
||||
if (esp_read_mac(mac, ESP_MAC_WIFI_STA) == ESP_OK) {
|
||||
std::snprintf(
|
||||
g_device_mac,
|
||||
sizeof(g_device_mac),
|
||||
// Lower case to match the identity MAC and the previous firmware's
|
||||
// prefix (main.cc), so one grep finds both the log lines and the
|
||||
// device_id the server knows this board by.
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
g_mac_available = true;
|
||||
}
|
||||
TraceContext::Initialize(g_device_mac);
|
||||
g_initialized = true;
|
||||
esp_log_set_vprintf(&DeviceLogFormatter::Print);
|
||||
return g_mac_available;
|
||||
}
|
||||
|
||||
void DeviceLogFormatter::MarkClockSynchronized()
|
||||
{
|
||||
g_clock_synchronized.store(true, std::memory_order_release);
|
||||
}
|
||||
|
||||
int DeviceLogFormatter::Print(
|
||||
const char* format,
|
||||
va_list arguments)
|
||||
{
|
||||
if (format == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if CONFIG_LOG_TIMESTAMP_SOURCE_RTOS
|
||||
const char* const level_marker = std::strstr(format, " (");
|
||||
const char* const header_end =
|
||||
std::strstr(format, kStandardHeaderEnd);
|
||||
if (level_marker != nullptr && level_marker != format &&
|
||||
header_end != nullptr && level_marker < header_end) {
|
||||
va_list message_arguments;
|
||||
va_copy(message_arguments, arguments);
|
||||
(void)va_arg(message_arguments, uint32_t);
|
||||
const char* const tag =
|
||||
va_arg(message_arguments, const char*);
|
||||
const char level = *(level_marker - 1);
|
||||
const char* const message_format =
|
||||
header_end + sizeof(kStandardHeaderEnd) - 1;
|
||||
char timestamp[40]{};
|
||||
FormatTimestamp(timestamp, sizeof(timestamp));
|
||||
|
||||
flockfile(stdout);
|
||||
const int prefix_size = std::fprintf(
|
||||
stdout,
|
||||
"[%s][%s][%s] %c %s: ",
|
||||
g_device_mac,
|
||||
timestamp,
|
||||
TraceContext::TraceId(),
|
||||
level,
|
||||
tag == nullptr ? "unknown" : tag);
|
||||
const int message_size = std::vfprintf(
|
||||
stdout, message_format, message_arguments);
|
||||
g_at_line_start = FormatEndsLine(message_format);
|
||||
funlockfile(stdout);
|
||||
va_end(message_arguments);
|
||||
return SumPrinted(prefix_size, message_size);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Some binary components emit one logical line through several vprintf
|
||||
// calls. Prefix only the first fragment; prefixing every callback corrupts
|
||||
// WiFi logs into several apparent lines.
|
||||
flockfile(stdout);
|
||||
int prefix_size = 0;
|
||||
if (g_at_line_start) {
|
||||
char timestamp[40]{};
|
||||
FormatTimestamp(timestamp, sizeof(timestamp));
|
||||
prefix_size = std::fprintf(
|
||||
stdout,
|
||||
"[%s][%s][%s] ",
|
||||
g_device_mac,
|
||||
timestamp,
|
||||
TraceContext::TraceId());
|
||||
}
|
||||
const int message_size = std::vfprintf(stdout, format, arguments);
|
||||
g_at_line_start = FormatEndsLine(format);
|
||||
funlockfile(stdout);
|
||||
return SumPrinted(prefix_size, message_size);
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user