diff --git a/impl/src/jsonlogger.c b/impl/src/jsonlogger.c index eef5837..4bb824d 100644 --- a/impl/src/jsonlogger.c +++ b/impl/src/jsonlogger.c @@ -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(); } diff --git a/impl/test/CMakeLists.txt b/impl/test/CMakeLists.txt index 1d4e1a8..69089ec 100644 --- a/impl/test/CMakeLists.txt +++ b/impl/test/CMakeLists.txt @@ -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 ) diff --git a/impl/test/test_jsonlogger.c b/impl/test/test_jsonlogger.c new file mode 100644 index 0000000..dee7ee7 --- /dev/null +++ b/impl/test/test_jsonlogger.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +#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"); +}