Added systematic stdout.flush runtime side, so that results appear in log window
authoretisserant
Wed, 14 Jan 2009 19:25:53 +0100
changeset 291 701c0601db02
parent 290 3bd617ae7a05
child 292 a47081eb6cec
Added systematic stdout.flush runtime side, so that results appear in log window
runtime/PLCObject.py
tests/python/beremiz.xml
tests/python/runtime.py
--- a/runtime/PLCObject.py	Wed Jan 14 16:41:14 2009 +0100
+++ b/runtime/PLCObject.py	Wed Jan 14 19:25:53 2009 +0100
@@ -25,6 +25,7 @@
 import Pyro.core as pyro
 from threading import Timer, Thread
 import ctypes, os, commands
+import sys
 
 if os.name in ("nt", "ce"):
     from _ctypes import LoadLibrary as dlopen
@@ -39,6 +40,10 @@
      "win32":".dll",
      }.get(sys.platform, "")
 
+def PLCprint(message):
+    sys.stdout.write("PLCobject : "+message+"\n")
+    sys.stdout.flush()
+
 class PLCObject(pyro.ObjBase):
     _Idxs = []
     def __init__(self, workingdir, daemon, argv, statuschange=None):
@@ -78,7 +83,7 @@
         Load PLC library
         Declare all functions, arguments and return values
         """
-        print "Load PLC"
+        PLCprint("Load PLC")
         try:
             self._PLClibraryHandle = dlopen(self._GetLibFileName())
             self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle)
@@ -119,7 +124,7 @@
             
             return True
         except:
-            print traceback.format_exc()
+            PLCprint(traceback.format_exc())
             return False
 
     def _FreePLC(self):
@@ -141,7 +146,7 @@
         self.PLClibraryHandle = None
         # Unload library explicitely
         if getattr(self,"_PLClibraryHandle",None) is not None:
-            print "Unload PLC"
+            PLCprint("Unload PLC")
             dlclose(self._PLClibraryHandle)
             res = self._DetectDirtyLibs()
         else:
@@ -168,13 +173,12 @@
                                   "libX11",
                                   ]:
                     #badhandle = dlopen(badlib, dl.RTLD_NOLOAD)
-                    print "Dirty lib detected :" + badlib
+                    PLCprint("Dirty lib detected :" + badlib)
                     #dlclose(badhandle)
                     return True
         return False
 
-    def PythonThreadProc(self):
-        print "PythonThreadProc started"
+    def ExecRuntimePy(self):
         self.python_threads_vars = globals().copy()
         pyfile = os.path.join(self.workingdir, "runtime.py")
         if os.path.exists(pyfile):
@@ -182,6 +186,14 @@
             # pyfile may redefine _runtime_cleanup
             # or even call _PythonThreadProc itself.
             execfile(pyfile, self.python_threads_vars)
+
+    def FinishRuntimePy(self):
+        if self.python_threads_vars.get("_runtime_cleanup",None) is not None:
+            self.python_threads_vars["_runtime_cleanup"]()
+        self.python_threads_vars = None
+
+    def PythonThreadProc(self):
+        PLCprint("PythonThreadProc started")
         res,cmd = "None","None"
         while self.PLCStatus == "Started":
             #print "_PythonIterator(", res, ")",
@@ -193,15 +205,11 @@
                 res = str(eval(cmd,self.python_threads_vars))
             except Exception,e:
                 res = "#EXCEPTION : "+str(e)
-                print res
-        print "PythonThreadProc interrupted"
-        if self.python_threads_vars.get("_runtime_cleanup",None) is not None:
-            self.python_threads_vars["_runtime_cleanup"]()
-        self.python_threads_vars = None
-        print "PythonThreadProc cleaned up"
+                PLCprint(res)
+        PLCprint("PythonThreadProc interrupted")
     
     def StartPLC(self, debug=False):
-        print "StartPLC"
+        PLCprint("StartPLC")
         if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped":
             c_argv = ctypes.c_char_p * len(self.argv)
             if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0:
@@ -209,11 +217,12 @@
                     self._resumeDebug()
                 self.PLCStatus = "Started"
                 self.StatusChange()
+                self.ExecRuntimePy()
                 self.PythonThread = Thread(target=self.PythonThreadProc)
                 self.PythonThread.start()
                 return True
             else:
-                print "_StartPLC did not return 0 !"
+                PLCprint("Problem starting PLC")
                 self._DoStopPLC()
         return False
 
@@ -222,7 +231,8 @@
         self.PLCStatus = "Stopped"
         self.PythonThread.join(timeout=1)
         if self.PythonThread.isAlive():
-            print "Python thread couldn't be killed"
+            PLCprint("Python thread couldn't be killed")
+        self.FinishRuntimePy()
         if self._FreePLC():
             self.PLCStatus = "Dirty"
         self.StatusChange()
@@ -250,7 +260,7 @@
         return self.PLCStatus
     
     def NewPLC(self, md5sum, data, extrafiles):
-        print "NewPLC (%s)"%md5sum
+        PLCprint("NewPLC (%s)"%md5sum)
         if self.PLCStatus in ["Stopped", "Empty", "Dirty"]:
             NewFileName = md5sum + lib_ext
             extra_files_log = os.path.join(self.workingdir,"extra_files.txt")
@@ -283,7 +293,7 @@
                 # Store new PLC filename
                 self.CurrentPLCFilename = NewFileName
             except:
-                print traceback.format_exc()
+                PLCprint(traceback.format_exc())
                 return False
             if self.PLCStatus == "Empty":
                 self.PLCStatus = "Stopped"
@@ -359,7 +369,7 @@
                         res.append(unpack_func(ctypes.cast(buffer,
                                                            ctypes.POINTER(c_type)).contents))
                     else:
-                        print "Debug error idx : %d, expected_idx %d, type : %s"%(idx.value, given_idx,typename.value)
+                        PLCprint("Debug error idx : %d, expected_idx %d, type : %s"%(idx.value, given_idx,typename.value))
                         res.append(None)
             self._FreeDebugData()
             return tick, res
--- a/tests/python/beremiz.xml	Wed Jan 14 16:41:14 2009 +0100
+++ b/tests/python/beremiz.xml	Wed Jan 14 19:25:53 2009 +0100
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<BeremizRoot URI_location="PYRO://127.0.0.1:3000">
+<BeremizRoot URI_location="LOCAL://">
   <TargetType>
     <Linux CFLAGS="-g" LDFLAGS="-g"/>
   </TargetType>
--- a/tests/python/runtime.py	Wed Jan 14 16:41:14 2009 +0100
+++ b/tests/python/runtime.py	Wed Jan 14 19:25:53 2009 +0100
@@ -1,4 +1,5 @@
-import time
+import time,sys
 def myprintfunc(arg):
     print arg
+    sys.stdout.flush()
     return arg