From 3339e3c6d80feb821cd4cac0bba89fd04fe24d0c Mon Sep 17 00:00:00 2001 From: Denis Tereshkin Date: Thu, 20 Nov 2025 22:03:31 +0700 Subject: [PATCH] Custom event tracing --- CMakeLists.txt | 2 +- src/main.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c01808..87b581c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ find_path(EVENTLOG_INCLUDE add_executable(eventlog-daemon ${eventlog_daemon_src}) -target_include_directories(eventlog-daemon PUBLIC ${EVENTLOG_INCLUDE}) +target_include_directories(eventlog-daemon PUBLIC ${EVENTLOG_INCLUDE} ${EVENTLOG_EXTRA_INCLUDE_PATH}) target_link_libraries(eventlog-daemon ${EVENTLOG_LIB}) diff --git a/src/main.c b/src/main.c index 10b866c..54e9ffe 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,3 @@ -#include "eventlog.h" #include #include @@ -7,6 +6,10 @@ #include #include +#define EVENTLOG_DAEMON 1 +#include "eventlog.h" +#include "events.h" + static uint64_t gs_head = 0; static void tracer_format_timestamp(uint64_t ns, char* buffer, size_t buffer_size) @@ -26,6 +29,24 @@ static void tracer_format_timestamp(uint64_t ns, char* buffer, size_t buffer_siz ms); } +void print_event(const struct eventlog_entry* e, char* buf, size_t buf_size) +{ + for (size_t i = 0; i < g_event_registry_size; ++i) { + if (g_event_registry[i].id == e->type_id) { + char ts_buf[32]; + tracer_format_timestamp(e->timestamp, ts_buf, sizeof(ts_buf)); + int chars = snprintf(buf, buf_size, "[%s] ", ts_buf); + if (chars <= 0) + { + return; + } + g_event_registry[i].printer(e->data, buf + chars, buf_size - chars); + return; + } + } + printf("Unknown event ID: %u\n", e->type_id); +} + static void dump_events_to_file(const char* filepath) { FILE* fp = fopen(filepath, "w+"); @@ -33,15 +54,21 @@ static void dump_events_to_file(const char* filepath) struct eventlog_entry entries[64]; do { - char timestamp_buffer[256]; eventlog_get_snapshot(&gs_head, entries, 64, &actual_size); for (size_t i = 0; i < actual_size; i++) { if (entries[i].type_id == EVENTLOG_TYPE_STRING) { + char timestamp_buffer[32]; tracer_format_timestamp(entries[i].timestamp, timestamp_buffer, sizeof(timestamp_buffer)); fprintf(fp, "[%s] %s\n", timestamp_buffer, entries[i].data); } + else + { + char buf[256]; + print_event(&entries[i], buf, sizeof(buf)); + fprintf(fp, "%s\n", buf); + } } } while(actual_size != 0);