/
dB.java
294 lines (238 loc) · 10.8 KB
/
dB.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package net.aufdemrand.denizen.utilities.debugging;
import net.aufdemrand.denizen.Settings;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Preferred method of outputting debugger information with Denizen and
* denizen-related plugins.
*
* Attempts to unify the style of reporting information to the Console and
* player with the use of color, headers, footers, and formatting.
*
*
* Example, this code:
*
* dB.echoDebug(DebugElement.Header, "Sample debug information");
* dB.echoDebug("This is an example of a piece of debug information. Parts and pieces " +
* "of an entire debug sequence may be in completely different classes, so making " +
* "a unified way to output to the console can make a world of difference with " +
* "debugging and usability.");
* dB.echoDebug(DebugElement.Spacer);
* dB.echoDebug("Here are some examples of a few different ways to log with the logger.");
* dB.echoApproval("Notable events can nicely show success or approval.");
* dB.echoError("Your users will be able to easily distinguish problems.");
* dB.info("...and important pieces of information can be easily spotted.");
* dB.echoDebug(DebugElement.Footer);
*
*
* will produce this output (with color):
*
* 16:05:05 [INFO] +- Sample debug information ------+
* 16:05:05 [INFO] This is an example of a piece of debug information. Parts
* and pieces of an entire debug sequence may be in completely
* different classes, so making a unified way to output to the
* console can make a world of difference with debugging and
* usability.
* 16:05:05 [INFO]
* 16:05:05 [INFO] Here are some examples of a few different ways to log with the
* logger.
* 16:05:05 [INFO] OKAY! Notable events can nicely show success or approval.
* 16:05:05 [INFO] ERROR! Your users will be able to easily distinguish problems.
* 16:05:05 [INFO] +> ...and important pieces of information can easily be spotted.
* 16:05:05 [INFO] +---------------------+
*
*
* @author Jeremy Schroeder
*
*/
public class dB {
public static boolean showDebug = Settings.ShowDebug();
public static boolean showStackTraces = true;
public static boolean showScriptBuilder = false;
public static boolean showColor = true;
public static boolean showEventsTrimming = false;
public static List<String> filter = new ArrayList<String>();
public static boolean shouldTrim = true;
public static int trimSize = 512;
public static boolean record = false;
public static StringBuilder Recording = new StringBuilder();
public static void toggle() { showDebug = !showDebug; }
/**
* Can be used with echoDebug(...) to output a header, footer,
* or a spacer.
*
* DebugElement.Header = +- string description ------+
* DebugElement.Spacer =
* DebugElement.Footer = +--------------+
*
* Also includes color.
*/
public static enum DebugElement {
Header, Footer, Spacer
}
////////////
// Public debugging methods, toggleable by checking extra criteria as implemented
// by the Debuggable interface, which usually checks a ScriptContainer's 'debug' node
//////
/**
* Used by Commands to report how the supplied arguments were parsed.
* Should be supplied a concatenated String with aH.debugObject() or dObject.debug() of all
* applicable objects used by the Command.
*
* @param caller
* @param name
* @param report
*/
public static void report(Debuggable caller, String name, String report) {
if (!showDebug) return;
echo("<Y>+> <G>Executing '<Y>" + name + "<G>': "
+ trimMessage(report), caller);
}
// Used by the various parts of Denizen that output debuggable information
// to help scripters see what is going on. Debugging an element is usually
// for formatting debug information.
public static void echoDebug(Debuggable caller, DebugElement element) {
if (!showDebug) return;
echoDebug(caller, element, null);
}
// Used by the various parts of Denizen that output debuggable information
// to help scripters see what is going on. Debugging an element is usually
// for formatting debug information.
public static void echoDebug(Debuggable caller, DebugElement element, String string) {
if (!showDebug) return;
StringBuilder sb = new StringBuilder(24);
switch(element) {
case Footer:
sb.append(ChatColor.LIGHT_PURPLE).append("+---------------------+");
break;
case Header:
sb.append(ChatColor.LIGHT_PURPLE).append("+- ").append(string).append(" ---------+");
break;
}
echo(sb.toString(), caller);
}
// Used by the various parts of Denizen that output debuggable information
// to help scripters see what is going on.
public static void echoDebug(Debuggable caller, String message) {
if (!showDebug) return;
echo(ChatColor.LIGHT_PURPLE + " " + ChatColor.WHITE + trimMessage(message), caller);
}
// These methods are deprecated. Please instead supply a valid Debuggable reference,
// which at this time is either a ScriptQueue, a ScriptEntry, or ScriptContainer.
// If none of these are available, using dB.log(...)
@Deprecated
public static void echoDebug(String message) {
echo(message, null);
}
@Deprecated
public static void echoDebug(DebugElement de, String message) {
echoDebug(null, de, message);
}
/////////////
// Other public debugging methods (Always show when debugger is enabled)
///////
public static void echoApproval(String message) {
if (!showDebug) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.GREEN + "OKAY! "
+ ChatColor.WHITE + message);
}
public static void echoError(String message) {
if (!showDebug) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.RED + "ERROR! "
+ ChatColor.WHITE + trimMessage(message));
}
public static void log(String message) {
if (!showDebug) return;
ConsoleSender.sendMessage(ChatColor.YELLOW + "+> ["
+ (sun.reflect.Reflection.getCallerClass(2).getSimpleName().length() > 16 ?
sun.reflect.Reflection.getCallerClass(2).getSimpleName().substring(0, 12) + "..."
: sun.reflect.Reflection.getCallerClass(2).getSimpleName()) + "] "
+ ChatColor.WHITE + trimMessage(message));
}
///////////////
// Private Helper Methods
/////////
// Some debug methods trim to keep super-long messages from hitting the console.
private static String trimMessage(String message) {
if (!shouldTrim) return message;
if (message.length() > trimSize)
message = message.substring(0, trimSize - 1) + "... * snip! *";
return message;
}
// Handles checking whether the provided debuggable should submit to the debugger
private static void echo(String string, Debuggable caller) {
boolean should_send = true;
// Attempt to see if the debug should even be sent by checking the
// script container's 'debug' node.
if (caller != null)
try {
if (filter.isEmpty())
should_send = caller.shouldDebug();
else {
should_send = false;
for (String criteria : filter)
if (caller.shouldFilter(criteria)) {
should_send = true;
break;
}
}
} catch (Exception e) {
// Had a problem determining whether it should debug, assume true.
should_send = true;
}
if (should_send) ConsoleSender.sendMessage(string);
}
/**
* ConsoleSender sends dScript debugging information to the logger
* will attempt to intelligently wrap any debug information that is more
* than one line. This is used by the dB static methods which do some
* additional formatting.
*/
private static class ConsoleSender {
// Bukkit CommandSender sends color nicely to the logger, so we'll use that.
static CommandSender commandSender = null;
static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
static boolean skipFooter = false;
// Use this method for sending a message
public static void sendMessage(String string) {
if (commandSender == null) commandSender = Bukkit.getServer().getConsoleSender();
// These colors are used a lot in the debugging of commands/etc, so having a few shortcuts is nicer
// than having a bunch of ChatColor.XXXX
string = string
.replace("<Y>", ChatColor.YELLOW + "")
.replace("<G>", ChatColor.DARK_GRAY + "")
.replace("<A>", ChatColor.AQUA + "");
// 'Hack-fix' for disallowing multiple 'footers' to print in a row
if (string.equals(ChatColor.LIGHT_PURPLE + "+---------------------+")) {
if (!skipFooter) skipFooter = true;
else { return; }
} else skipFooter = false;
// Create buffer for wrapping debug text nicely. This is mostly needed for Windows logging.
String[] words = string.split(" ");
String buffer = "";
int length = 0;
for (String word : words) { // # of total chars * # of lines - timestamp
if (length + ChatColor.stripColor(word).length() + 1 < Settings.ConsoleWidth()) {
buffer = buffer + word + " ";
length = length + ChatColor.stripColor(word).length() + 1;
} else {
// Increase # of lines to account for
length = ChatColor.stripColor(word).length() + 1;
// Leave spaces to account for timestamp and indent
buffer = buffer + "\n" + " " + word + " ";
} // 16:05:06 [INFO]
}
// Record current buffer to the to-be-submitted buffer
if (dB.record) dB.Recording.append(URLEncoder.encode(dateFormat.format(new Date())
+ " [INFO] " + buffer.replace(ChatColor.COLOR_CHAR, (char)0x01) + "\n"));
// Send buffer to the player
commandSender.sendMessage(showColor ? buffer : ChatColor.stripColor(buffer));
}
}
}