hamnet70/impl/test/test_jsonlogger.c
Thomas Kolb 2e91fd7c42 Apply free software+documentation licenses
All code is now licensed under GPLv3+. The documentation is licensed under
CC BY-SA 4.0.

This is now officially free software! \o/
2024-08-23 11:53:40 +02:00

86 lines
1.6 KiB
C

/*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Copyright (C) 2024 Thomas Kolb
* Copyright (C) 2024 Simon Ruderich
*/
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "logger.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;
LOG(LVL_INFO, "Gracefully shutting down on signal %d.", signal);
m_running = false;
}
int main(void)
{
logger_init();
if(!jsonlogger_init("jsonlog_test.fifo")) {
LOG(LVL_ERR, "Could not initialize JSON logger.");
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) {
LOG(LVL_ERR, "sigaction: %s", strerror(errno));
exit(EXIT_FAILURE);
}
if(sigaction(SIGINT, &term_action, NULL) < 0) {
LOG(LVL_ERR, "sigaction: %s", strerror(errno));
exit(EXIT_FAILURE);
}
// ignore SIGPIPE
struct sigaction term_action_ign = {
.sa_handler = SIG_IGN,
};
if(sigaction(SIGPIPE, &term_action_ign, NULL) < 0) {
LOG(LVL_ERR, "sigaction: %s", strerror(errno));
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();
LOG(LVL_INFO, "Done.");
}