f@0: /* f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool f@0: f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) f@0: f@0: This program is free software: you can redistribute it and/or modify f@0: it under the terms of the GNU General Public License as published by f@0: the Free Software Foundation, either version 3 of the License, or f@0: (at your option) any later version. f@0: f@0: This program is distributed in the hope that it will be useful, f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the f@0: GNU General Public License for more details. f@0: f@0: You should have received a copy of the GNU General Public License f@0: along with this program. If not, see . f@0: */ f@0: f@0: package uk.ac.qmul.eecs.ccmi.utils; f@0: f@0: import java.io.IOException; f@0: import java.io.PrintWriter; f@0: import java.io.StringWriter; f@0: import java.io.Writer; f@0: import java.lang.Thread.UncaughtExceptionHandler; f@0: import java.util.logging.FileHandler; f@0: import java.util.logging.Level; f@0: import java.util.logging.Logger; f@0: f@0: /** f@0: * The UncaughtExceptionHandler for the CCmI Editor. It logs the occurred exception stack trace f@0: * on a file (errorN.log, where N is an integer number automatically assigned to avoid f@0: * collision with other files of the same type) which is created in the same directory where f@0: * the program is run. The exception stack trace will be in the format defined by the {@code XMLFormatter} class f@0: * of the java.utli.logging package. f@0: * f@0: * @see java.util.logging.XMLFormatter f@0: */ f@0: public class CCmIUncaughtExceptionHandler implements UncaughtExceptionHandler { f@0: f@0: @Override f@0: public void uncaughtException(Thread thread, Throwable throwable) { f@0: try{ f@0: Logger logger = Logger.getLogger("uncaught_exception"); f@0: logger.setLevel(Level.SEVERE); f@0: FileHandler fileHandler = null; f@0: try { f@0: fileHandler = new FileHandler("error%u.log",true); f@0: } catch (IOException e) { f@0: System.err.println(throwable.toString()); f@0: System.err.println(); f@0: System.err.println("Could not use error log file"); f@0: e.printStackTrace(); f@0: return; f@0: } f@0: fileHandler.setLevel(Level.SEVERE); f@0: logger.addHandler(fileHandler); f@0: StringBuilder builder = new StringBuilder(throwable.toString()); f@0: builder.append('\n'); f@0: final Writer result = new StringWriter(); f@0: final PrintWriter printWriter = new PrintWriter(result); f@0: throwable.printStackTrace(printWriter); f@0: builder.append(result.toString()); f@0: logger.severe(builder.toString()); f@0: fileHandler.close(); f@0: throwable.printStackTrace(); f@0: }catch(Exception exception){ f@0: exception.printStackTrace(); f@0: System.exit(-1); f@0: } f@0: } f@0: }