Even more robust logger, up to the really last line that comes after the rush
authorEdouard Tisserant
Thu, 16 Feb 2012 00:19:27 +0100
changeset 688 6324b40d82a5
parent 687 c59baa44ec2f
child 689 dac752b613bb
Even more robust logger, up to the really last line that comes after the rush
Beremiz.py
--- a/Beremiz.py	Wed Feb 15 19:17:09 2012 +0100
+++ b/Beremiz.py	Thu Feb 16 00:19:27 2012 +0100
@@ -283,7 +283,7 @@
             dc.DrawBitmap(self._bitmap, 0, 0, True)
 
                         
-from threading import Lock
+from threading import Lock,Timer
 REFRESH_PERIOD = 0.1
 from time import time as gettime
 class LogPseudoFile:
@@ -301,14 +301,21 @@
         self.RefreshLock = Lock()
         self.stack = []
         self.LastRefreshTime = gettime()
+        self.LastRefreshTimer = None
 
     def write(self, s, style = None):
         if self.lock.acquire():
             self.stack.append((s,style))
             self.lock.release()
             current_time = gettime()
+            if self.LastRefreshTimer:
+                self.LastRefreshTimer.cancel()
+                self.LastRefreshTimer=None
             if current_time - self.LastRefreshTime > REFRESH_PERIOD and self.RefreshLock.acquire(False):
                 wx.CallAfter(self._write)
+            else:
+                self.LastRefreshTimer = Timer(REFRESH_PERIOD, wx.CallAfter, [self._write])
+                self.LastRefreshTimer.start()
 
     def _write(self):
         self.output.Freeze(); 
@@ -325,7 +332,10 @@
         self.output.ShowPosition(self.output.GetLastPosition())
         self.output.Thaw()
         self.LastRefreshTime = gettime()
-        self.RefreshLock.release()
+        try:
+            self.RefreshLock.release()
+        except:
+            pass
         newtime = time.time()
         if newtime - self.rising_timer > 1:
             self.risecall()