andrej@1792: #!/usr/bin/env python andrej@1792: # -*- coding: utf-8 -*- andrej@1792: andrej@1792: # This file is part of Beremiz, a Integrated Development Environment for andrej@1792: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. andrej@1792: # andrej@1792: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD andrej@1792: # Copyright (C) 2016-2017: Andrey Skvortsov andrej@1792: # andrej@1792: # See COPYING file for copyrights details. andrej@1792: # andrej@1792: # This program is free software; you can redistribute it and/or andrej@1792: # modify it under the terms of the GNU General Public License andrej@1792: # as published by the Free Software Foundation; either version 2 andrej@1792: # of the License, or (at your option) any later version. andrej@1792: # andrej@1792: # This program is distributed in the hope that it will be useful, andrej@1792: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1792: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1792: # GNU General Public License for more details. andrej@1792: # andrej@1792: # You should have received a copy of the GNU General Public License andrej@1792: # along with this program; if not, write to the Free Software andrej@1792: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. andrej@1792: andrej@1881: kinsamanka@3750: andrej@1792: import os andrej@1792: import sys andrej@1792: import time andrej@1792: import tempfile andrej@1792: import platform andrej@1792: import traceback andrej@1792: import threading andrej@1792: import wx andrej@1792: andrej@1792: Max_Traceback_List_Size = 20 andrej@1792: andrej@1792: edouard@1941: def Display_Exception_Dialog(e_type, e_value, e_tb, bug_report_path, exit): andrej@1792: trcbck_lst = [] andrej@1792: for i, line in enumerate(traceback.extract_tb(e_tb)): andrej@1792: trcbck = " " + str(i+1) + ". " andrej@1792: if line[0].find(os.getcwd()) == -1: andrej@1792: trcbck += "file : " + str(line[0]) + ", " andrej@1792: else: andrej@1792: trcbck += "file : " + str(line[0][len(os.getcwd()):]) + ", " andrej@1792: trcbck += "line : " + str(line[1]) + ", " + "function : " + str(line[2]) andrej@1792: trcbck_lst.append(trcbck) andrej@1792: andrej@1792: # Allow clicking.... edouard@3303: cap = wx.Window.GetCapture() andrej@1792: if cap: andrej@1792: cap.ReleaseMouse() andrej@1792: andrej@1792: dlg = wx.SingleChoiceDialog( andrej@1792: None, andrej@1792: _(""" andrej@1792: An unhandled exception (bug) occured. Bug report saved at : andrej@1792: (%s) andrej@1792: andrej@1792: Please be kind enough to send this file to: andrej@1792: beremiz-devel@lists.sourceforge.net andrej@1792: andrej@1792: You should now restart program. andrej@1792: andrej@1792: Traceback: andrej@1792: """) % bug_report_path + andrej@1792: repr(e_type) + " : " + repr(e_value), andrej@1792: _("Error"), andrej@1792: trcbck_lst) andrej@1792: try: andrej@1792: res = (dlg.ShowModal() == wx.ID_OK) andrej@1792: finally: andrej@1792: dlg.Destroy() andrej@1792: Edouard@1953: if exit: Edouard@1953: sys.exit() # wx.Exit() edouard@1941: andrej@1792: return res andrej@1792: andrej@1792: andrej@1792: def get_last_traceback(tb): edouard@3546: while True: edouard@3546: if not hasattr(tb, "tb_next"): edouard@3546: break edouard@3546: if tb.tb_next: edouard@3546: tb = tb.tb_next edouard@3546: else: edouard@3546: break edouard@3546: andrej@1792: return tb andrej@1792: andrej@1792: andrej@1792: def format_namespace(d, indent=' '): kinsamanka@3750: return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.items()]) andrej@1792: andrej@1792: andrej@1792: ignored_exceptions = [] # a problem with a line in a module is only reported once per session andrej@1792: andrej@1792: edouard@3444: def AddExceptHook(app_version='[No version]', logf = None): andrej@1792: andrej@1792: def save_bug_report(e_type, e_value, e_traceback, bug_report_path, date): andrej@1792: info = { andrej@1792: 'app-title': wx.GetApp().GetAppName(), andrej@1792: 'app-version': app_version, andrej@1792: 'wx-version': wx.VERSION_STRING, andrej@1792: 'wx-platform': wx.Platform, andrej@1792: 'python-version': platform.python_version(), andrej@1792: 'platform': platform.platform(), andrej@1792: 'e-type': e_type, andrej@1792: 'e-value': e_value, andrej@1792: 'date': date, andrej@1792: 'cwd': os.getcwd(), andrej@1792: } andrej@1792: if e_traceback: andrej@1792: info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value) andrej@1792: last_tb = get_last_traceback(e_traceback) edouard@3546: # save locals at the level of the stack trace where the exception actually occurred edouard@3546: exception_locals = last_tb.tb_frame.f_locals if last_tb else {}; andrej@1792: info['locals'] = format_namespace(exception_locals) andrej@1792: if 'self' in exception_locals: andrej@1792: try: andrej@1792: info['self'] = format_namespace(exception_locals['self'].__dict__) andrej@1792: except Exception: andrej@1792: pass andrej@1792: path = os.path.dirname(bug_report_path) andrej@1792: if not os.path.exists(path): andrej@1792: os.mkdir(path) andrej@1792: output = open(bug_report_path, 'w') kinsamanka@3750: lst = list(info.keys()) andrej@1792: lst.sort() andrej@1792: for a in lst: edouard@3444: line = a + ":\n" + str(info[a]) + "\n\n" edouard@3444: output.write(line) edouard@3444: if logf is not None: edouard@3444: logf.write(line) andrej@1792: output.close() andrej@1792: Edouard@1953: def handle_exception(e_type, e_value, e_traceback, exit=False): andrej@1792: traceback.print_exception(e_type, e_value, e_traceback) # this is very helpful when there's an exception in the rest of this func andrej@1792: last_tb = get_last_traceback(e_traceback) edouard@3546: ex = (last_tb.tb_frame.f_code.co_filename if last_tb else "unknown", edouard@3546: last_tb.tb_frame.f_lineno if last_tb else None) andrej@1792: if ex not in ignored_exceptions: andrej@1792: ignored_exceptions.append(ex) andrej@1792: date = time.ctime() andrej@1792: path = tempfile.gettempdir()+os.sep+wx.GetApp().GetAppName() andrej@1792: bug_report_path = path + os.sep + "bug_report_" + time.strftime("%Y_%m_%d__%H-%M-%S") + ".txt" andrej@1792: save_bug_report(e_type, e_value, e_traceback, bug_report_path, date) edouard@1941: wx.CallAfter(Display_Exception_Dialog, e_type, e_value, e_traceback, bug_report_path, exit) andrej@1792: # sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args) andrej@1792: sys.excepthook = handle_exception andrej@1792: andrej@1792: init_old = threading.Thread.__init__ andrej@1792: andrej@1792: def init(self, *args, **kwargs): andrej@1792: init_old(self, *args, **kwargs) andrej@1792: run_old = self.run andrej@1792: andrej@1792: def run_with_except_hook(*args, **kw): andrej@1792: try: andrej@1792: run_old(*args, **kw) andrej@1792: except (KeyboardInterrupt, SystemExit): andrej@1792: raise andrej@1792: except Exception: andrej@1792: sys.excepthook(*sys.exc_info()) andrej@1792: self.run = run_with_except_hook andrej@1792: threading.Thread.__init__ = init edouard@1941: edouard@1941: return handle_exception