Trying to get better logging/display of exceptions at runtime's startup
authorEdouard Tisserant
Wed, 24 Jan 2018 15:44:36 +0100
changeset 1906 60edd0c901f1
parent 1905 87c908079bc6
child 1907 9d17b9d91697
Trying to get better logging/display of exceptions at runtime's startup
Beremiz_service.py
runtime/PLCObject.py
--- a/Beremiz_service.py	Wed Jan 24 14:48:49 2018 +0100
+++ b/Beremiz_service.py	Wed Jan 24 15:44:36 2018 +0100
@@ -538,12 +538,18 @@
 # Exception hooks s
 
 
+
+def LogMessageAndException(msg, exp=None):
+    if exp is None:
+        exp = sys.exc_info()
+    if pyroserver.plcobj is not None:
+        pyroserver.plcobj.LogMessage(0, msg + '\n'.join(traceback.format_exception(*exp)))
+    else:
+        print(msg)
+        traceback.print_exception(*exp)
+
 def LogException(*exp):
-    if pyroserver.plcobj is not None:
-        pyroserver.plcobj.LogMessage(0, '\n'.join(traceback.format_exception(*exp)))
-    else:
-        traceback.print_exception(*exp)
-
+    LogExceptionAndMessage("",exp)
 
 sys.excepthook = LogException
 
@@ -600,8 +606,8 @@
             website = NS.RegisterWebsite(webport)
             pyruntimevars["website"] = website
             statuschange.append(NS.website_statuslistener_factory(website))
-        except Exception, e:
-            print(_("Nevow Web service failed. "), e)
+        except Exception:
+            LogMessageAndException(_("Nevow Web service failed. "))
 
     if wampconf is not None:
         try:
@@ -612,11 +618,11 @@
                     pyruntimevars["wampsession"] = WC.GetSession
                     WC.SetServer(pyroserver)
                 else:
-                    print(_("WAMP config is incomplete."))
+                    raise Exception(_("WAMP config is incomplete."))
             else:
-                print(_("WAMP config is missing."))
-        except Exception, e:
-            print(_("WAMP client startup failed. "), e)
+                raise Exception(_("WAMP config is missing."))
+        except Exception:
+            LogMessageAndException(_("WAMP client startup failed. "))
 
 
 if havetwisted or havewx:
@@ -630,7 +636,7 @@
 else:
     try:
         pyroserver.Loop()
-    except KeyboardInterrupt, e:
+    except KeyboardInterrupt:
         pass
 pyroserver.Quit()
 sys.exit(0)
--- a/runtime/PLCObject.py	Wed Jan 24 14:48:49 2018 +0100
+++ b/runtime/PLCObject.py	Wed Jan 24 15:44:36 2018 +0100
@@ -106,7 +106,10 @@
         else:
             level = LogLevelsDefault
             msg, = args
-        return self._LogMessage(level, msg, len(msg))
+        PLCprint(msg)
+        if self._LogMessage is not None:
+            return self._LogMessage(level, msg, len(msg))
+        return None
 
     def ResetLogCount(self):
         if self._ResetLogCount is not None:
@@ -254,7 +257,7 @@
         self._resumeDebug = lambda: None
         self._PythonIterator = lambda: ""
         self._GetLogCount = None
-        self._LogMessage = lambda l, m, s: PLCprint("OFF LOG :"+m)
+        self._LogMessage = None
         self._GetLogMessage = None
         self.PLClibraryHandle = None
         # Unload library explicitely