123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- /*
- * Copyright (c) 2006-2013 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.logger;
-
- import com.dmdirc.config.ConfigManager;
- import com.dmdirc.config.IdentityManager;
- import com.dmdirc.interfaces.ConfigChangeListener;
- import com.dmdirc.interfaces.IdentityController;
- import com.dmdirc.ui.FatalErrorDialog;
- import com.dmdirc.util.collections.ListenerList;
-
- import java.awt.GraphicsEnvironment;
- import java.util.Date;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.atomic.AtomicLong;
-
- /**
- * Error manager.
- */
- public class ErrorManager implements ConfigChangeListener {
-
- /** Previously instantiated instance of ErrorManager. */
- private static ErrorManager me;
-
- /** A list of exceptions which we don't consider bugs and thus don't report. */
- private static final Class<?>[] BANNED_EXCEPTIONS = new Class<?>[]{
- NoSuchMethodError.class, NoClassDefFoundError.class,
- UnsatisfiedLinkError.class, AbstractMethodError.class,
- IllegalAccessError.class, OutOfMemoryError.class,
- NoSuchFieldError.class,
- };
-
- /** Whether or not to send error reports. */
- private boolean sendReports;
-
- /** Whether or not to log error reports. */
- private boolean logReports;
-
- /** Queue of errors to be reported. */
- private final BlockingQueue<ProgramError> reportQueue = new LinkedBlockingQueue<>();
-
- /** Thread used for sending errors. */
- private volatile Thread reportThread;
-
- /** Error list. */
- private final List<ProgramError> errors;
-
- /** Listener list. */
- private final ListenerList errorListeners = new ListenerList();
-
- /** Next error ID. */
- private final AtomicLong nextErrorID;
-
- /** Creates a new instance of ErrorListDialog. */
- public ErrorManager() {
- errors = new LinkedList<>();
- nextErrorID = new AtomicLong();
- }
-
- /**
- * Initialiases the error manager.
- *
- * @param controller The controller to use to read/write settings.
- */
- public void initialise(final IdentityController controller) {
- final ConfigManager config = controller.getGlobalConfiguration();
-
- config.addChangeListener("general", "logerrors", this);
- config.addChangeListener("general", "submitErrors", this);
- config.addChangeListener("temp", "noerrorreporting", this);
-
- updateSettings();
- }
-
- /**
- * Returns the instance of ErrorManager.
- *
- * @return Instance of ErrorManager
- */
- public static synchronized ErrorManager getErrorManager() {
- if (me == null) {
- me = new ErrorManager();
- me.initialise(IdentityManager.getIdentityManager());
- }
- return me;
- }
-
- /**
- * Sets the singleton instance of the error manager.
- *
- * @param errorManager The error manager to use.
- */
- public static void setErrorManager(final ErrorManager errorManager) {
- me = errorManager;
- }
-
- /**
- * Adds a new error to the manager with the specified details. It is
- * assumed that errors without exceptions or details are not application
- * errors.
- *
- * @param level The severity of the error
- * @param message The error message
- * @since 0.6.3m1
- */
- protected void addError(final ErrorLevel level, final String message) {
- addError(level, message, new String[0], false);
- }
-
- /**
- * Adds a new error to the manager with the specified details.
- *
- * @param level The severity of the error
- * @param message The error message
- * @param exception The exception that caused this error
- * @param appError Whether or not this is an application error
- * @since 0.6.3m1
- */
- protected void addError(final ErrorLevel level, final String message,
- final Throwable exception, final boolean appError) {
- addError(level, message, getTrace(exception), appError, isValidError(exception));
- }
-
- /**
- * Adds a new error to the manager with the specified details.
- *
- * @param level The severity of the error
- * @param message The error message
- * @param details The details of the exception
- * @param appError Whether or not this is an application error
- * @since 0.6.3m1
- */
- protected void addError(final ErrorLevel level, final String message,
- final String[] details, final boolean appError) {
- addError(level, message, details, appError, true);
- }
-
- /**
- * Adds a new error to the manager with the specified details.
- *
- * @param level The severity of the error
- * @param message The error message
- * @param details The details of the exception
- * @param appError Whether or not this is an application error
- * @param canReport Whether or not this error can be reported
- * @since 0.6.3m1
- */
- protected void addError(final ErrorLevel level, final String message,
- final String[] details, final boolean appError, final boolean canReport) {
- final ProgramError error = getError(level, message, details, appError);
-
- final boolean dupe = addError(error);
- if (level.equals(ErrorLevel.FATAL)) {
- if (dupe) {
- error.setReportStatus(ErrorReportStatus.NOT_APPLICABLE);
- error.setFixedStatus(ErrorFixedStatus.DUPLICATE);
- }
- } else if (!canReport || (appError && !error.isValidSource())) {
- error.setReportStatus(ErrorReportStatus.NOT_APPLICABLE);
- error.setFixedStatus(ErrorFixedStatus.INVALID);
- } else if (!appError) {
- error.setReportStatus(ErrorReportStatus.NOT_APPLICABLE);
- error.setFixedStatus(ErrorFixedStatus.UNREPORTED);
- } else if (dupe) {
- error.setReportStatus(ErrorReportStatus.NOT_APPLICABLE);
- error.setFixedStatus(ErrorFixedStatus.DUPLICATE);
- } else if (sendReports) {
- sendError(error);
- }
-
- if (logReports) {
- error.save();
- }
-
- if (!dupe) {
- if (level == ErrorLevel.FATAL) {
- fireFatalError(error);
- } else {
- fireErrorAdded(error);
- }
- }
- }
-
- /**
- * Adds the specified error to the list of known errors and determines if
- * it was previously added.
- *
- * @param error The error to be added
- * @return True if a duplicate error has already been registered, false
- * otherwise
- */
- protected boolean addError(final ProgramError error) {
- int index;
-
- synchronized (errors) {
- index = errors.indexOf(error);
- if (index == -1) {
- errors.add(error);
- } else {
- errors.get(index).updateLastDate();
- }
- }
-
- return index > -1;
- }
-
- /**
- * Retrieves a {@link ProgramError} that represents the specified details.
- *
- * @param level The severity of the error
- * @param message The error message
- * @param details The details of the exception
- * @param appError Whether or not this is an application error
- * @since 0.6.3m1
- * @return A corresponding ProgramError
- */
- protected ProgramError getError(final ErrorLevel level, final String message,
- final String[] details, final boolean appError) {
- return new ProgramError(nextErrorID.getAndIncrement(), level, message,
- details, new Date());
- }
-
- /**
- * Determines whether or not the specified exception is one that we are
- * willing to report.
- *
- * @param exception The exception to test
- * @since 0.6.3m1
- * @return True if the exception may be reported, false otherwise
- */
- protected boolean isValidError(final Throwable exception) {
- Throwable target = exception;
-
- while (target != null) {
- for (Class<?> bad : BANNED_EXCEPTIONS) {
- if (bad.equals(target.getClass())) {
- return false;
- }
- }
-
- target = target.getCause();
- }
-
- return true;
- }
-
- /**
- * Converts an exception into a string array.
- *
- * @param throwable Exception to convert
- * @since 0.6.3m1
- * @return Exception string array
- */
- public static String[] getTrace(final Throwable throwable) {
- String[] trace;
-
- if (throwable == null) {
- trace = new String[0];
- } else {
- final StackTraceElement[] traceElements = throwable.getStackTrace();
- trace = new String[traceElements.length + 1];
-
- trace[0] = throwable.toString();
-
- for (int i = 0; i < traceElements.length; i++) {
- trace[i + 1] = traceElements[i].toString();
- }
-
- if (throwable.getCause() != null) {
- final String[] causeTrace = getTrace(throwable.getCause());
- final String[] newTrace = new String[trace.length + causeTrace.length];
- trace[0] = "\nWhich caused: " + trace[0];
-
- System.arraycopy(causeTrace, 0, newTrace, 0, causeTrace.length);
- System.arraycopy(trace, 0, newTrace, causeTrace.length, trace.length);
-
- trace = newTrace;
- }
- }
-
- return trace;
- }
-
- /**
- * Sends an error to the developers.
- *
- * @param error error to be sent
- */
- public void sendError(final ProgramError error) {
- if (error.getReportStatus() != ErrorReportStatus.ERROR
- && error.getReportStatus() != ErrorReportStatus.WAITING) {
- return;
- }
-
- error.setReportStatus(ErrorReportStatus.QUEUED);
-
- reportQueue.add(error);
-
- if (reportThread == null || !reportThread.isAlive()) {
- reportThread = new ErrorReportingThread(reportQueue);
- reportThread.start();
- }
- }
-
- /**
- * Called when an error needs to be deleted from the list.
- *
- * @param error ProgramError that changed
- */
- public void deleteError(final ProgramError error) {
- synchronized (errors) {
- errors.remove(error);
- }
-
- fireErrorDeleted(error);
- }
-
- /**
- * Deletes all errors from the manager.
- *
- * @since 0.6.3m1
- */
- public void deleteAll() {
- synchronized (errors) {
- for (ProgramError error : errors) {
- fireErrorDeleted(error);
- }
-
- errors.clear();
- }
- }
-
- /**
- * Returns the number of errors.
- *
- * @return Number of ProgramErrors
- */
- public int getErrorCount() {
- return errors.size();
- }
-
- /**
- * Returns the list of program errors.
- *
- * @return Program error list
- */
- public List<ProgramError> getErrors() {
- synchronized (errors) {
- return new LinkedList<>(errors);
- }
- }
-
- /**
- * Adds an ErrorListener to the listener list.
- *
- * @param listener Listener to add
- */
- public void addErrorListener(final ErrorListener listener) {
- if (listener == null) {
- return;
- }
-
- errorListeners.add(ErrorListener.class, listener);
- }
-
- /**
- * Removes an ErrorListener from the listener list.
- *
- * @param listener Listener to remove
- */
- public void removeErrorListener(final ErrorListener listener) {
- errorListeners.remove(ErrorListener.class, listener);
- }
-
- /**
- * Fired when the program encounters an error.
- *
- * @param error Error that occurred
- */
- protected void fireErrorAdded(final ProgramError error) {
- int firedListeners = 0;
-
- for (ErrorListener listener : errorListeners.get(ErrorListener.class)) {
- if (listener.isReady()) {
- listener.errorAdded(error);
- firedListeners++;
- }
- }
-
- if (firedListeners == 0) {
- System.err.println("An error has occurred: " + error.getLevel()
- + ": " + error.getMessage());
-
- for (String line : error.getTrace()) {
- System.err.println("\t" + line);
- }
- }
- }
-
- /**
- * Fired when the program encounters a fatal error.
- *
- * @param error Error that occurred
- */
- protected void fireFatalError(final ProgramError error) {
- final boolean restart;
- if (GraphicsEnvironment.isHeadless()) {
- System.err.println("A fatal error has occurred: " + error.getMessage());
- for (String line : error.getTrace()) {
- System.err.println("\t" + line);
- }
- restart = false;
- } else {
- final FatalErrorDialog fed = new FatalErrorDialog(error);
- fed.setVisible(true);
- try {
- synchronized (fed) {
- while (fed.isWaiting()) {
- fed.wait();
- }
- }
- } catch (InterruptedException ex) {
- //Oh well, carry on
- }
- restart = fed.getRestart();
- }
-
- try {
- synchronized (error) {
- while (!error.getReportStatus().isTerminal()) {
- error.wait();
- }
- }
- } catch (InterruptedException ex) {
- // Do nothing
- }
-
- if (restart) {
- System.exit(42);
- } else {
- System.exit(1);
- }
- }
-
- /**
- * Fired when an error is deleted.
- *
- * @param error Error that has been deleted
- */
- protected void fireErrorDeleted(final ProgramError error) {
- for (ErrorListener listener : errorListeners.get(ErrorListener.class)) {
- listener.errorDeleted(error);
- }
- }
-
- /**
- * Fired when an error's status is changed.
- *
- * @param error Error that has been altered
- */
- protected void fireErrorStatusChanged(final ProgramError error) {
- for (ErrorListener listener : errorListeners.get(ErrorListener.class)) {
- listener.errorStatusChanged(error);
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void configChanged(final String domain, final String key) {
- updateSettings();
- }
-
- /** Updates the settings used by this error manager. */
- protected void updateSettings() {
- final ConfigManager config = IdentityManager.getIdentityManager().getGlobalConfiguration();
-
- try {
- sendReports = config.getOptionBool("general", "submitErrors")
- && !config.getOptionBool("temp", "noerrorreporting");
- logReports = config.getOptionBool("general", "logerrors");
- } catch (IllegalArgumentException ex) {
- sendReports = false;
- logReports = true;
- }
- }
-
- }
|