-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
DebugCommand.java
161 lines (154 loc) · 6.16 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
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
package com.denizenscript.denizencore.scripts.commands.core;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.commands.generator.*;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
public class DebugCommand extends AbstractCommand implements Holdable {
public DebugCommand() {
setName("debug");
setSyntax("debug [<type>] [<message>] (name:<name>)");
setRequiredArguments(2, 3);
isProcedural = true;
generateDebug = false;
autoCompile();
}
// <--[command]
// @Name Debug
// @Syntax debug [<type>] [<message>] (name:<name>)
// @Required 2
// @Maximum 3
// @Short Shows a debug message.
// @Group core
// @Guide https://guide.denizenscript.com/guides/first-steps/problem-solving.html
//
// @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.
// RECORD: Use message 'start' to start recording, 'submit' to submit a recording, or 'cancel' to cancel a recording.
//
// TODO: Should [<type>] be required? Perhaps default to 'debug' mode?
//
// @Tags
// <entry[saveName].submitted> returns the submit link (if any).
//
// @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 <[milliseconds].div[1000].round> seconds!"
//
// @Usage
// Use to record a debug log of a certain script.
// - debug record start
// - run myscript
// - ~debug record submit save:mylog
// - narrate "Recorded log as <entry[mylog].submitted||<red>FAILED>"
//
// -->
public enum DebugType {
DEBUG,
HEADER,
FOOTER,
SPACER,
LOG,
APPROVAL,
ERROR,
REPORT,
EXCEPTION,
RECORD
}
@Override
public void addCustomTabCompletions(TabCompletionsBuilder tab) {
tab.add(DebugType.values());
tab.add("start", "submit", "cancel");
}
public static void autoExecute(ScriptEntry scriptEntry,
@ArgRaw @ArgLinear @ArgName("debug") String debug,
@ArgName("type") DebugType dbType,
@ArgPrefixed @ArgName("name") @ArgDefaultText("name") String name) {
if (dbType != DebugType.RECORD) {
scriptEntry.setFinished(true);
}
switch (dbType) {
case DEBUG:
Debug.echoDebug(scriptEntry, debug);
break;
case HEADER:
Debug.echoDebug(scriptEntry, Debug.DebugElement.Header, debug);
break;
case FOOTER:
Debug.echoDebug(scriptEntry, Debug.DebugElement.Footer, debug);
break;
case SPACER:
Debug.echoDebug(scriptEntry, Debug.DebugElement.Spacer, debug);
break;
case LOG:
Debug.log(debug);
break;
case APPROVAL:
Debug.echoApproval(debug);
break;
case ERROR:
Debug.echoError(scriptEntry, debug);
break;
case REPORT:
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, name, debug);
}
break;
case EXCEPTION:
Debug.echoError(scriptEntry, new RuntimeException(debug));
break;
case RECORD:
String form = CoreUtilities.toLowerCase(debug);
switch (form) {
case "start":
Debug.echoDebug(scriptEntry, "Starting debug recording...");
Debug.startRecording();
scriptEntry.setFinished(true);
break;
case "cancel":
Debug.echoDebug(scriptEntry, "Stopping debug recording...");
Debug.stopRecording();
scriptEntry.setFinished(true);
break;
case "submit":
DenizenCore.implementation.submitRecording(s -> {
if (s == null) {
Debug.echoDebug(scriptEntry, "Submit failed.");
}
else if (s.equals("disabled")) {
Debug.echoDebug(scriptEntry, "Submit failed: not recording");
}
else {
Debug.echoDebug(scriptEntry, "Submitted to " + s);
scriptEntry.addObject("submitted", new ElementTag(s));
}
scriptEntry.setFinished(true);
});
break;
default:
Debug.echoError("Debug 'record' command failed: unknown record form '" + form + "'");
scriptEntry.setFinished(true);
break;
}
break;
}
}
}