Browse Source

Custom event tracing

master
Denis Tereshkin 2 weeks ago
parent
commit
3339e3c6d8
  1. 2
      CMakeLists.txt
  2. 31
      src/main.c

2
CMakeLists.txt

@ -16,7 +16,7 @@ find_path(EVENTLOG_INCLUDE
add_executable(eventlog-daemon ${eventlog_daemon_src}) 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}) target_link_libraries(eventlog-daemon ${EVENTLOG_LIB})

31
src/main.c

@ -1,4 +1,3 @@
#include "eventlog.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@ -7,6 +6,10 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#define EVENTLOG_DAEMON 1
#include "eventlog.h"
#include "events.h"
static uint64_t gs_head = 0; static uint64_t gs_head = 0;
static void tracer_format_timestamp(uint64_t ns, char* buffer, size_t buffer_size) 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); 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) static void dump_events_to_file(const char* filepath)
{ {
FILE* fp = fopen(filepath, "w+"); FILE* fp = fopen(filepath, "w+");
@ -33,15 +54,21 @@ static void dump_events_to_file(const char* filepath)
struct eventlog_entry entries[64]; struct eventlog_entry entries[64];
do do
{ {
char timestamp_buffer[256];
eventlog_get_snapshot(&gs_head, entries, 64, &actual_size); eventlog_get_snapshot(&gs_head, entries, 64, &actual_size);
for (size_t i = 0; i < actual_size; i++) for (size_t i = 0; i < actual_size; i++)
{ {
if (entries[i].type_id == EVENTLOG_TYPE_STRING) if (entries[i].type_id == EVENTLOG_TYPE_STRING)
{ {
char timestamp_buffer[32];
tracer_format_timestamp(entries[i].timestamp, timestamp_buffer, sizeof(timestamp_buffer)); tracer_format_timestamp(entries[i].timestamp, timestamp_buffer, sizeof(timestamp_buffer));
fprintf(fp, "[%s] %s\n", timestamp_buffer, entries[i].data); 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); } while(actual_size != 0);

Loading…
Cancel
Save