1 """GNUmed exception handling widgets."""
2
3
4
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
28 global _client_version
29 _client_version = version
30
32 global _sender_email
33 _sender_email = email
34
36 global _helpdesk
37 _helpdesk = helpdesk
38
40 global _staff_name
41 _staff_name = staff_name
42
44 global _is_public_database
45 _is_public_database = value
46
48
49 _log2.debug('unhandled exception caught:', exc_info = (t, v, tb))
50
51
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
59 try: wx.EndBusyCursor()
60 except: pass
61
62
63 if application_is_closing:
64
65 if t == wx._core.PyDeadObjectError:
66 return
67 gmLog2.log_stack_trace()
68 return
69
70
71 if t == wx._core.PyDeadObjectError:
72 _log.warning('continuing and hoping for the best')
73 return
74
75
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
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
146
153
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397