More robust Logger, now resist to flooding.
authorEdouard Tisserant
Wed, 15 Feb 2012 18:52:31 +0100
changeset 686 e4e1da75d411
parent 685 2db8b87016a0
child 687 c59baa44ec2f
More robust Logger, now resist to flooding.
Removed wxCallAfter f*ck'up in ProcessLogger
Removed noisy useless warning when adding a plugin
Beremiz.py
plugger.py
wxPopen.py
--- a/Beremiz.py	Wed Feb 15 16:39:56 2012 +0100
+++ b/Beremiz.py	Wed Feb 15 18:52:31 2012 +0100
@@ -283,6 +283,9 @@
             dc.DrawBitmap(self._bitmap, 0, 0, True)
 
                         
+from threading import Lock
+REFRESH_PERIOD = 0.1
+from time import time as gettime
 class LogPseudoFile:
     """ Base class for file like objects to facilitate StdOut for the Shell."""
     def __init__(self, output, risecall):
@@ -294,21 +297,39 @@
         self.risecall = risecall
         # to prevent rapid fire on rising log panel
         self.rising_timer = 0
+        self.lock = Lock()
+        self.RefreshLock = Lock()
+        self.stack = []
+        self.LastRefreshTime = gettime()
 
     def write(self, s, style = None):
-        if style is None : style=self.black_white
+        if self.lock.acquire():
+            self.stack.append((s,style))
+            self.lock.release()
+            current_time = gettime()
+            if current_time - self.LastRefreshTime > REFRESH_PERIOD and self.RefreshLock.acquire(False):
+                wx.CallAfter(self._write)
+
+    def _write(self):
         self.output.Freeze(); 
-        if self.default_style != style: 
-            self.output.SetDefaultStyle(style)
-            self.default_style = style
-        self.output.AppendText(s)
-        self.output.ScrollLines(s.count('\n')+1)
+        self.lock.acquire()
+        for s, style in self.stack:
+            if style is None : style=self.black_white
+            if self.default_style != style: 
+                self.output.SetDefaultStyle(style)
+                self.default_style = style
+            self.output.AppendText(s)
+            #self.output.ScrollLines(s.count('\n')+1)
+        self.stack = []
+        self.lock.release()
         self.output.ShowPosition(self.output.GetLastPosition())
         self.output.Thaw()
+        self.LastRefreshTime = gettime()
+        self.RefreshLock.release()
         newtime = time.time()
         if newtime - self.rising_timer > 1:
             self.risecall()
-        self.rising_timer = newtime 
+        self.rising_timer = newtime
 
     def write_warning(self, s):
         self.write(s,self.red_white)
--- a/plugger.py	Wed Feb 15 16:39:56 2012 +0100
+++ b/plugger.py	Wed Feb 15 18:52:31 2012 +0100
@@ -498,8 +498,6 @@
                 res +=  1 # Test for n-1
         # Finally set IEC Channel
         self.BaseParams.setIEC_Channel(res)
-        if DesiredChannel != res:
-            self.GetPlugRoot().logger.write_warning(_("A child with IEC channel %d already exist -> %d\n")%(DesiredChannel,res))
         return res
 
     def _OpenView(self):
--- a/wxPopen.py	Wed Feb 15 16:39:56 2012 +0100
+++ b/wxPopen.py	Wed Feb 15 18:52:31 2012 +0100
@@ -126,19 +126,13 @@
         self.outdata += v
         self.outlen += 1
         if not self.no_stdout:
-            if wx.GetApp() is None:
-                self.logger.write(v)
-            else:
-                wx.CallAfter(self.logger.write,v)
+            self.logger.write(v)
             
     def errors(self,v):
         self.errdata += v
         self.errlen += 1
         if not self.no_stderr:
-            if wx.GetApp() is None:
-                self.logger.write_warning(v)
-            else:
-                wx.CallAfter(self.logger.write_warning,v)
+            self.logger.write_warning(v)
 
     def log_the_end(self,ecode,pid):
         self.logger.write(self.Command_str + "\n")
@@ -148,10 +142,7 @@
         self.finished = True
         self.exitcode = ecode
         if self.exitcode != 0:
-            if wx.GetApp() is None:
-                self.log_the_end(ecode,pid)
-            else:
-                wx.CallAfter(self.log_the_end,ecode,pid)
+            self.log_the_end(ecode,pid)
         if self.finish_callback is not None:
             self.finish_callback(self,ecode,pid)