hamnet70/impl/test/test_jsonlogger.c

73 lines
1.3 KiB
C
Raw Normal View History

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "../src/jsonlogger.h"
#include "../src/debug_structs.h"
static rx_stats_t m_rx_stats;
static bool m_running;
static void signal_handler(int signal, siginfo_t *info, void *ctx)
{
(void)signal;
(void)info;
(void)ctx;
fprintf(stderr, "\nGracefully shutting down on signal %d.\n", signal);
m_running = false;
}
int main()
{
if(!jsonlogger_init("jsonlog_test.fifo")) {
fprintf(stderr, "Could not initialize JSON logger.\n");
return EXIT_FAILURE;
}
// ** Set up signal handling
struct sigaction term_action = {0};
term_action.sa_sigaction = signal_handler;
if(sigaction(SIGTERM, &term_action, NULL) < 0) {
perror("sigaction");
exit(EXIT_FAILURE);
}
if(sigaction(SIGINT, &term_action, NULL) < 0) {
perror("sigaction");
exit(EXIT_FAILURE);
}
// ignore SIGPIPE
term_action.sa_sigaction = SIG_IGN;
if(sigaction(SIGPIPE, &term_action, NULL) < 0) {
perror("sigaction");
exit(EXIT_FAILURE);
}
m_running = true;
while(m_running) {
m_rx_stats.preambles_found++;
m_rx_stats.successful_decodes += 2;
m_rx_stats.failed_decodes += 3;
m_rx_stats.header_errors += 4;
jsonlogger_log_rx_stats(&m_rx_stats);
sleep(1);
}
// ** Cleanup **
jsonlogger_shutdown();
fprintf(stderr, "Done.\n");
}