creation of logfile only if logging is enabled

This commit is contained in:
Dirk Alders 2023-08-01 09:18:43 +02:00
parent 7c00cf30f0
commit ed1be6be48

View File

@ -9,6 +9,7 @@ import time
VERSION = "0.1.0"
STARTTIME = time.time()
logfile = None
HELPTEXT = """
F1: Get this help message
@ -48,6 +49,8 @@ def rx_mqtt(mc, userdata, message):
if match:
print("%9.04f::%75s::%s" % (ts, message.topic, data))
if my_bb.get_entry('log2file'):
if logfile is None:
logfile = open('mqtt_sniffer.log', 'w')
logfile.write("%9.04f::%s::%s\n" % (ts, message.topic, data))
logfile.flush()
@ -76,7 +79,7 @@ if __name__ == "__main__":
my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True)
my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True)
with open('mqtt_sniffer.log', 'w') as logfile:
mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
mc.add_callback("#", rx_mqtt)
my_bb.run()
mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
mc.add_callback("#", rx_mqtt)
my_bb.run()