comparison java/src/uk/ac/qmul/eecs/ccmi/utils/CCmIUncaughtExceptionHandler.java @ 0:9418ab7b7f3f

Initial import
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Fri, 16 Dec 2011 17:35:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9418ab7b7f3f
1 /*
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
3
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package uk.ac.qmul.eecs.ccmi.utils;
21
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.io.Writer;
26 import java.lang.Thread.UncaughtExceptionHandler;
27 import java.util.logging.FileHandler;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 /**
32 * The UncaughtExceptionHandler for the CCmI Editor. It logs the occurred exception stack trace
33 * on a file (errorN.log, where N is an integer number automatically assigned to avoid
34 * collision with other files of the same type) which is created in the same directory where
35 * the program is run. The exception stack trace will be in the format defined by the {@code XMLFormatter} class
36 * of the java.utli.logging package.
37 *
38 * @see java.util.logging.XMLFormatter
39 */
40 public class CCmIUncaughtExceptionHandler implements UncaughtExceptionHandler {
41
42 @Override
43 public void uncaughtException(Thread thread, Throwable throwable) {
44 try{
45 Logger logger = Logger.getLogger("uncaught_exception");
46 logger.setLevel(Level.SEVERE);
47 FileHandler fileHandler = null;
48 try {
49 fileHandler = new FileHandler("error%u.log",true);
50 } catch (IOException e) {
51 System.err.println(throwable.toString());
52 System.err.println();
53 System.err.println("Could not use error log file");
54 e.printStackTrace();
55 return;
56 }
57 fileHandler.setLevel(Level.SEVERE);
58 logger.addHandler(fileHandler);
59 StringBuilder builder = new StringBuilder(throwable.toString());
60 builder.append('\n');
61 final Writer result = new StringWriter();
62 final PrintWriter printWriter = new PrintWriter(result);
63 throwable.printStackTrace(printWriter);
64 builder.append(result.toString());
65 logger.severe(builder.toString());
66 fileHandler.close();
67 throwable.printStackTrace();
68 }catch(Exception exception){
69 exception.printStackTrace();
70 System.exit(-1);
71 }
72 }
73 }