commit
444af0b257
1 changed files with 76 additions and 0 deletions
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
#include "eventlog.h" |
||||
|
||||
#include <stdbool.h> |
||||
#include <stdio.h> |
||||
#include <stdint.h> |
||||
#include <signal.h> |
||||
#include <unistd.h> |
||||
#include <time.h> |
||||
|
||||
static uint64_t gs_head = 0; |
||||
|
||||
static void tracer_format_timestamp(uint64_t ns, char* buffer, size_t buffer_size) |
||||
{ |
||||
time_t sec = ns / 1000000000ULL; |
||||
uint32_t ms = (ns % 1000000000ULL) / 1000000; |
||||
|
||||
struct tm* tm_info = localtime(&sec); |
||||
|
||||
snprintf(buffer, buffer_size, "%04d-%02d-%02d %02d:%02d:%02d.%03u", |
||||
tm_info->tm_year + 1900, |
||||
tm_info->tm_mon + 1, |
||||
tm_info->tm_mday, |
||||
tm_info->tm_hour, |
||||
tm_info->tm_min, |
||||
tm_info->tm_sec, |
||||
ms); |
||||
} |
||||
|
||||
static void dump_events_to_file(const char* filepath) |
||||
{ |
||||
FILE* fp = fopen(filepath, "w+"); |
||||
size_t actual_size = 0; |
||||
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) |
||||
{ |
||||
tracer_format_timestamp(entries[i].timestamp, timestamp_buffer, sizeof(timestamp_buffer)); |
||||
fprintf(fp, "[%s] %s\n", timestamp_buffer, entries[i].data); |
||||
} |
||||
} |
||||
} while(actual_size != 0); |
||||
|
||||
fclose(fp); |
||||
} |
||||
|
||||
static void signal_handler(int sig) |
||||
{ |
||||
dump_events_to_file("/tmp/eventlog.json"); |
||||
} |
||||
|
||||
int main(int argc, char** argv) |
||||
{ |
||||
int ret = eventlog_init("/eventlog", EVENTLOG_MODE_CREATE); |
||||
if(ret < 0) |
||||
{ |
||||
fprintf(stderr, "Can't create evenlog: %d\n", ret); |
||||
return -1; |
||||
} |
||||
|
||||
bool running = true; |
||||
signal(SIGUSR1, signal_handler); |
||||
int i = 0; |
||||
while (running) |
||||
{ |
||||
eventlog_emit_event(eventlog_get_timestamp(), "Iteration %d", i); |
||||
sleep(1); |
||||
i++; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
Loading…
Reference in new issue