Hi.

 

I noticed that you want to create a global exception handler for dabo. I
have created something like that for use in my wx applications that
might be of interest. It's far from complete, but you can see the basic
concepts. I wanted to be able to automatically send error reports to a
specified e-mail upon crash (not yet done, but not to big change
needed). So far it just catched unhandled exceptions and writes a
crashlog and has basic logging capabilities. Still, you might be
interested in having a look.

 

# -*- encoding: iso8859-1 -*-

"""

CrashNotify is a top-level exceptionhandler created to log unhandled
exceptions

"""

 

__version__ = '0.1'

__author__  = 'Simen Haugen'

 

try:

    import logging

    import logging.handlers

    import wx

    import traceback

    import sys

    import datetime

except ImportError:

    raise ImportError, 'Could not load CrashNotify. Missing modules?'

 

class CrashinfoDialog(wx.Dialog):

    def __init__(self, parent=None, id=-1, title="Ooops... You did
something we didn't think of!", 

                pos=wx.DefaultPosition, size=wx.DefaultSize, 

                style=wx.DEFAULT_DIALOG_STYLE, name=wx.DialogNameStr,

                data={}, crashdata=''):

        wx.Dialog.__init__(self, parent, id, title, pos, size, style,
name)

        

        self.data = data

        self.crashdata = crashdata

        self.CreateControls()

        

    def GetComment(self):

        return self.comment.GetValue()

    

    def CreateControls(self):

        topSizer = wx.BoxSizer(wx.VERTICAL)

        topSizer.Add(wx.StaticText(self, label=self.data['crashinfo']),

                     0, wx.ALL|wx.GROW, 5)

        

        topSizer.Add(wx.StaticLine(self), 0, wx.ALL|wx.GROW, 5)

        topSizer.Add(wx.StaticText(self, label='Feilmelding'), 0,
wx.ALL, 5)

        crashdata = wx.TextCtrl(self, value=self.crashdata,
style=wx.TE_MULTILINE)

        crashdata.SetEditable(False)

        topSizer.Add(crashdata, 1, wx.ALL|wx.GROW, 5)

        

        topSizer.Add(wx.StaticText(self, label='Kommentar'), 0, wx.ALL,
5)

        comment = wx.TextCtrl(self, style=wx.TE_MULTILINE)

        comment.Enable(False) #FIXME: remove as soon as mail is working

        topSizer.Add(comment, 1, wx.ALL|wx.GROW, 5)

        topSizer.Add(wx.StaticLine(self), 0, wx.ALL|wx.GROW, 5)

        

        self.sendBtn = wx.Button(self, label='Send feilrapport')

        self.sendBtn.Enable(False)

        btnsizer = wx.BoxSizer(wx.HORIZONTAL)

        btnsizer.Add(self.sendBtn, 1, wx.ALL, 5)

        btnsizer.Add(wx.Button(self, wx.ID_CANCEL), 1, wx.ALL, 5)

 

        topSizer.Add(btnsizer, 0, wx.ALL|wx.CENTRE, 5)

        

        self.SetSizer(topSizer)

        self.Fit()

 

class CrashNotify:

    defaultCrashInformation = '''The application has crashed.

                        Note the following information and contact your
system administrator.'''

 

    def __init__(self, appName='Unknown', appVersion='?.?.?',
author='Unknown',

                 email='Unknown', crashinfo=defaultCrashInformation):

        sys.excepthook = self.exceptionhook

 

        self.crashData = {}

 

        self.appData = {}

        self.appData['name']      = appName

        self.appData['version']   = appVersion

        self.appData['author']    = author

        self.appData['email']     = email

        self.appData['crashinfo'] = crashinfo

        

        formatter = logging.Formatter('%(asctime)s %(levelname)s
%(message)s')

        

        #internal logger

        self.log = logging.getLogger('CrashNotify')

        self.log.setLevel(logging.DEBUG)

        logFileHandler = logging.FileHandler('CrashNotify.log')

        logFileHandler.setFormatter(formatter)

        self.log.addHandler(logFileHandler)

        

        #logger for the application

        self.appLog = logging.getLogger(self.appData['name'])

        self.appLog.setLevel(logging.DEBUG)

        #file

        appFileHandler = logging.FileHandler(self.appData['name'] +
'.log')

        appFileHandler.setFormatter(formatter)

        self.appLog.addHandler(appFileHandler)

        

        #FIXME: Find a workaround for crashes caused by no connection
here

        #mail

        #appSmtpHandler =
logging.handlers.SMTPHandler('deframx04.softcom.dk', '[EMAIL PROTECTED]',
'[EMAIL PROTECTED]', 'Crash')

        #appSmtpHandler.setFormatter(formatter)

        #self.appLog.addHandler(appSmtpHandler)

    def setAuthor(self, author):

        self.appData['author'] = author

        

    def setEmail(self, email):

        self.appData['email'] = email

    

    def setCrashinfo(self, crashinfo):

        self.appData['crashinfo']= crashinfo

        

    def exceptionhook(self, type, value, tb):

        self.crashData['time'] = datetime.datetime.now()

        self.crashData['timeString'] = str(self.crashData['time'])

        self.crashData['exceptionType'] = str(type)

        self.crashData['exceptionValue'] = str(value)

        self.crashData['line'] = str(traceback.tb_lineno(tb))

        self.crashData['traceback'] = tb

        self.crashData['tracebackString'] =
''.join(traceback.format_exception(type, value, tb))

 

        self.writeCrashDump()

 

        try:

            app = wx.GetApp()

            if not app:

                app = wx.PySimpleApp()

            dlg =
CrashinfoDialog(data=self.appData,crashdata=self.formatCrashData())

            dlg.ShowModal()

        except Exception, e:

            self.log.error(str(e))

            print str(e)

        

    def logging(self):

        return self.appLog

    

    def formatCrashData(self):

        if len(self.crashData) == 0:

            return ''

        

        return 'E-mail: '    + self.appData['email'] +\

               '\nAuthor: '  + self.appData['author'] +\

               '\n\nName: '  + self.appData['name'] +\

               '\nVersion: ' + self.appData['version'] +\

               '\nTime: '    + self.crashData['timeString'] +\

               '\n\nType: '  + self.crashData['exceptionType'] +\

               '\nValue: '   + self.crashData['exceptionValue'] +\

               '\nLine: '    + self.crashData['line'] +\

               '\n\n'        + self.crashData['tracebackString']

        

    def writeCrashDump(self):

        filename = self.appData['name'] + '.crash'

        file = open(filename, 'a')

        file.write(self.formatCrashData())

        file.close()

        return filename

        



--- StripMime Report -- processed MIME parts ---
multipart/alternative
  text/plain (text body -- kept)
  text/html
---

_______________________________________________
Post Messages to: Dabo-users@leafe.com
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users

Reply via email to