143 lines
3.6 KiB
C++
143 lines
3.6 KiB
C++
#include "diagnostics/trace_context.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
#if __has_include(<esp_timer.h>)
|
|
#include <esp_timer.h>
|
|
#define BORLA_TRACE_HAS_ESP_TIMER 1
|
|
#else
|
|
#include <chrono>
|
|
#define BORLA_TRACE_HAS_ESP_TIMER 0
|
|
#endif
|
|
|
|
namespace borla {
|
|
namespace {
|
|
|
|
constexpr const char kUnset[] = "-";
|
|
|
|
char g_device_id[TraceContext::kIdCapacity] = "dev";
|
|
char g_trace_id[TraceContext::kIdCapacity] = "-";
|
|
char g_session_id[TraceContext::kIdCapacity] = "-";
|
|
char g_request_id[TraceContext::kIdCapacity] = "-";
|
|
|
|
uint64_t MonotonicMs()
|
|
{
|
|
#if BORLA_TRACE_HAS_ESP_TIMER
|
|
return static_cast<uint64_t>(esp_timer_get_time() / 1000LL);
|
|
#else
|
|
using namespace std::chrono;
|
|
return static_cast<uint64_t>(
|
|
duration_cast<milliseconds>(
|
|
steady_clock::now().time_since_epoch())
|
|
.count());
|
|
#endif
|
|
}
|
|
|
|
} // namespace
|
|
|
|
size_t TraceContext::FormatId(
|
|
const char* prefix,
|
|
const char* device_id,
|
|
uint64_t monotonic_ms,
|
|
char* output,
|
|
size_t capacity)
|
|
{
|
|
if (output == nullptr || capacity == 0) {
|
|
return 0;
|
|
}
|
|
const char* safe_prefix =
|
|
prefix != nullptr && prefix[0] != '\0' ? prefix : "dev";
|
|
const char* safe_device =
|
|
device_id != nullptr && device_id[0] != '\0' ? device_id : "dev";
|
|
const int written = std::snprintf(
|
|
output,
|
|
capacity,
|
|
"%s-%s-%llu",
|
|
safe_prefix,
|
|
safe_device,
|
|
static_cast<unsigned long long>(monotonic_ms));
|
|
if (written < 0) {
|
|
output[0] = '\0';
|
|
return 0;
|
|
}
|
|
// snprintf already truncated and terminated; report what actually fits so
|
|
// callers never see a length past the buffer.
|
|
return static_cast<size_t>(written) < capacity
|
|
? static_cast<size_t>(written)
|
|
: capacity - 1;
|
|
}
|
|
|
|
void TraceContext::Assign(
|
|
const char* value,
|
|
char* output,
|
|
size_t capacity)
|
|
{
|
|
if (output == nullptr || capacity == 0) {
|
|
return;
|
|
}
|
|
if (value == nullptr || value[0] == '\0') {
|
|
std::snprintf(output, capacity, "%s", kUnset);
|
|
return;
|
|
}
|
|
std::snprintf(output, capacity, "%s", value);
|
|
}
|
|
|
|
void TraceContext::Initialize(const char* device_id)
|
|
{
|
|
// MAC colons would collide with the "field=value" log grammar the server
|
|
// greps, so they are folded to dashes here once.
|
|
char sanitized[kIdCapacity]{};
|
|
std::snprintf(
|
|
sanitized,
|
|
sizeof(sanitized),
|
|
"%s",
|
|
device_id != nullptr && device_id[0] != '\0' ? device_id : "dev");
|
|
for (size_t index = 0; sanitized[index] != '\0'; ++index) {
|
|
if (sanitized[index] == ':' || sanitized[index] == ' ') {
|
|
sanitized[index] = '-';
|
|
}
|
|
}
|
|
std::memcpy(g_device_id, sanitized, sizeof(g_device_id));
|
|
g_device_id[sizeof(g_device_id) - 1] = '\0';
|
|
}
|
|
|
|
void TraceContext::RefreshTrace(const char* prefix)
|
|
{
|
|
FormatId(
|
|
prefix, g_device_id, MonotonicMs(),
|
|
g_trace_id, sizeof(g_trace_id));
|
|
}
|
|
|
|
const char* TraceContext::NewRequestId()
|
|
{
|
|
FormatId(
|
|
"req", g_device_id, MonotonicMs(),
|
|
g_request_id, sizeof(g_request_id));
|
|
return g_request_id;
|
|
}
|
|
|
|
const char* TraceContext::RefreshSession()
|
|
{
|
|
FormatId(
|
|
"s", g_device_id, MonotonicMs(),
|
|
g_session_id, sizeof(g_session_id));
|
|
return g_session_id;
|
|
}
|
|
|
|
void TraceContext::SetSession(const char* session_id)
|
|
{
|
|
Assign(session_id, g_session_id, sizeof(g_session_id));
|
|
}
|
|
|
|
void TraceContext::SetTrace(const char* trace_id)
|
|
{
|
|
Assign(trace_id, g_trace_id, sizeof(g_trace_id));
|
|
}
|
|
|
|
const char* TraceContext::TraceId() { return g_trace_id; }
|
|
const char* TraceContext::SessionId() { return g_session_id; }
|
|
const char* TraceContext::RequestId() { return g_request_id; }
|
|
|
|
} // namespace borla
|