/
LogCommand.java
132 lines (105 loc) · 4.32 KB
/
LogCommand.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
package net.aufdemrand.denizen.scripts.commands.core;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URLDecoder;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.debugging.DebugLog;
import net.aufdemrand.denizen.utilities.debugging.dB;
public class LogCommand extends AbstractCommand {
public enum Type { SEVERE, INFO, WARNING, FINE, FINER, FINEST, NONE, CLEAR }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("type")
&& arg.matchesPrefix("type")
&& arg.matchesEnum(Type.values()))
scriptEntry.addObject("type", arg.asElement());
else if (!scriptEntry.hasObject("file")
&& arg.matchesPrefix("file"))
scriptEntry.addObject("file", arg.asElement());
else if (!scriptEntry.hasObject("message"))
scriptEntry.addObject("message", arg.asElement());
else
arg.reportUnhandled();
}
if(!scriptEntry.hasObject("message"))
throw new InvalidArgumentsException("Must specify a message.");
if(!scriptEntry.hasObject("file"))
throw new InvalidArgumentsException("Must specify a file.");
if (!scriptEntry.hasObject("type"))
scriptEntry.addObject("type", new Element("INFO"));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
if (!Settings.allowLogging()) {
dB.echoError("Logging disabled by administrator.");
return;
}
Element message = scriptEntry.getElement("message");
Element fileName = scriptEntry.getElement("file");
Element typeElement = scriptEntry.getElement("type");
dB.report(scriptEntry, getName(),
message.debug() + fileName.debug() + typeElement.debug());
Type type = Type.valueOf(typeElement.asString().toUpperCase());
String directory = URLDecoder.decode(System.getProperty("user.dir"));
File file = new File(directory, fileName.asString());
String output = TagManager.cleanOutputFully(message.asString());
if (type == Type.NONE) {
try {
file.getParentFile().mkdirs();
FileWriter fw = new FileWriter(file, true);
fw.write(output + "\n");
fw.close();
}
catch (IOException e) {
dB.echoError(scriptEntry.getResidingQueue(), "Error logging to file...");
dB.echoError(scriptEntry.getResidingQueue(), e);
}
return;
}
else if (type == Type.CLEAR) {
try {
file.getParentFile().mkdirs();
FileWriter fw = new FileWriter(file);
if (output.length() > 0) {
fw.write(output + "\n");
}
fw.close();
}
catch (IOException e) {
dB.echoError(scriptEntry.getResidingQueue(), "Error logging to file...");
dB.echoError(scriptEntry.getResidingQueue(), e);
}
return;
}
DebugLog log = new DebugLog("Denizen-ScriptLog-" + fileName, file.getAbsolutePath());
switch(type) {
case SEVERE:
log.severe(output);
break;
case INFO:
log.info(output);
break;
case WARNING:
log.warning(output);
break;
case FINE:
log.fine(output);
break;
case FINER:
log.finer(output);
break;
case FINEST:
log.finest(output);
}
log.close();
}
}