Browse Source

Bit of tidying and add Fatal errors.

pull/578/head
Greg Holmes 9 years ago
parent
commit
8933a842bd
3 changed files with 165 additions and 67 deletions
  1. 1
    1
      build.gradle
  2. 22
    66
      src/com/dmdirc/ProgramErrorAppender.java
  3. 142
    0
      src/com/dmdirc/util/LogUtils.java

+ 1
- 1
build.gradle View File

@@ -36,7 +36,7 @@ dependencies {
36 36
     compile group: 'com.squareup.dagger', name: 'dagger-compiler', version: '1.2.1'
37 37
     compile group: 'com.google.auto.value', name: 'auto-value', version: '1.0'
38 38
 
39
-    bundle group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.2+'
39
+    bundle group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.2'
40 40
     bundle group: 'org.slf4j', name: 'slf4j-api', version:'1.7.10'
41 41
     bundle group: 'com.squareup.dagger', name: 'dagger', version: '1.2.1'
42 42
     bundle group: 'com.esotericsoftware.yamlbeans', name: 'yamlbeans', version: '1.08'

+ 22
- 66
src/com/dmdirc/ProgramErrorAppender.java View File

@@ -25,31 +25,27 @@ package com.dmdirc;
25 25
 import com.dmdirc.events.AppErrorEvent;
26 26
 import com.dmdirc.events.UserErrorEvent;
27 27
 import com.dmdirc.logger.ErrorLevel;
28
+import com.dmdirc.logger.ProgramError;
28 29
 
29
-import java.lang.reflect.Constructor;
30
-
31
-import org.slf4j.Marker;
32
-import org.slf4j.MarkerFactory;
33
-
34
-import ch.qos.logback.classic.Level;
35 30
 import ch.qos.logback.classic.spi.ILoggingEvent;
36
-import ch.qos.logback.classic.spi.IThrowableProxy;
37
-import ch.qos.logback.classic.spi.StackTraceElementProxy;
38 31
 import ch.qos.logback.core.AppenderBase;
39 32
 
33
+import static com.dmdirc.util.LogUtils.APP_ERROR;
34
+import static com.dmdirc.util.LogUtils.FATAL_APP_ERROR;
35
+import static com.dmdirc.util.LogUtils.FATAL_USER_ERROR;
36
+import static com.dmdirc.util.LogUtils.USER_ERROR;
37
+import static com.dmdirc.util.LogUtils.getErrorLevel;
38
+import static com.dmdirc.util.LogUtils.getThrowable;
39
+
40 40
 /**
41
- *
41
+ * Converts log states into {@link ProgramError}s so they can be displayed to the user.
42 42
  */
43 43
 public class ProgramErrorAppender extends AppenderBase<ILoggingEvent> {
44 44
 
45 45
     private DMDircMBassador eventBus;
46
-    private Marker appError;
47
-    private Marker userError;
48 46
 
49 47
     @Override
50 48
     public void start() {
51
-        appError = MarkerFactory.getMarker("AppError");
52
-        userError = MarkerFactory.getMarker("UserError");
53 49
         if (eventBus == null) {
54 50
             addError("No eventBus set for the appender named ["+ name +"].");
55 51
             return;
@@ -62,63 +58,23 @@ public class ProgramErrorAppender extends AppenderBase<ILoggingEvent> {
62 58
         if (eventObject.getMarker() == null) {
63 59
             return;
64 60
         }
65
-        if (eventObject.getMarker() == appError) {
66
-            eventBus.publish(new AppErrorEvent(level(eventObject), throwable
67
-                    (eventObject), eventObject.getFormattedMessage(), ""));
68
-        }
69
-        if (eventObject.getMarker() == userError) {
70
-            eventBus.publish(new UserErrorEvent(level(eventObject), throwable
71
-                    (eventObject), eventObject.getFormattedMessage(), ""));
61
+        if (eventObject.getMarker() == APP_ERROR) {
62
+            eventBus.publish(new AppErrorEvent(
63
+                    getErrorLevel(eventObject.getLevel()),
64
+                    getThrowable(eventObject), eventObject.getFormattedMessage(), ""));
65
+        } else if (eventObject.getMarker() == USER_ERROR) {
66
+            eventBus.publish(new UserErrorEvent(getErrorLevel(eventObject.getLevel()),
67
+                    getThrowable(eventObject), eventObject.getFormattedMessage(), ""));
68
+        } else if (eventObject.getMarker() == FATAL_APP_ERROR) {
69
+            eventBus.publish(new AppErrorEvent(ErrorLevel.FATAL, getThrowable(eventObject),
70
+                    eventObject.getFormattedMessage(), ""));
71
+        } else if (eventObject.getMarker() == FATAL_USER_ERROR) {
72
+            eventBus.publish(new UserErrorEvent(ErrorLevel.FATAL, getThrowable(eventObject),
73
+                    eventObject.getFormattedMessage(), ""));
72 74
         }
73 75
     }
74 76
 
75 77
     public void setEventBus(final DMDircMBassador eventBus) {
76 78
         this.eventBus = eventBus;
77 79
     }
78
-
79
-    private ErrorLevel level(final ILoggingEvent eventObject) {
80
-        final Level level = eventObject.getLevel();
81
-        if (level.equals(Level.ERROR)) {
82
-            return ErrorLevel.HIGH;
83
-        } else if (level.equals(Level.WARN)) {
84
-            return ErrorLevel.MEDIUM;
85
-        } else if (level.equals(Level.INFO)) {
86
-            return ErrorLevel.LOW;
87
-        } else {
88
-            return ErrorLevel.UNKNOWN;
89
-        }
90
-    }
91
-
92
-    private Throwable throwable(final ILoggingEvent eventObject) {
93
-        try {
94
-            return convert(eventObject.getThrowableProxy());
95
-        } catch (ReflectiveOperationException ex) {
96
-            return new IllegalStateException("Error converting exception");
97
-        }
98
-    }
99
-
100
-    @SuppressWarnings("unchecked")
101
-    private Throwable convert(final IThrowableProxy proxy)
102
-            throws ReflectiveOperationException {
103
-        final Class<Throwable> clazz = (Class<Throwable>) Class.forName(proxy.getClassName());
104
-        final Throwable throwable;
105
-        if (proxy.getCause() == null) {
106
-            final Constructor<Throwable> ctor = clazz.getDeclaredConstructor(String.class);
107
-            throwable = ctor.newInstance(proxy.getMessage());
108
-        } else {
109
-            final Constructor<Throwable> ctor = clazz.getDeclaredConstructor(String.class,
110
-                    Throwable.class);
111
-            throwable = ctor.newInstance(proxy.getMessage(), convert(proxy.getCause()));
112
-        }
113
-        throwable.setStackTrace(convert(proxy.getStackTraceElementProxyArray()));
114
-        return throwable;
115
-    }
116
-
117
-    private StackTraceElement[] convert(final StackTraceElementProxy... elements) {
118
-        final StackTraceElement[] returnValue = new StackTraceElement[elements.length];
119
-        for (int i = 0; i < elements.length; i++) {
120
-            returnValue[i] = elements[i].getStackTraceElement();
121
-        }
122
-        return returnValue;
123
-    }
124 80
 }

+ 142
- 0
src/com/dmdirc/util/LogUtils.java View File

@@ -0,0 +1,142 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util;
24
+
25
+import com.dmdirc.logger.ErrorLevel;
26
+
27
+import java.lang.reflect.Constructor;
28
+
29
+import javax.annotation.Nonnull;
30
+import javax.annotation.Nullable;
31
+
32
+import org.slf4j.Marker;
33
+import org.slf4j.MarkerFactory;
34
+
35
+import ch.qos.logback.classic.Level;
36
+import ch.qos.logback.classic.spi.ILoggingEvent;
37
+import ch.qos.logback.classic.spi.IThrowableProxy;
38
+import ch.qos.logback.classic.spi.StackTraceElementProxy;
39
+
40
+/**
41
+ * Utility methods for logging in the client.
42
+ */
43
+public final class LogUtils {
44
+
45
+    public static final Marker USER_ERROR = MarkerFactory.getMarker("UserError");
46
+    public static final Marker APP_ERROR = MarkerFactory.getMarker("AppError");
47
+    public static final Marker FATAL_USER_ERROR = MarkerFactory.getMarker("FatalUserError");
48
+    public static final Marker FATAL_APP_ERROR = MarkerFactory.getMarker("FatalAppError");
49
+
50
+    private LogUtils() {
51
+    }
52
+
53
+    /**
54
+     * Converts a SLF4J {@link Level} into a DMDirc {@link ErrorLevel}
55
+     *
56
+     * @param level Error to convert
57
+     *
58
+     * @return Converted error
59
+     */
60
+    @Nonnull
61
+    public static ErrorLevel getErrorLevel(@Nonnull final Level level) {
62
+        if (level.equals(Level.ERROR)) {
63
+            return ErrorLevel.HIGH;
64
+        } else if (level.equals(Level.WARN)) {
65
+            return ErrorLevel.MEDIUM;
66
+        } else if (level.equals(Level.INFO)) {
67
+            return ErrorLevel.LOW;
68
+        } else {
69
+            return ErrorLevel.UNKNOWN;
70
+        }
71
+    }
72
+
73
+    /**
74
+     * Converts the {@link IThrowableProxy} from an SLF4J {@link ILoggingEvent} into a
75
+     * {@link Throwable}.
76
+     *
77
+     * @param eventObject Event containing the {@link IThrowableProxy}
78
+     *
79
+     * @return {@link Throwable} representation of the {@link IThrowableProxy} or {@code null} if
80
+     *         there is no {@link IThrowableProxy} present.
81
+     */
82
+    @Nullable
83
+    public static Throwable getThrowable(final ILoggingEvent eventObject) {
84
+        if (eventObject.getThrowableProxy() == null) {
85
+            return null;
86
+        }
87
+        try {
88
+            return getThrowable(eventObject.getThrowableProxy());
89
+        } catch (ReflectiveOperationException ex) {
90
+            return new IllegalStateException("Error converting exception");
91
+        }
92
+    }
93
+
94
+    /**
95
+     * Converts a SLF4J {@link IThrowableProxy} into a {@link Throwable}.
96
+     *
97
+     * @param proxy {@link IThrowableProxy} to convert
98
+     *
99
+     * @return {@link Throwable} representation of the {@link IThrowableProxy}
100
+     *
101
+     * @throws ReflectiveOperationException If an error ocurred creating the real {@link Throwable}
102
+     */
103
+    @Nonnull
104
+    public static Throwable getThrowable(@Nonnull final IThrowableProxy proxy)
105
+            throws ReflectiveOperationException {
106
+        final Class<Throwable> clazz = getThrowableClass(proxy.getClassName());
107
+        final Throwable throwable;
108
+        if (proxy.getCause() == null) {
109
+            final Constructor<Throwable> ctor = clazz.getDeclaredConstructor(String.class);
110
+            throwable = ctor.newInstance(proxy.getMessage());
111
+        } else {
112
+            final Constructor<Throwable> ctor = clazz.getDeclaredConstructor(String.class,
113
+                    Throwable.class);
114
+            throwable = ctor.newInstance(proxy.getMessage(), getThrowable(proxy.getCause()));
115
+        }
116
+        throwable.setStackTrace(getStackTraceElements(proxy.getStackTraceElementProxyArray()));
117
+        return throwable;
118
+    }
119
+
120
+    /**
121
+     * Converts an SLF4F {@link StackTraceElementProxy}[] into a {@link StackTraceElement}[].
122
+     *
123
+     * @param elements {@link StackTraceElementProxy}s to convert
124
+     *
125
+     * @return Non null {@link StackTraceElement}[] of the input
126
+     */
127
+    @Nonnull
128
+    public static StackTraceElement[] getStackTraceElements(
129
+            final StackTraceElementProxy... elements) {
130
+        final StackTraceElement[] returnValue = new StackTraceElement[elements.length];
131
+        for (int i = 0; i < elements.length; i++) {
132
+            returnValue[i] = elements[i].getStackTraceElement();
133
+        }
134
+        return returnValue;
135
+    }
136
+
137
+    @SuppressWarnings("unchecked")
138
+    private static Class<Throwable> getThrowableClass(final String string)
139
+            throws ClassNotFoundException {
140
+        return (Class<Throwable>) Class.forName(string);
141
+    }
142
+}

Loading…
Cancel
Save