How can I capture all exceptions from a wxPython application?

前端 未结 4 666
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 15:50

I\'m writing a little debug app for a bit of kit we\'re developing and I\'d like to roll it out to a few users to see if they can provoke any crashes. Does anyone know a way

4条回答
  •  [愿得一人]
    2020-12-29 16:13

    For logging standard output, you can use a stdout wrapper, such as this one:

    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)
    

    You then have to initialize it in your main Python file (the one that runs everything):

    import sys    
    sys.stdout = OutWrapper(sys.stdout, r'c:\temp\log.txt')
    

    As to logging exceptions, the easiest thing to do is to wrap MainLoop method of wx.App in a try..except, then extract the exception information, save it in some way, and then re-raise the exception through raise, e.g.:

    try:
        app.MainLoop()
    except:
        exc_info = sys.exc_info()
        saveExcInfo(exc_info) # this method you have to write yourself
        raise
    

提交回复
热议问题