|
@@ -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
|
+}
|