/
SubLoggerImpl.java
244 lines (223 loc) · 8.72 KB
/
SubLoggerImpl.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
package net.ME1312.SubServers.Host.Executable;
import net.ME1312.Galaxi.Library.Callback.Callback;
import net.ME1312.Galaxi.Library.Container;
import net.ME1312.Galaxi.Library.Log.LogStream;
import net.ME1312.Galaxi.Library.Log.Logger;
import net.ME1312.Galaxi.Library.NamedContainer;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.SubData.Client.DataClient;
import net.ME1312.SubData.Client.Library.DisconnectReason;
import net.ME1312.SubData.Client.SubDataClient;
import net.ME1312.SubServers.Host.ExHost;
import net.ME1312.SubServers.Host.Library.TextColor;
import net.ME1312.SubServers.Host.Network.Packet.PacketOutExLogMessage;
import net.ME1312.SubServers.Host.SubAPI;
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Internal Process Logger Class
*/
public class SubLoggerImpl {
protected Process process;
private Object handle;
protected final Logger logger;
protected final String name;
protected UUID address;
protected Container<Boolean> log;
protected static boolean logn = true;
protected static boolean logc = true;
protected File file;
private SubDataClient channel = null;
private PrintWriter writer = null;
private boolean started = false;
private Thread out = null;
private Thread err = null;
/**
* Creates a new Internal Process Logger
*
* @param process Process
* @param user Object using this logger (or null)
* @param name Prefix
* @param address External Logger Address
* @param log Console Logging Status
* @param file File to log to (or null for disabled)
*/
protected SubLoggerImpl(Process process, Object user, String name, UUID address, Container<Boolean> log, File file) {
this.process = process;
this.handle = user;
this.logger = new Logger(name);
this.name = name;
this.address = address;
this.log = log;
this.file = file;
}
/**
* Start Logger
*/
public void start() {
started = true;
if (file != null && writer == null) {
try {
this.writer = new PrintWriter(file, "UTF-8");
this.writer.println("---------- LOG START \u2014 " + name + " ----------");
this.writer.flush();
} catch (IOException e) {
logger.error.println(e);
}
}
Process process = this.process;
ExHost host = SubAPI.getInstance().getInternals();
if (logn) Util.isException(() -> {
channel = (SubDataClient) SubAPI.getInstance().getSubDataNetwork()[0].openChannel();
channel.on.closed(new Callback<NamedContainer<DisconnectReason, DataClient>>() {
@Override
public void run(NamedContainer<DisconnectReason, DataClient> client) {
if (started && SubLoggerImpl.this.process != null && process == SubLoggerImpl.this.process && process.isAlive()) {
int reconnect = host.config.get().getMap("Settings").getMap("SubData").getInt("Reconnect", 30);
if (Util.getDespiteException(() -> Util.reflect(ExHost.class.getDeclaredField("reconnect"), host), false) && reconnect > 0
&& client.name() != DisconnectReason.PROTOCOL_MISMATCH && client.name() != DisconnectReason.ENCRYPTION_MISMATCH) {
Timer timer = new Timer(SubAPI.getInstance().getAppInfo().getName() + "::Log_Reconnect_Handler");
Callback<NamedContainer<DisconnectReason, DataClient>> run = this;
reconnect++;
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!started || SubLoggerImpl.this.process == null || process != SubLoggerImpl.this.process || !process.isAlive()) {
timer.cancel();
} else try {
SubDataClient open = (SubDataClient) SubAPI.getInstance().getSubDataNetwork()[0].openChannel();
open.on.closed(run);
channel = open;
timer.cancel();
} catch (NullPointerException | IOException e) {}
}
}, TimeUnit.SECONDS.toMillis(reconnect), TimeUnit.SECONDS.toMillis(reconnect));
}
}
}
});
});
if (out == null) (out = new Thread(() -> start(process.getInputStream(), false), SubAPI.getInstance().getAppInfo().getName() + "::Log_Spooler(" + name + ')')).start();
if (err == null) (err = new Thread(() -> start(process.getErrorStream(), true), SubAPI.getInstance().getAppInfo().getName() + "::Error_Spooler(" + name + ')')).start();
}
@SuppressWarnings("deprecation")
private void start(InputStream in, boolean isErr) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
log(line);
}
} catch (IOException e) {} finally {
if (isErr) {
err = null;
} else {
out = null;
}
stop();
}
}
private void log(String line) {
if (!line.startsWith(">")) {
String msg = line;
LogStream level;
// REGEX Formatting
String type = "";
Matcher matcher = Pattern.compile("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARNING|WARN|ERROR|ERR|SEVERE)\\]?:?(?:\\s*>)?\\s*)").matcher(msg.replaceAll("\u001B\\[[;\\d]*m", ""));
while (matcher.find()) {
type = matcher.group(3).toUpperCase();
}
msg = msg.replaceAll("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARNING|WARN|ERROR|ERR|SEVERE)\\]?:?(?:\\s*>)?\\s*)", "");
// Determine LOG LEVEL
switch (type) {
case "WARNING":
case "WARN":
level = logger.warn;
break;
case "SEVERE":
level = logger.severe;
break;
case "ERROR":
case "ERR":
level = logger.error;
break;
default:
level = logger.info;
}
// Log to NETWORK
if (log.get() && channel != null && !channel.isClosed()) channel.sendPacket(new PacketOutExLogMessage(address, line));
// Log to CONSOLE
if (log.get() && logc) level.println(TextColor.convertColor(msg));
// Log to FILE
if (writer != null) {
writer.println(line);
writer.flush();
}
}
}
/**
* Stop Logger
*/
public void stop() {
try {
if (out != null) out.interrupt();
if (err != null) err.interrupt();
destroy();
} catch (NullPointerException e) {}
}
private void destroy() {
if (started) {
started = false;
if (writer != null) {
PrintWriter writer = this.writer;
this.writer = null;
int l = (int) Math.floor((("---------- LOG START \u2014 " + name + " ----------").length() - 9) / 2);
String s = "";
while (s.length() < l) s += '-';
if (writer != null) {
writer.println(s + " LOG END " + s);
writer.close();
}
}
if (channel != null && !channel.isClosed()) {
channel.sendPacket(new PacketOutExLogMessage(address, true));
}
channel = null;
}
}
/**
* Gets the Object using this Logger
*
* @return Object
*/
public Object getHandler() {
return handle;
}
/**
* Gets the Name of the task logging
*
* @return Log Task Name
*/
public String getName() {
return name;
}
/**
* Get if the Logger is currently logging
*
* @return Logging Status
*/
public boolean isLogging() {
return log.get();
}
/**
* Get the Logging Address
*
* @return Address
*/
public UUID getAddress() {
return address;
}
}