| Home | Trees | Indices | Help |
|
|---|
|
|
1 """GNUmed exception handling widgets."""
2 # ========================================================================
3 # $Source: /home/ncq/Projekte/cvs2git/vcs-mirror/gnumed/gnumed/client/wxpython/gmExceptionHandlingWidgets.py,v $
4 # $Id: gmExceptionHandlingWidgets.py,v 1.17 2010-01-31 18:15:55 ncq Exp $
5 __version__ = "$Revision: 1.17 $"
6 __author__ = "K. Hilbert <Karsten.Hilbert@gmx.net>"
7 __license__ = "GPL (details at http://www.gnu.org)"
8
9 import logging, exceptions, traceback, re as regex, sys, os, shutil, datetime as pyDT, codecs
10
11
12 import wx
13
14
15 from Gnumed.business import gmSurgery
16 from Gnumed.pycommon import gmDispatcher, gmTools, gmCfg2, gmI18N, gmLog2
17 from Gnumed.wxpython import gmGuiHelpers
18 from Gnumed.wxGladeWidgets import wxgUnhandledExceptionDlg
19
20
21 _log2 = logging.getLogger('gm.gui')
22 _log2.info(__version__)
23
24 _prev_excepthook = None
25 application_is_closing = False
26 #=========================================================================
30 #-------------------------------------------------------------------------
34 #-------------------------------------------------------------------------
38 #-------------------------------------------------------------------------
42 #-------------------------------------------------------------------------
46 #-------------------------------------------------------------------------
48
49 _log2.debug('unhandled exception caught:', exc_info = (t, v, tb))
50
51 # Strg-C ?
52 if t == KeyboardInterrupt:
53 print "<Ctrl-C>: Shutting down ..."
54 top_win = wx.GetApp().GetTopWindow()
55 wx.CallAfter(top_win.Close)
56 return
57
58 # careful: MSW does reference counting on Begin/End* :-(
59 try: wx.EndBusyCursor()
60 except: pass
61
62 # exception on shutdown ?
63 if application_is_closing:
64 # dead object error ?
65 if t == wx._core.PyDeadObjectError:
66 return
67 gmLog2.log_stack_trace()
68 return
69
70 # try to ignore those, they come about from async handling
71 if t == wx._core.PyDeadObjectError:
72 _log.warning('continuing and hoping for the best')
73 return
74
75 # failed import ?
76 if t == exceptions.ImportError:
77 gmGuiHelpers.gm_show_error (
78 aTitle = _('Missing GNUmed module'),
79 aMessage = _(
80 'GNUmed detected that parts of it are not\n'
81 'properly installed. The following message\n'
82 'names the missing part:\n'
83 '\n'
84 ' "%s"\n'
85 '\n'
86 'Please make sure to get the missing\n'
87 'parts installed. Otherwise some of the\n'
88 'functionality will not be accessible.'
89 ) % v
90 )
91 _log2.error('module [%s] not installed', v)
92 return
93
94 # other exceptions
95 _cfg = gmCfg2.gmCfgData()
96 if _cfg.get(option = 'debug') is False:
97 _log2.error('enabling debug mode')
98 _cfg.set_option(option = 'debug', value = True)
99 root_logger = logging.getLogger()
100 root_logger.setLevel(logging.DEBUG)
101 _log2.debug('unhandled exception caught:', exc_info = (t, v, tb))
102
103 gmLog2.log_stack_trace()
104
105 name = os.path.basename(_logfile_name)
106 name, ext = os.path.splitext(name)
107 new_name = os.path.expanduser(os.path.join (
108 '~',
109 'gnumed',
110 'logs',
111 '%s_%s%s' % (name, pyDT.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'), ext)
112 ))
113
114 dlg = cUnhandledExceptionDlg(parent = None, id = -1, exception = (t, v, tb), logfile = new_name)
115 dlg.ShowModal()
116 comment = dlg._TCTRL_comment.GetValue()
117 dlg.Destroy()
118 if (comment is not None) and (comment.strip() != u''):
119 _log2.error(u'user comment: %s', comment.strip())
120
121 _log2.warning('syncing log file for backup to [%s]', new_name)
122 gmLog2.flush()
123 shutil.copy2(_logfile_name, new_name)
124 # ------------------------------------------------------------------------
126
127 global _logfile_name
128 _logfile_name = gmLog2._logfile_name
129
130 global _local_account
131 _local_account = os.path.basename(os.path.expanduser('~'))
132
133 set_helpdesk(gmSurgery.gmCurrentPractice().helpdesk)
134 set_staff_name(_local_account)
135 set_is_public_database(False)
136 set_sender_email(None)
137 set_client_version('gmExceptionHandlingWidgets.py %s' % __version__)
138
139 gmDispatcher.connect(signal = 'application_closing', receiver = _on_application_closing)
140
141 global _prev_excepthook
142 _prev_excepthook = sys.excepthook
143 sys.excepthook = handle_uncaught_exception_wx
144
145 return True
146 # ------------------------------------------------------------------------
148 if _prev_excepthook is None:
149 sys.excepthook = sys.__excepthook__
150 return True
151 sys.excepthook = _prev_excepthook
152 return True
153 # ------------------------------------------------------------------------
155 global application_is_closing
156 # used to ignore a few exceptions, such as when the
157 # C++ object has been destroyed before the Python one
158 application_is_closing = True
159 # ========================================================================
161
163
164 exception = kwargs['exception']
165 del kwargs['exception']
166 self.logfile = kwargs['logfile']
167 del kwargs['logfile']
168
169 wxgUnhandledExceptionDlg.wxgUnhandledExceptionDlg.__init__(self, *args, **kwargs)
170
171 if _sender_email is not None:
172 self._TCTRL_sender.SetValue(_sender_email)
173 self._TCTRL_helpdesk.SetValue(_helpdesk)
174 self._TCTRL_logfile.SetValue(self.logfile)
175 t, v, tb = exception
176 self._TCTRL_exc_type.SetValue(str(t))
177 self._TCTRL_exc_value.SetValue(str(v))
178 self._TCTRL_traceback.SetValue(''.join(traceback.format_tb(tb)))
179
180 self.Fit()
181 #------------------------------------------
192 #------------------------------------------
335 #------------------------------------------
341 # ========================================================================
342 # $Log: gmExceptionHandlingWidgets.py,v $
343 # Revision 1.17 2010-01-31 18:15:55 ncq
344 # - ignore one more PyDeadObjectError
345 #
346 # Revision 1.16 2009/12/21 15:06:05 ncq
347 # - better layout
348 #
349 # Revision 1.15 2009/07/30 12:04:06 ncq
350 # - better handle Ctrl-C
351 #
352 # Revision 1.14 2009/05/22 11:01:23 ncq
353 # - better catch exceptions on mailing log
354 #
355 # Revision 1.13 2009/05/08 07:59:55 ncq
356 # - improved default version
357 #
358 # Revision 1.12 2009/04/19 22:27:00 ncq
359 # - cleanup
360 #
361 # Revision 1.11 2009/04/03 12:30:16 ncq
362 # - attach log rather than include
363 #
364 # Revision 1.10 2009/02/24 10:13:02 ncq
365 # - -devel -> -bugs
366 #
367 # Revision 1.9 2009/02/20 15:43:05 ncq
368 # - typo fix
369 #
370 # Revision 1.8 2009/02/05 14:29:27 ncq
371 # - improved report mail reporting
372 #
373 # Revision 1.7 2008/12/25 23:31:51 ncq
374 # - ignore but log most exceptions during application shutdown
375 #
376 # Revision 1.6 2008/12/09 23:29:54 ncq
377 # - trap exceptions during smtp handling inside top-level exception handler
378 #
379 # Revision 1.5 2008/11/20 19:50:45 ncq
380 # - improved wording
381 #
382 # Revision 1.4 2008/10/12 16:17:57 ncq
383 # - include client version at top of bug email
384 # - improved launchpad tracking tags
385 #
386 # Revision 1.3 2008/07/28 20:26:49 ncq
387 # - fixed include_log logic
388 #
389 # Revision 1.2 2008/07/16 11:10:46 ncq
390 # - set_sender_email and use it
391 # - some cleanup and better docs
392 #
393 # Revision 1.1 2008/05/13 12:32:54 ncq
394 # - factor out exception handling widgets
395 #
396 #
397
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Mon Jun 28 04:11:17 2010 | http://epydoc.sourceforge.net |