Compare commits

..

2 Commits

  1. 23
      CMakeLists.txt
  2. 31
      src/main.c

23
CMakeLists.txt

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.10)
project(eventlog-daemon)
set(eventlog_daemon_src
src/main.c)
find_library(EVENTLOG_LIB
NAMES eventlog
PATHS ${EVENTLOG_PATH} ${CMAKE_LIBRARY_PATH}
NO_DEFAULT_PATH)
find_path(EVENTLOG_INCLUDE
NAMES eventlog.h
PATHS ${EVENTLOG_INCLUDE_PATH} ${CMAKE_INCLUDE_PATH}
NO_DEFAULT_PATH)
add_executable(eventlog-daemon ${eventlog_daemon_src})
target_include_directories(eventlog-daemon PUBLIC ${EVENTLOG_INCLUDE} ${EVENTLOG_EXTRA_INCLUDE_PATH})
target_link_libraries(eventlog-daemon ${EVENTLOG_LIB})

31
src/main.c

@ -1,4 +1,3 @@ @@ -1,4 +1,3 @@
#include "eventlog.h"
#include <stdbool.h>
#include <stdio.h>
@ -7,6 +6,10 @@ @@ -7,6 +6,10 @@
#include <unistd.h>
#include <time.h>
#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 @@ -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) @@ -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);

Loading…
Cancel
Save