SubServers-2/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalSubLogger.java

133 lines
5.6 KiB
Java
Raw Normal View History

2016-12-24 05:55:17 +01:00
package net.ME1312.SubServers.Bungee.Host.Internal;
2016-12-05 04:21:04 +01:00
2016-12-24 05:55:17 +01:00
import net.ME1312.SubServers.Bungee.Library.Container;
import net.md_5.bungee.api.ProxyServer;
2016-12-05 04:21:04 +01:00
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2016-12-29 08:29:22 +01:00
public class InternalSubLogger {
private Process process;
private String name;
private Container<Boolean> log;
private PrintWriter writer = null;
private boolean started = false;
2016-12-05 04:21:04 +01:00
2016-12-29 08:29:22 +01:00
InternalSubLogger(Process process, String name, Container<Boolean> log, File file) {
this.process = process;
2016-12-05 04:21:04 +01:00
this.name = name;
this.log = log;
if (file != null)
try {
this.writer = new PrintWriter(file, "UTF-8");
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
}
}
2016-12-29 08:29:22 +01:00
public void start() {
started = true;
if (writer != null) {
this.writer.println("---------- LOG START: " + name + " ----------");
this.writer.flush();
}
new Thread(() -> {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
if (!line.startsWith(">")) {
if (log.get()) {
String msg = line;
// REGEX Formatting
String type = "INFO";
2017-01-01 20:34:46 +01:00
Matcher matcher = Pattern.compile("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARN|WARNING|ERROR|ERR|SEVERE)\\]?:?\\s*)").matcher(msg);
2016-12-29 08:29:22 +01:00
while (matcher.find()) {
2017-01-01 20:34:46 +01:00
type = matcher.group(3).toUpperCase();
2016-12-29 08:29:22 +01:00
}
2017-01-01 20:34:46 +01:00
msg = msg.replaceAll("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARN|WARNING|ERROR|ERR|SEVERE)\\]?:?\\s*)", "");
2016-12-05 04:21:04 +01:00
2016-12-29 08:29:22 +01:00
switch (type) {
case "INFO":
2017-01-01 20:34:46 +01:00
case "MESSAGE":
2016-12-29 08:29:22 +01:00
ProxyServer.getInstance().getLogger().info(name + " > " + msg);
break;
case "WARNING":
case "WARN":
ProxyServer.getInstance().getLogger().warning(name + " > " + msg);
break;
case "SEVERE":
case "ERROR":
case "ERR":
ProxyServer.getInstance().getLogger().severe(name + " > " + msg);
break;
}
}
if (writer != null) {
writer.println(line);
writer.flush();
2016-12-19 01:38:02 +01:00
}
}
2016-12-29 08:29:22 +01:00
}
2017-01-01 20:34:46 +01:00
} catch (IOException e) {} finally {
2016-12-29 08:29:22 +01:00
stop();
}
}).start();
new Thread(() -> {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = br.readLine()) != null) {
if (!line.startsWith(">")) {
if (log.get()) {
String msg = line;
2017-01-01 20:34:46 +01:00
// REGEX Formatting
String type = "INFO";
Matcher matcher = Pattern.compile("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARN|WARNING|ERROR|ERR|SEVERE)\\]?:?\\s*)").matcher(msg);
while (matcher.find()) {
type = matcher.group(3).toUpperCase();
}
msg = msg.replaceAll("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARN|WARNING|ERROR|ERR|SEVERE)\\]?:?\\s*)", "");
switch (type) {
case "INFO":
case "MESSAGE":
ProxyServer.getInstance().getLogger().info(name + " > " + msg);
break;
case "WARNING":
case "WARN":
ProxyServer.getInstance().getLogger().warning(name + " > " + msg);
break;
case "SEVERE":
case "ERROR":
case "ERR":
ProxyServer.getInstance().getLogger().severe(name + " > " + msg);
break;
}
2016-12-29 08:29:22 +01:00
}
if (writer != null) {
writer.println(line);
writer.flush();
}
2016-12-05 04:21:04 +01:00
}
}
2017-01-01 20:34:46 +01:00
} catch (IOException e) {} finally {
2016-12-29 08:29:22 +01:00
stop();
2016-12-05 04:21:04 +01:00
}
2016-12-29 08:29:22 +01:00
}).start();
}
private void stop() {
if (started) {
started = false;
2016-12-05 04:21:04 +01:00
if (writer != null) {
writer.println("---------- END LOG ----------");
writer.close();
}
}
}
}