Skip to content

Commit

Permalink
Add global exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tlecomte committed Oct 14, 2015
1 parent d14131c commit 53385b1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion friture/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# specifically import from PyQt5.QtGui and QWidgets for startup time improvement :
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QApplication, QSplashScreen
from PyQt5.QtGui import QPixmap
import friture.exceptionhandler
from friture.ui_friture import Ui_MainWindow
from friture.about import About_Dialog # About dialog
from friture.settings import Settings_Dialog # Setting dialog
Expand All @@ -43,7 +44,6 @@
# (and text painting is costly)
SLOW_TIMER_PERIOD_MS = 1000


class Friture(QMainWindow, ):

def __init__(self, logger):
Expand Down
69 changes: 69 additions & 0 deletions friture/exceptionhandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (C) 2009 Timothée Lecomte

# This file is part of Friture.
#
# Friture is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as published by
# the Free Software Foundation.
#
# Friture is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Friture. If not, see <http://www.gnu.org/licenses/>.

import sys
import os.path
import time
import io
import traceback
import friture
from PyQt5 import QtCore
from PyQt5 import QtWidgets

def excepthook(exception_type, exception_value, traceback_object):
# call the standard exception handler to have prints on the console
sys.__excepthook__(exception_type, exception_value, traceback_object)

separator = '-' * 80
log_dir = QtCore.QStandardPaths.standardLocations(QtCore.QStandardPaths.AppDataLocation)[0]
logFile = os.path.join(log_dir, "friture.log")

notice = \
"""An unhandled exception occurred. Please report the problem\n"""\
"""on GitHub or via email to <%s>.\n"""\
"""A log has been written to "%s".\n\nError information:\n""" % \
("[email protected]", logFile)

versionInfo="Friture " + friture.__versionXXXX__

timeString = time.strftime("%Y-%m-%d, %H:%M:%S")

tbinfofile = io.StringIO()
traceback.print_tb(traceback_object, None, tbinfofile)
tbinfofile.seek(0)
tbinfo = tbinfofile.read()
errmsg = '%s: \n%s' % (str(exception_type), str(exception_value))
sections = [separator, timeString, separator, errmsg, separator, tbinfo, separator, versionInfo]
msg = '\n'.join(sections)

try:
os.makedirs(log_dir, exist_ok=True)
with open(logFile, "w") as f:
f.write(msg)
except IOError as e:
print(e)
pass

errorbox = QtWidgets.QMessageBox()
errorbox.setWindowTitle("Friture critical error")
errorbox.setText(str(notice)+str(msg))
errorbox.setIcon(QtWidgets.QMessageBox.Critical)
errorbox.exec_()

sys.excepthook = excepthook

0 comments on commit 53385b1

Please sign in to comment.