from __future__ import with_statement
class OutWrapper(object):
def __init__(self, realOutput, logFileName):
self._realOutput = realOutput
self._logFileName = logFileName
def _log(self, text):
with open(self._logFileName, 'a') as logFile:
logFile.write(text)
def write(self, text):
self._log(text)
self._realOutput.write(text)
然后,您必须在主Python文件(运行所有文件的文件)中对其进行初始化:
import sys
sys.stdout = OutWrapper(sys.stdout, r'c:\temp\log.txt')
对于记录异常,最简单的MainLoop
方法是将wx.App的方法包装在try..except中,然后提取异常信息,以某种方式保存它,然后通过重新引发异常raise
,例如:
try:
app.MainLoop()
except:
exc_info = sys.exc_info()
saveExcInfo(exc_info) # this method you have to write yourself
raise