Ver código fonte

creation of logfile only if logging is enabled

master
Dirk Alders 1 ano atrás
pai
commit
ed1be6be48
1 arquivos alterados com 7 adições e 4 exclusões
  1. 7
    4
      mqtt_sniffer.py

+ 7
- 4
mqtt_sniffer.py Ver arquivo

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

Carregando…
Cancelar
Salvar