win32traceutil.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # This is a helper for the win32trace module
  2. # If imported from a normal Python program, it sets up sys.stdout and sys.stderr
  3. # so output goes to the collector.
  4. # If run from the command line, it creates a collector loop.
  5. # Eg:
  6. # C:>start win32traceutil.py (or python.exe win32traceutil.py)
  7. # will start a process with a (pretty much) blank screen.
  8. #
  9. # then, switch to a DOS prompt, and type:
  10. # C:>python.exe
  11. # Python 1.4 etc...
  12. # >>> import win32traceutil
  13. # Redirecting output to win32trace remote collector
  14. # >>> print "Hello"
  15. # >>>
  16. # And the output will appear in the first collector process.
  17. # Note - the client or the collector can be started first.
  18. # There is a 0x20000 byte buffer. If this gets full, it is reset, and new
  19. # output appended from the start.
  20. import win32trace
  21. def RunAsCollector():
  22. import sys
  23. try:
  24. import win32api
  25. win32api.SetConsoleTitle("Python Trace Collector")
  26. except:
  27. pass # Oh well!
  28. win32trace.InitRead()
  29. print("Collecting Python Trace Output...")
  30. try:
  31. while 1:
  32. # a short timeout means ctrl+c works next time we wake...
  33. sys.stdout.write(win32trace.blockingread(500))
  34. except KeyboardInterrupt:
  35. print("Ctrl+C")
  36. def SetupForPrint():
  37. win32trace.InitWrite()
  38. try: # Under certain servers, sys.stdout may be invalid.
  39. print("Redirecting output to win32trace remote collector")
  40. except:
  41. pass
  42. win32trace.setprint() # this works in an rexec environment.
  43. if __name__=='__main__':
  44. RunAsCollector()
  45. else:
  46. SetupForPrint()