commit 9187b7690fba5af9448685e13fc44a4c7cd4b278
Author: MFori <forejt.martin97@gmail.com>
Date:   Mon May 10 14:20:45 2021 +0200

    Re: #8893 - add log messages

diff --git a/aswi2021vochomurka/app.py b/aswi2021vochomurka/app.py
index 15f9fd0..ea63143 100644
--- a/aswi2021vochomurka/app.py
+++ b/aswi2021vochomurka/app.py
@@ -16,17 +16,18 @@ class Application(QApplication):
 
 def init_logger():
     logging.basicConfig(
-        level=logging.INFO,
+        level=logging.DEBUG,
         filename='data/app.log',
         format='%(asctime)s %(name)s: %(levelname)-8s %(message)s',
         datefmt='%y-%m-%d %H:%M',
-        filemode='w'
+        filemode='w+'
     )
 
-    console = logging.StreamHandler()
-    console.setLevel(logging.INFO)
     formatter = logging.Formatter('%(asctime)s %(name)s: %(levelname)-8s %(message)s')
+    console = logging.StreamHandler()
     console.setFormatter(formatter)
+    console.setLevel(logging.INFO)
     logging.getLogger('').addHandler(console)
 
-    logging.getLogger('apscheduler.scheduler').setLevel(logging.WARNING)
+    logging.getLogger('apscheduler').setLevel(logging.WARNING)
+    logging.getLogger('matplotlib').setLevel(logging.WARNING)
diff --git a/aswi2021vochomurka/service/file_manager.py b/aswi2021vochomurka/service/file_manager.py
index 12d4fc3..7f29c4a 100644
--- a/aswi2021vochomurka/service/file_manager.py
+++ b/aswi2021vochomurka/service/file_manager.py
@@ -1,3 +1,4 @@
+import logging
 import os
 import time
 
@@ -26,13 +27,14 @@ class FileManager:
 
     def __init__(self, topic: str, message: Message):
         self.topic = topic
+        logging.debug('opening file ' + self.topic)
 
         try:
             filename = create_filename(message)
             os.makedirs(os.path.dirname(filename), exist_ok=True)
             self.file = open(filename, "w+")
         except Exception as error:
-            print(error)
+            logging.warning(error)
             raise error
         self.write(message)
 
@@ -41,5 +43,6 @@ class FileManager:
         self.lastUpdate = time.time()
 
     def close(self):
+        logging.debug('closing file ' + self.topic)
         self.file.flush()
         self.file.close()
diff --git a/aswi2021vochomurka/service/message_parser.py b/aswi2021vochomurka/service/message_parser.py
index 4d37064..7c9a81a 100644
--- a/aswi2021vochomurka/service/message_parser.py
+++ b/aswi2021vochomurka/service/message_parser.py
@@ -1,3 +1,5 @@
+import logging
+
 from paho.mqtt.client import MQTTMessage
 
 from aswi2021vochomurka.model.Message import Message
@@ -10,6 +12,7 @@ class ParseException(Exception):
 def parse_mqtt_message(message: MQTTMessage) -> Message:
     data = message.payload.decode("utf-8")
     parts = data.split(";")
+    logging.debug('Parsing message: ' + data + ', parts: ' + str(len(parts)))
     if len(parts) != 4:
         raise ParseException
 
diff --git a/aswi2021vochomurka/service/mqtt/mqtt_subscriber.py b/aswi2021vochomurka/service/mqtt/mqtt_subscriber.py
index 226331e..bba2456 100644
--- a/aswi2021vochomurka/service/mqtt/mqtt_subscriber.py
+++ b/aswi2021vochomurka/service/mqtt/mqtt_subscriber.py
@@ -1,3 +1,5 @@
+import logging
+
 import paho.mqtt.client as mqtt
 
 from aswi2021vochomurka.service.message_parser import parse_mqtt_message, ParseException
@@ -8,29 +10,30 @@ class MQTTSubscriber(Subscriber):
 
     # The callback for when the client receives a CONNACK response from the server.
     def on_connect(self, client, userdata, flags, rc, properties=None):
-        print("Connected with result code " + str(rc))
+        logging.info('Connected with result code ' + str(rc))
         self.callback.onConnected()
 
         # Subscribing in on_connect() means that if we lose the connection and
         # reconnect then subscriptions will be renewed.
         for topic in self.params.topics:
-            print("Subscribed to topic: " + topic)
+            logging.info('Subscribed to topic: ' + topic)
             client.subscribe(topic)
 
     # The callback for when a PUBLISH message is received from the server.
     def on_message(self, client, userdata, message: mqtt.MQTTMessage):
         try:
             m = parse_mqtt_message(message)
-            print(m)
+            logging.info(m)
             self.callback.onMessage(m)
             self.write_to_file(m)
         except ParseException as error:
-            print('invalid message data format')
+            logging.warning('Invalid message data format of: ' + message.payload.decode('utf-8'))
+            logging.info('Unsubscribed topic: ' + message.topic)
             client.unsubscribe(message.topic)
-            # TODO better reaction on bad format
             pass
 
     def on_disconnect(self, client, userdata, rc):
+        logging.info('Disconnected')
         self.callback.onDisconnected()
         self.stop()
 
@@ -40,18 +43,22 @@ class MQTTSubscriber(Subscriber):
         client.on_connect = self.on_connect
         client.on_message = self.on_message
         client.on_disconnect = self.on_disconnect
+        logging.info('Setting up client')
 
         if not self.params.anonymous:
+            logging.info('Using credentials, username=' + self.params.username + ', password=' + self.params.password)
             client.tls_set()
             client.username_pw_set(self.params.username, self.params.password)
 
         try:
+            logging.info('Connecting to: ' + self.params.connection.host + ':' + str(self.params.connection.port))
             client.connect(
                 self.params.connection.host,
                 self.params.connection.port,
                 self.params.connection.timeout
             )
         except Exception as error:
+            logging.warning('Cannot connect')
             self.callback.onError()
             return
 
