user974168
user974168

Reputation: 237

redirect subprocess log to wxpython txt ctrl

I would like to capture log o/p from a python based subprocess . Here is part of my code. How do I ridirect my log also to this txt ctrl Here is mytest.py:

import logging     
log=logging.getLogger('test')    
class MyTestClass():           
    def TestFunction(self) :    
    log.info("start function"
    # runs for 5 - 10 mins and has lots of log statments   
    print "some stuff"      
    log.info("after Test Function")
    # for now
    return a,b    
    #sys.exit(2)    

if __name__ == "__main__":   
  myApp=MyTestClass()  
  myApp.TestFunction()

I am doing something of this sort in my maingui:

    class WxLog(logging.Handler):
           def __init__(self, ctrl):
                logging.Handler.__init__(self)
                self.ctrl = ctrl
           def emit(self, record):
                  if self.ctrl:
                        self.ctrl.AppendText(self.format(record)+"\n") 

and in my gui

    self.log = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE| wx.TE_RICH2)
    #logging.basicConfig(level=logging.INFO)
    self.logr = logging.getLogger('')
    self.logr.setLevel(logging.INFO)
    hdlr = WxLog(self.log)
    hdlr.setFormatter(logging.Formatter('%(message)s '))
    self.logr.addHandler(hdlr)

    #snip 

    prog = os.path.join(mydir,"mytest.py")
    params = [sys.executable,prog]
    # Start the subprocess
    outmode = subprocess.PIPE
    errmode = subprocess.STDOUT
    self._proc = subprocess.Popen(params,
                                      stdout=outmode,
                                      stderr=errmode,
                                      shell=True
                                     )


    # Read from stdout while there is output from process
    while self._proc.poll() == None:
         txt = self._proc.stdout.readline()
         print txt

    # also direct log to txt ctrl 
    txt =  'Return code was ' + str(self._proc.returncode) +'\n'   

    # direct     
    self.logr.info("On end ")

Upvotes: 1

Views: 958

Answers (2)

Mike Driscoll
Mike Driscoll

Reputation: 33071

I wrote an article about how I redirect a few things like ping and traceroute using subprocess to my TextCtrl widget here: http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/

That might help you figure it out. Here's a more generic article that doesn't use subprocess: http://www.blog.pythonlibrary.org/2009/01/01/wxpython-redirecting-stdout-stderr/

I haven't tried redirecting with the logging module yet, but that may be something I'll do in the future.

Upvotes: 1

Vinay Sajip
Vinay Sajip

Reputation: 99365

You can try following the suggestion in this post.

Update: You can set the logger in the subprocess to use a SocketHandler and set up a socket server in the GUI to listen for messages from the subprocess, using the technique in the linked-to post to actually make things appear in the GUI. A working socket server is included in the logging documentation.

Upvotes: 1

Related Questions