-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
DebugCommand.java
135 lines (117 loc) · 4.59 KB
/
DebugCommand.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
package com.denizenscript.denizencore.scripts.commands.core;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import java.util.HashSet;
public class DebugCommand extends AbstractCommand {
// <--[command]
// @Name Debug
// @Syntax debug [<type>] [<message>] (name:<name>)
// @Required 2
// @Short Shows a debug message.
// @Group core
//
// @Description
// Use to quickly output debug information to console.
//
// Valid types include:
// DEBUG: standard hideable debug.
// HEADER: standard hideable debug inside a header line.
// FOOTER: a footer line.
// SPACER: a spacer line.
// LOG: global output, non-hideable.
// APPROVAL: "Okay!" output, non-hideable.
// ERROR: "Error!" output, non-hideable.
// REPORT: normally used to describe the arguments of a command, requires a name, hideable.
// EXCEPTION: outputs a full java stacktrace.
//
// TODO: Should [<type>] be required? Perhaps default to 'debug' mode?
//
// @Tags
// None
//
// @Usage
// Use to show an error
// - debug error "Something went wrong!"
//
// @Usage
// Use to add some information to help your own ability to read debug output from you script
// - debug debug "Time is currently <def[milliseconds].div[1000].round> seconds!"
//
// -->
public enum DebugType {
DEBUG,
HEADER,
FOOTER,
SPACER,
LOG,
APPROVAL,
ERROR,
REPORT,
EXCEPTION
}
public static HashSet<String> DBINFO = Argument.precalcEnum(DebugType.values());
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(DBINFO)) {
scriptEntry.addObject("type", arg.asElement());
}
else if (!scriptEntry.hasObject("debug")) {
scriptEntry.addObject("debug", new ElementTag(arg.raw_value));
}
else if (!scriptEntry.hasObject("name")
&& arg.matchesOnePrefix("name")) {
scriptEntry.addObject("name", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("type") || !scriptEntry.hasObject("debug")) {
throw new InvalidArgumentsException("Must specify a definition and value!");
}
scriptEntry.defaultObject("name", new ElementTag("name"));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag debug = scriptEntry.getElement("debug");
ElementTag type = scriptEntry.getElement("type");
ElementTag name = scriptEntry.getElement("name");
// Intentionally do not DB REPORT - we're making our own debug output!
switch (DebugType.valueOf(type.asString().toUpperCase())) {
case DEBUG:
Debug.echoDebug(scriptEntry, debug.asString());
break;
case HEADER:
Debug.echoDebug(scriptEntry, Debug.DebugElement.Header, debug.asString());
break;
case FOOTER:
Debug.echoDebug(scriptEntry, Debug.DebugElement.Footer, debug.asString());
break;
case SPACER:
Debug.echoDebug(scriptEntry, Debug.DebugElement.Spacer, debug.asString());
break;
case LOG:
Debug.log(debug.asString());
break;
case APPROVAL:
Debug.echoApproval(debug.asString());
break;
case ERROR:
Debug.echoError(scriptEntry.getResidingQueue(), debug.asString());
break;
case REPORT:
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, name.asString(), debug.asString());
}
break;
case EXCEPTION:
Debug.echoError(scriptEntry.getResidingQueue(), new RuntimeException(debug.asString()));
}
}
}