From 8618b382aebc4488d04b1dc72e7184b3598ebcb7 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 1 Jan 2022 20:34:38 +0100 Subject: [PATCH] BSB logger: Log file names based on date --- bsb/logger/burrow-bsb-logger.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/bsb/logger/burrow-bsb-logger.c b/bsb/logger/burrow-bsb-logger.c index 4fbef56..8a2d412 100644 --- a/bsb/logger/burrow-bsb-logger.c +++ b/bsb/logger/burrow-bsb-logger.c @@ -1,13 +1,14 @@ /* * A BSB frame logger * - * (c) 2020 Martin Mares + * (c) 2020--2022 Martin Mares */ #include #include #include #include +#include #include #include @@ -20,6 +21,8 @@ #include #include +#define LOG_DIR "/var/log" + /*** MQTT ***/ static struct mosquitto *mosq; @@ -89,6 +92,8 @@ static void mqtt_connect(void) die("Mosquitto: connect failed"); } +static struct log_stream *packet_log, *stat_log; + static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m) { time_t now = time(NULL); @@ -101,16 +106,10 @@ static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, c val[m->payloadlen] = 0; msg(L_DEBUG, "MQTT < %s %s", m->topic, val); - static struct fastbuf *logfile; - if (!logfile) - logfile = bopen_file("/var/log/bsb-frames", O_WRONLY | O_CREAT | O_APPEND, NULL); - if (!strcmp(m->topic, "bsb/frame")) - bprintf(logfile, "%jd %s\n", (uintmax_t) time(NULL), val); - - // FIXME: Log statistics - - bflush(logfile); + msg(L_INFO | packet_log->regnum, "%s", val); + else if (str_has_prefix(m->topic, "bsb/stats/")) + msg(L_INFO | stat_log->regnum, "%s:%s", m->topic + 10, val); } /*** Main ***/ @@ -143,6 +142,11 @@ int main(int argc UNUSED, char **argv) if (!use_debug) log_default_stream()->levels &= ~(1U << L_DEBUG); + packet_log = log_new_file(LOG_DIR "/frames-%Y%m%d", 0); + packet_log->msgfmt = LSFMT_TIME; + stat_log = log_new_file(LOG_DIR "/stats-%Y%m%d", 0); + stat_log->msgfmt = LSFMT_TIME; + mqtt_connect(); int err = mosquitto_loop_forever(mosq, 10000, 1); -- 2.39.2