2017-04-01 07:37:48 +02:00
|
|
|
package net.ME1312.SubServers.Host.Executable;
|
|
|
|
|
2019-05-13 05:28:27 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Map.ObjectMap;
|
2018-09-09 00:06:21 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Container;
|
|
|
|
import net.ME1312.Galaxi.Library.Log.LogStream;
|
|
|
|
import net.ME1312.Galaxi.Library.Log.Logger;
|
2019-05-13 05:28:27 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Util;
|
|
|
|
import net.ME1312.SubData.Client.SubDataClient;
|
2018-03-21 21:45:59 +01:00
|
|
|
import net.ME1312.SubServers.Host.Library.TextColor;
|
2017-04-01 07:37:48 +02:00
|
|
|
import net.ME1312.SubServers.Host.Network.Packet.PacketOutExLogMessage;
|
|
|
|
import net.ME1312.SubServers.Host.SubAPI;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal Process Logger Class
|
|
|
|
*/
|
|
|
|
public class SubLogger {
|
|
|
|
protected Process process;
|
|
|
|
private Object handle;
|
2017-04-01 22:31:57 +02:00
|
|
|
protected final Logger logger;
|
|
|
|
protected final String name;
|
2017-04-01 07:37:48 +02:00
|
|
|
protected UUID address;
|
|
|
|
protected Container<Boolean> log;
|
2019-01-17 19:12:32 +01:00
|
|
|
protected static boolean logn = true;
|
|
|
|
protected static boolean logc = true;
|
2017-04-01 07:37:48 +02:00
|
|
|
protected File file;
|
2019-05-13 05:28:27 +02:00
|
|
|
private SubDataClient channel = null;
|
2017-04-01 07:37:48 +02:00
|
|
|
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 SubLogger(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;
|
2019-05-13 05:28:27 +02:00
|
|
|
if (logn) Util.isException(() -> channel = (SubDataClient) SubAPI.getInstance().getSubDataNetwork()[0].newChannel());
|
2017-04-01 07:37:48 +02:00
|
|
|
if (file != null && writer == null) {
|
|
|
|
try {
|
|
|
|
this.writer = new PrintWriter(file, "UTF-8");
|
|
|
|
this.writer.println("---------- LOG START \u2014 " + name + " ----------");
|
|
|
|
this.writer.flush();
|
2019-05-13 05:28:27 +02:00
|
|
|
} catch (IOException e) {
|
2017-04-03 19:52:28 +02:00
|
|
|
logger.error.println(e);
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-06 07:58:13 +01:00
|
|
|
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();
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
private void start(InputStream in, boolean isErr) {
|
|
|
|
try {
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(in));
|
|
|
|
String line;
|
|
|
|
while ((line = br.readLine()) != null) {
|
2019-01-17 19:12:32 +01:00
|
|
|
log(line);
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
} catch (IOException e) {} finally {
|
|
|
|
if (isErr) {
|
|
|
|
err = null;
|
|
|
|
} else {
|
|
|
|
out = null;
|
|
|
|
}
|
|
|
|
|
2018-11-03 00:48:46 +01:00
|
|
|
stop();
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:12:32 +01:00
|
|
|
private void log(String line) {
|
2019-01-05 05:35:20 +01:00
|
|
|
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
|
2019-05-13 05:28:27 +02:00
|
|
|
if (log.get() && channel != null && !channel.isClosed()) channel.sendPacket(new PacketOutExLogMessage(address, line));
|
2019-01-05 05:35:20 +01:00
|
|
|
|
|
|
|
// Log to CONSOLE
|
2019-01-17 19:12:32 +01:00
|
|
|
if (log.get() && logc) level.println(TextColor.convertColor(msg));
|
2019-01-05 05:35:20 +01:00
|
|
|
|
|
|
|
// Log to FILE
|
|
|
|
if (writer != null) {
|
|
|
|
writer.println(line);
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:37:48 +02:00
|
|
|
/**
|
|
|
|
* Stop Logger
|
|
|
|
*/
|
|
|
|
public void stop() {
|
|
|
|
if (out != null) out.interrupt();
|
|
|
|
if (err != null) err.interrupt();
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void destroy() {
|
|
|
|
if (started) {
|
|
|
|
started = false;
|
|
|
|
if (writer != null) {
|
2017-04-14 04:39:51 +02:00
|
|
|
PrintWriter writer = this.writer;
|
|
|
|
this.writer = null;
|
2017-04-01 07:37:48 +02:00
|
|
|
int l = (int) Math.floor((("---------- LOG START \u2014 " + name + " ----------").length() - 9) / 2);
|
|
|
|
String s = "";
|
|
|
|
while (s.length() < l) s += '-';
|
2019-01-14 01:52:44 +01:00
|
|
|
if (writer != null) {
|
|
|
|
writer.println(s + " LOG END " + s);
|
|
|
|
writer.close();
|
|
|
|
}
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
2019-05-13 05:28:27 +02:00
|
|
|
if (channel != null && !channel.isClosed()) {
|
|
|
|
channel.sendPacket(new PacketOutExLogMessage(address, true));
|
|
|
|
}
|
|
|
|
channel = null;
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
}
|