2024-05-05 18:09:00 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "jsonlogger.h"
|
|
|
|
|
|
|
|
#define LOG_OR_RETURN(...) \
|
|
|
|
if(dprintf(m_fifo_fd, __VA_ARGS__) < 0) { \
|
|
|
|
return false; \
|
|
|
|
}
|
|
|
|
|
|
|
|
static int m_fifo_fd = -1;
|
2024-05-05 18:40:50 +02:00
|
|
|
static char m_fifo_path[1024];
|
2024-05-05 18:09:00 +02:00
|
|
|
|
|
|
|
static bool start_message(void)
|
|
|
|
{
|
2024-05-05 18:40:50 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-05 18:09:00 +02:00
|
|
|
ssize_t ret = write(m_fifo_fd, "{", 1);
|
|
|
|
if(ret < 0) {
|
|
|
|
// FIFO is not writable or other error
|
2024-05-05 18:40:50 +02:00
|
|
|
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;
|
2024-05-05 18:09:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool end_message(void)
|
|
|
|
{
|
|
|
|
ssize_t ret = write(m_fifo_fd, "}\n", 2);
|
|
|
|
if(ret < 0) {
|
|
|
|
// FIFO is not writable or other error
|
|
|
|
if(errno != EAGAIN) {
|
|
|
|
perror("write");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool jsonlogger_init(const char *fifoname)
|
|
|
|
{
|
|
|
|
// try to create the named FIFO
|
|
|
|
int ret = mkfifo(fifoname, 0666);
|
|
|
|
if(ret < 0) {
|
|
|
|
if(errno != EEXIST) {
|
|
|
|
perror("mkfifo");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-05 18:40:50 +02:00
|
|
|
strncpy(m_fifo_path, fifoname, sizeof(m_fifo_path));
|
|
|
|
|
|
|
|
m_fifo_fd = -1; // open on demand
|
2024-05-05 18:09:00 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void jsonlogger_shutdown(void)
|
|
|
|
{
|
|
|
|
close(m_fifo_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool jsonlogger_log_simple_integer(const char *msg_type, int64_t value)
|
|
|
|
{
|
|
|
|
if(!start_message()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_OR_RETURN("\"%s\": %ld", msg_type, value);
|
|
|
|
|
|
|
|
return end_message();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool jsonlogger_log_simple_double(const char *msg_type, double value)
|
|
|
|
{
|
|
|
|
if(!start_message()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_OR_RETURN("\"%s\": %g", msg_type, value);
|
|
|
|
|
|
|
|
return end_message();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool jsonlogger_log_rx_stats(const rx_stats_t *rx_stats)
|
|
|
|
{
|
|
|
|
if(!start_message()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2024-05-05 18:40:50 +02:00
|
|
|
LOG_OR_RETURN("\"header_errors\": %zd", rx_stats->header_errors);
|
2024-05-05 18:09:00 +02:00
|
|
|
|
|
|
|
return end_message();
|
|
|
|
}
|