Merge from trunk.
[bertos.git] / wizard / exception_handler.py
1 #!/usr/bin/env python\r
2 # encoding: utf-8\r
3 #\r
4 # This file is part of BeRTOS.\r
5 #\r
6 # Bertos is free software; you can redistribute it and/or modify\r
7 # it under the terms of the GNU General Public License as published by\r
8 # the Free Software Foundation; either version 2 of the License, or\r
9 # (at your option) any later version.\r
10 #\r
11 # This program is distributed in the hope that it will be useful,\r
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 # GNU General Public License for more details.\r
15 #\r
16 # You should have received a copy of the GNU General Public License\r
17 # along with this program; if not, write to the Free Software\r
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
19 #\r
20 # As a special exception, you may use this file as part of a free software\r
21 # library without restriction.  Specifically, if other files instantiate\r
22 # templates or use macros or inline functions from this file, or you compile\r
23 # this file and link it with other files to produce an executable, this\r
24 # file does not by itself cause the resulting executable to be covered by\r
25 # the GNU General Public License.  This exception does not however\r
26 # invalidate any other reasons why the executable file might be covered by\r
27 # the GNU General Public License.\r
28 #\r
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)\r
30 #\r
31 # $Id: const.py 2907 2009-09-08 14:02:04Z duplo $\r
32 #\r
33 # Author: Lorenzo Berni <duplo@develer.com>\r
34 #\r
35 \r
36 import sys\r
37 import os\r
38 import traceback\r
39 \r
40 from PyQt4.QtCore import *\r
41 from PyQt4.QtGui import *\r
42 \r
43 def _excepthook(exc_type, exc_value, exc_traceback):\r
44     project_dir = QApplication.instance().project.info("PROJECT_PATH")\r
45     if not project_dir:\r
46         project_dir = os.getcwd()\r
47     file_name = os.path.join(project_dir, "wizard_error.log")\r
48     if os.path.exists(file_name):\r
49         content = open(file_name, "r").read()\r
50     else:\r
51         content = ""\r
52         if not os.path.exists(os.path.dirname(file_name)):\r
53             os.makedirs(os.path.dirname(file_name))\r
54     f = open(file_name, "w")\r
55     message = "\n".join(traceback.format_exception(exc_type, exc_value, exc_traceback))\r
56     f.write(message)\r
57     f.write(">"*80 + "\n")\r
58     f.write(content)\r
59     f.close()\r
60     print>>sys.stderr, message\r
61     QMessageBox.critical(\r
62         None,\r
63         "Exception occurred",\r
64         "An exception is occurred. Please attach the '%s' file to the support request." %os.path.abspath(file_name),\r
65     )\r
66     QApplication.instance().quit()\r
67 \r
68 sys.excepthook = _excepthook\r
69 \r