-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Debug.java
233 lines (207 loc) · 9.73 KB
/
Debug.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.denizenscript.denizencore.utilities.debugging;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
public class Debug {
/** Current debug recording text, if recording enabled, for submission to paste server. */
public static StringBuilder debugRecording = new StringBuilder();
/** Current main thread context, maintained automatically by stacked calls, for error handling. */
public static TagContext currentContext = null;
/** Start debug recording, for submission to paste server. */
public static void startRecording() {
if (!CoreConfiguration.debugRecordingAllowed) {
Debug.echoError("Cannot start debug recording: forbidden by config.");
return;
}
CoreConfiguration.shouldRecordDebug = true;
debugRecording = new StringBuilder();
}
/** Stop debug recording, to cancel a startRecording. */
public static void stopRecording() {
CoreConfiguration.shouldRecordDebug = false;
debugRecording = new StringBuilder();
}
/**
* Can be used with echoDebug(...) to output a header, footer,
* or a spacer.
* <p/>
* DebugElement.Header = +- string description ------+
* DebugElement.Spacer =
* DebugElement.Footer = +--------------+
* <p/>
* Also includes color.
*/
public enum DebugElement {
Header, Footer, Spacer
}
/** Echos an error message, using automatically gathered context. */
public static void echoError(String error) {
echoError(currentContext, error);
}
/** Echos an error message, using manually specified context. */
public static void echoError(TagContext context, String error) {
echoError(context, null, error);
}
/** Echos an error message, using manually specified context and optional extra text context. */
public static void echoError(TagContext context, String addedContext, String error) {
if (context == null) {
context = currentContext;
}
if (context != null && context.entry != null) {
echoError(context.entry, addedContext, error);
}
else if (context != null && context.script != null) {
echoError(context.script.getContainer(), addedContext, error);
}
else {
DenizenCore.runOnMainThread(() -> DebugInternals.echoErrorInternal(null, addedContext, error, true));
}
}
/** Echos an error message, using a specific script container as the context source. */
public static void echoError(ScriptContainer script, String error) {
echoError(script, null, error);
}
/** Echos an error message, using a specific script container as the context source with optional extra text content. */
public static void echoError(ScriptContainer script, String addedContext, String error) {
if (script != null) {
addedContext = " <LR>In script '<A>" + script.getName() + "<LR>'" + (addedContext == null ? "" : addedContext);
}
final String text = addedContext;
DenizenCore.runOnMainThread(() -> DebugInternals.echoErrorInternal(null, text, error, true));
}
/** Echos an error message, using a specific script entry as the context source. */
public static void echoError(ScriptEntry entry, String error) {
echoError(entry, null, error);
}
/** Echos an error message, using a specific script entry as the context source with optional extra text content. */
public static void echoError(ScriptEntry entry, String addedContext, String error) {
DenizenCore.runOnMainThread(() -> DebugInternals.echoErrorInternal(entry, addedContext, error, true));
}
/** Echos an exception error message, with automatic context filling. */
public static void echoError(Throwable ex) {
DenizenCore.runOnMainThread(() -> DebugInternals.echoExceptionInternal(currentContext == null ? null : currentContext.entry, ex));
}
/** Echos an exception error message, with a specific script entry as the context source. */
public static void echoError(ScriptEntry entry, Throwable error) {
DenizenCore.runOnMainThread(() -> DebugInternals.echoExceptionInternal(entry, error));
}
/** Logs output text only if Verbose is enabled. */
public static void verboseLog(String message) {
if (CoreConfiguration.debugVerbose) {
String caller = DebugInternals.getCaller();
DenizenCore.runOnMainThread(() -> DebugInternals.logInternal(caller, message));
}
}
/** Logs contextless output information with a generated caller classname label. */
public static void log(String message) {
String caller = DebugInternals.getCaller();
DenizenCore.runOnMainThread(() -> DebugInternals.logInternal(caller, message));
}
/** Logs contextless output information with a specified caller label. */
public static void log(String caller, String message) {
DenizenCore.runOnMainThread(() -> DebugInternals.logInternal(caller, message));
}
/** Logs contextless output information with a given formatting element. */
public static void log(DebugElement element, String message) {
if (!CoreConfiguration.shouldShowDebug) {
return;
}
switch (element) {
case Footer:
message = "<LP>+---------------------+";
break;
case Header:
message = "<LP>+- " + message + "<LP> ---------+";
break;
}
final String finalMessage = message;
DenizenCore.runOnMainThread(() -> DebugInternals.finalOutputDebugText(finalMessage, null, true));
}
/** Echos a contextless "Okay!" message. */
public static void echoApproval(String message) {
DenizenCore.runOnMainThread(() -> {
if (!CoreConfiguration.shouldShowDebug) {
return;
}
DebugInternals.finalOutputDebugText("<GR>OKAY! <W>" + message, null, true);
});
}
/** Echoes general debug on a given caller. Only shows if debug is enabled for the caller. */
public static void echoDebug(Debuggable entry, DebugElement element) {
if (!CoreConfiguration.shouldShowDebug || !shouldDebug(entry)) {
return;
}
echoDebug(entry, element, null);
}
/** Echoes general debug on a given caller. Only shows if debug is enabled for the caller. */
public static void echoDebug(Debuggable entry, String message) {
if (!CoreConfiguration.shouldShowDebug || !shouldDebug(entry)) {
return;
}
DenizenCore.runOnMainThread(() -> {
DebugInternals.echo("<W>"+ DebugInternals.trimMessage(message), entry);
if (CoreConfiguration.debugVerbose && entry != null) {
DebugInternals.echo("<LG>(Verbose) Caller = " + entry, entry);
}
});
}
/** Echoes general debug on a given caller. Only shows if debug is enabled for the caller. */
public static void echoDebug(Debuggable entry, DebugElement element, String message) {
if (!CoreConfiguration.shouldShowDebug || !shouldDebug(entry)) {
return;
}
switch (element) {
case Footer:
message = "<LP>+---------------------+";
break;
case Header:
message = "<LP>+- " + message + "<LP> ---------+";
break;
}
final String finalMessage = message;
DenizenCore.runOnMainThread(() -> DebugInternals.echo(finalMessage, entry));
}
/** Reports a command's output to debug, if debug is enabled for the entry. */
public static void report(Debuggable caller, String name, String message) {
if (!CoreConfiguration.shouldShowDebug || !shouldDebug(caller)) {
return;
}
DenizenCore.runOnMainThread(() -> DebugInternals.echo("<Y>+> <G>Executing '<Y>" + name + "<G>': " + DebugInternals.trimMessage(message), caller));
}
/** Reports a command's output to debug, if debug is enabled for the entry. */
public static void report(Debuggable caller, String name, Object... values) {
if (!CoreConfiguration.shouldShowDebug || !shouldDebug(caller)) {
return;
}
StringBuilder output = new StringBuilder();
for (Object obj : values) {
if (obj == null) {
continue;
}
if (obj instanceof ObjectTag) {
ObjectTag objTag = (ObjectTag) obj;
output.append("<G>").append(objTag.getPrefix()).append("='<Y>").append(objTag.debuggable()).append("<G>' ");
}
else {
output.append(obj);
}
}
DenizenCore.runOnMainThread(() -> DebugInternals.echo("<Y>+> <G>Executing '<Y>" + name + "<G>': " + DebugInternals.trimMessage(output.toString()), caller));
}
/** Returns truie if the debuggable object should output debug. */
public static boolean shouldDebug(Debuggable caller) {
if (CoreConfiguration.debugOverride) {
return true;
}
if (!CoreConfiguration.shouldShowDebug) {
return false;
}
if (caller != null) {
return caller.shouldDebug();
}
return true;
}
}