]> mj.ucw.cz Git - home-hw.git/blobdiff - bsb/logger/burrow-bsb-logger.c
fixup!
[home-hw.git] / bsb / logger / burrow-bsb-logger.c
index 4fbef56bb9701da3d5eb695111c5462962058655..f3489effa45ab7d78a53abeeb781b86ba29fff4a 100644 (file)
@@ -1,13 +1,14 @@
 /*
  *     A BSB frame logger
  *
- *     (c) 2020 Martin Mares <mj@ucw.cz>
+ *     (c) 2020--2022 Martin Mares <mj@ucw.cz>
  */
 
 #include <ucw/lib.h>
 #include <ucw/fastbuf.h>
 #include <ucw/log.h>
 #include <ucw/opt.h>
+#include <ucw/string.h>
 
 #include <fcntl.h>
 #include <stdio.h>
@@ -20,6 +21,8 @@
 #include <curl/curl.h>
 #include <mosquitto.h>
 
+#define LOG_DIR "/var/log/bsb"
+
 /*** 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);