|
@@ -0,0 +1,124 @@
|
|
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;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.events.AppErrorEvent;
|
|
26
|
+import com.dmdirc.events.UserErrorEvent;
|
|
27
|
+import com.dmdirc.logger.ErrorLevel;
|
|
28
|
+
|
|
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
|
+import ch.qos.logback.classic.spi.ILoggingEvent;
|
|
36
|
+import ch.qos.logback.classic.spi.IThrowableProxy;
|
|
37
|
+import ch.qos.logback.classic.spi.StackTraceElementProxy;
|
|
38
|
+import ch.qos.logback.core.AppenderBase;
|
|
39
|
+
|
|
40
|
+/**
|
|
41
|
+ *
|
|
42
|
+ */
|
|
43
|
+public class ProgramErrorAppender extends AppenderBase<ILoggingEvent> {
|
|
44
|
+
|
|
45
|
+ private DMDircMBassador eventBus;
|
|
46
|
+ private Marker appError;
|
|
47
|
+ private Marker userError;
|
|
48
|
+
|
|
49
|
+ @Override
|
|
50
|
+ public void start() {
|
|
51
|
+ appError = MarkerFactory.getMarker("AppError");
|
|
52
|
+ userError = MarkerFactory.getMarker("UserError");
|
|
53
|
+ if (eventBus == null) {
|
|
54
|
+ addError("No eventBus set for the appender named ["+ name +"].");
|
|
55
|
+ return;
|
|
56
|
+ }
|
|
57
|
+ super.start();
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ @Override
|
|
61
|
+ protected void append(final ILoggingEvent eventObject) {
|
|
62
|
+ if (eventObject.getMarker() == null) {
|
|
63
|
+ return;
|
|
64
|
+ }
|
|
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(), ""));
|
|
72
|
+ }
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ public void setEventBus(final DMDircMBassador eventBus) {
|
|
76
|
+ this.eventBus = eventBus;
|
|
77
|
+ }
|
|
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
|
+}
|