gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:05:35 25 4
gpt4 key购买 nike

我想从基于 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")

在我的图形用户界面中

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

最佳答案

您可以尝试按照this post中的建议进行操作.

更新:您可以在子进程中设置记录器以使用 SocketHandler 并在 GUI 中设置套接字服务器以监听来自子进程的消息,使用链接帖子中的技术实际上使内容出现在 GUI 中。 logging documentation 中包含一个工作套接字服务器.

关于python - 将子进程日志重定向到 wxpython txt ctrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8123962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com