]> 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 dbcc1545b85861b13f95ba3bc882464ea58294c0..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;
@@ -34,20 +37,18 @@ static void mqtt_publish(const char *topic, const char *fmt, ...)
        va_list args;
        va_start(args, fmt);
 
-       if (1) {
-               char m[256];
-               int l = vsnprintf(m, sizeof(m), fmt, args);
-               int err = mosquitto_publish(mosq, NULL, topic, l, m, 0, true);
-               if (err != MOSQ_ERR_SUCCESS)
-                       mqtt_error("publish", err, false);
-       }
+       char m[256];
+       int l = vsnprintf(m, sizeof(m), fmt, args);
+       int err = mosquitto_publish(mosq, NULL, topic, l, m, 0, true);
+       if (err != MOSQ_ERR_SUCCESS)
+               mqtt_error("publish", err, false);
 
        va_end(args);
 }
 
 static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message)
 {
-       msg(L_INFO, "MQTT(%d): %s", level, message);
+       // msg(L_INFO, "MQTT(%d): %s", level, message);
 }
 
 static void mqtt_conn_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int status)
@@ -91,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);
@@ -103,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 ***/
@@ -145,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);