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

133 lines
4.5 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;
2017-01-07 20:06:54 +01:00
/**
* Internal Process Logger Class
*/
2016-12-29 08:29:22 +01:00
public class InternalSubLogger {
private Process process;
private String name;
private Container<Boolean> log;
2017-01-07 20:06:54 +01:00
private File file;
2016-12-29 08:29:22 +01:00
private PrintWriter writer = null;
private boolean started = false;
2017-01-07 20:06:54 +01:00
private Thread out = null;
private Thread err = null;
2016-12-05 04:21:04 +01:00
2017-01-07 20:06:54 +01:00
/**
* Creates a new Internal Process Logger
*
* @param process Process
* @param name Prefix
* @param log Console Logging Status
* @param file File to log to (or null for disabled)
*/
public InternalSubLogger(Process process, String name, Container<Boolean> log, File file) {
2016-12-29 08:29:22 +01:00
this.process = process;
2016-12-05 04:21:04 +01:00
this.name = name;
this.log = log;
2017-01-07 20:06:54 +01:00
this.file = file;
}
/**
* Start the Logger
*/
public void start() {
started = true;
if (file != null && writer == null) {
2016-12-05 04:21:04 +01:00
try {
this.writer = new PrintWriter(file, "UTF-8");
2017-01-07 20:06:54 +01:00
this.writer.println("---------- LOG START \u2014 " + name + " ----------");
this.writer.flush();
2016-12-05 04:21:04 +01:00
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
}
2016-12-29 08:29:22 +01:00
}
2017-01-07 20:06:54 +01:00
if (out == null) (out = new Thread(() -> start(process.getInputStream(), false))).start();
if (err == null) (err = new Thread(() -> start(process.getErrorStream(), true))).start();
}
2017-01-07 20:06:54 +01:00
private void start(InputStream in, boolean isErr) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
if (!line.startsWith(">")) {
if (log.get()) {
String msg = line;
2016-12-05 04:21:04 +01:00
2017-01-07 20:06:54 +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();
2016-12-19 01:38:02 +01:00
}
2017-01-01 20:34:46 +01:00
2017-01-07 20:06:54 +01:00
msg = msg.replaceAll("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARN|WARNING|ERROR|ERR|SEVERE)\\]?:?\\s*)", "");
2017-01-01 20:34:46 +01:00
2017-01-07 20:06:54 +01:00
// Determine LOG LEVEL
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
}
2016-12-05 04:21:04 +01:00
}
2017-01-07 20:06:54 +01:00
// Log to FILE
if (writer != null) {
writer.println(line);
writer.flush();
}
2016-12-05 04:21:04 +01:00
}
}
2017-01-07 20:06:54 +01:00
} catch (IOException e) {} finally {
if (isErr) {
err = null;
} else {
out = null;
}
destroy();
}
}
/**
* Stop the Logger
*/
public void stop() {
if (out != null) out.interrupt();
if (err != null) err.interrupt();
destroy();
2016-12-29 08:29:22 +01:00
}
2017-01-07 20:06:54 +01:00
private void destroy() {
2016-12-29 08:29:22 +01:00
if (started) {
started = false;
2016-12-05 04:21:04 +01:00
if (writer != null) {
2017-01-07 20:06:54 +01:00
int l = (int) Math.floor((("---------- LOG START \u2014 " + name + " ----------").length() - 9) / 2);
String s = "";
while (s.length() < l) s += '-';
writer.println(s + " LOG END " + s);
2016-12-05 04:21:04 +01:00
writer.close();
2017-01-07 20:06:54 +01:00
writer = null;
2016-12-05 04:21:04 +01:00
}
}
}
}