codekingpro/portable-devtools
114k
1# win32traceutil like utility for Pythonwin2import _thread3 4import win32api5import win32event6import win32trace7from pywin.framework import winout8 9outputWindow = None10 11 12def CollectorThread(stopEvent, file):13 win32trace.InitRead()14 handle = win32trace.GetHandle()15 # Run this thread at a lower priority to the main message-loop (and printing output)16 # thread can keep up17 import win32process18 19 win32process.SetThreadPriority(20 win32api.GetCurrentThread(), win32process.THREAD_PRIORITY_BELOW_NORMAL21 )22 23 try:24 while 1:25 rc = win32event.WaitForMultipleObjects(26 (handle, stopEvent), 0, win32event.INFINITE27 )28 if rc == win32event.WAIT_OBJECT_0:29 # About the only char we can't live with is \0!30 file.write(win32trace.read().replace("\0", "<null>"))31 else:32 # Stop event33 break34 finally:35 win32trace.TermRead()36 print("Thread dieing")37 38 39class WindowOutput(winout.WindowOutput):40 def __init__(self, *args):41 winout.WindowOutput.__init__(*(self,) + args)42 self.hStopThread = win32event.CreateEvent(None, 0, 0, None)43 _thread.start_new(CollectorThread, (self.hStopThread, self))44 45 def _StopThread(self):46 win32event.SetEvent(self.hStopThread)47 self.hStopThread = None48 49 def Close(self):50 self._StopThread()51 winout.WindowOutput.Close(self)52 # def OnViewDestroy(self, frame):53 # return winout.WindowOutput.OnViewDestroy(self, frame)54 # def Create(self, title=None, style = None):55 # rc = winout.WindowOutput.Create(self, title, style)56 return rc57 58 59def MakeOutputWindow():60 # Note that it will not show until the first string written or61 # you pass bShow = 162 global outputWindow63 if outputWindow is None:64 title = "Python Trace Collector"65 # queueingFlag doesnt matter, as all output will come from new thread66 outputWindow = WindowOutput(title, title)67 # Let people know what this does!68 msg = """\69# This window will display output from any programs that import win32traceutil70# win32com servers registered with '--debug' are in this category.71"""72 outputWindow.write(msg)73 # force existing window open74 outputWindow.write("")75 return outputWindow76 77 78if __name__ == "__main__":79 MakeOutputWindow()80 