jsonlogger: add test file and improve open/close behaviour

This commit is contained in:
Thomas Kolb 2024-05-05 18:40:50 +02:00
parent 251aca7738
commit 5bcd3a6933
3 changed files with 121 additions and 13 deletions

View file

@ -15,14 +15,36 @@
}
static int m_fifo_fd = -1;
static char m_fifo_path[1024];
static bool start_message(void)
{
if(m_fifo_fd < 0) {
// FIFO was not opened yet. Try to open it now.
m_fifo_fd = open(m_fifo_path, O_WRONLY | O_NONBLOCK);
if(m_fifo_fd < 0) {
if(errno != ENXIO) {
perror("open");
}
return false;
}
}
ssize_t ret = write(m_fifo_fd, "{", 1);
if(ret < 0) {
// FIFO is not writable or other error
if(errno != EAGAIN) {
perror("write");
switch(errno) {
case EAGAIN: // FIFO not ready for writing -> drop message
break;
case EPIPE: // FIFO was closed on the other side
close(m_fifo_fd);
m_fifo_fd = -1;
break;
default: // other errors are logged to stderr
perror("write");
break;
}
return false;
@ -58,12 +80,9 @@ bool jsonlogger_init(const char *fifoname)
}
}
// FIFO was created or did already exist -> open it
m_fifo_fd = open(fifoname, O_WRONLY | O_NONBLOCK);
if(m_fifo_fd < 0) {
perror("open");
return false;
}
strncpy(m_fifo_path, fifoname, sizeof(m_fifo_path));
m_fifo_fd = -1; // open on demand
return true;
}
@ -108,7 +127,7 @@ bool jsonlogger_log_rx_stats(const rx_stats_t *rx_stats)
LOG_OR_RETURN("\"preambles_found\": %zd, ", rx_stats->preambles_found);
LOG_OR_RETURN("\"successful_decodes\": %zd, ", rx_stats->successful_decodes);
LOG_OR_RETURN("\"failed_decodes\": %zd, ", rx_stats->failed_decodes);
LOG_OR_RETURN("\"header_errors\": %zd, ", rx_stats->header_errors);
LOG_OR_RETURN("\"header_errors\": %zd", rx_stats->header_errors);
return end_message();
}

View file

@ -11,7 +11,7 @@ add_executable(
target_link_libraries(
test_correlator
m
liquid
/usr/local/lib64/libliquid.a
)
#------------------------------------
@ -46,7 +46,7 @@ add_executable(
target_link_libraries(
test_layer1_loopback
m
liquid
/usr/local/lib64/libliquid.a
fftw3
fftw3f
fec
@ -66,7 +66,7 @@ target_link_libraries(
test_freq_est
fftw3f
m
liquid
/usr/local/lib64/libliquid.a
)
#------------------------------------
@ -98,5 +98,22 @@ target_link_libraries(
fec
fftw3f
m
liquid
/usr/local/lib64/libliquid.a
)
#------------------------------------
add_executable(
test_jsonlogger
../src/jsonlogger.c
../src/jsonlogger.h
../src/debug_structs.h
test_jsonlogger.c
)
target_link_libraries(
test_freq_est
fftw3f
m
/usr/local/lib64/libliquid.a
)

View file

@ -0,0 +1,72 @@
#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");
}