将子进程日志重定向到 wxpython txt ctrl

发布于 2024-12-15 04:34:49 字数 1950 浏览 1 评论 0原文

我想从基于 python 的子进程捕获日志 o/p 。这是我的代码的一部分。如何将我的日志也重定向到此 txt ctrl 这是 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()

我正在我的 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") 

在我的 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 ")

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 ")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-12-22 04:34:49

您可以尝试按照这篇文章中的建议进行操作。

更新:您可以在子进程中设置记录器以使用SocketHandler,并在 GUI 中设置套接字服务器来侦听来自子进程的消息,使用链接到的帖子实际上使内容出现在 GUI 中。工作套接字服务器包含在 记录文档

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.

恏ㄋ傷疤忘ㄋ疼 2024-12-22 04:34:49

我在这里写了一篇关于如何使用子进程将 ping 和 traceroute 等一些内容重定向到我的 TextCtrl 小部件的文章: http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/

这可能会帮助你弄清楚。这是一篇不使用子进程的更通用的文章: http://www.blog.pythonlibrary.org/2009/01/01/wxpython-redirecting-stdout-stderr/

我没有尝试过尚未使用日志记录模块进行重定向,但这可能是我将来会做的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文