/*
* 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>
#include <curl/curl.h>
#include <mosquitto.h>
+#define LOG_DIR "/var/log"
+
/*** MQTT ***/
static struct mosquitto *mosq;
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);
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 ***/
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);