Revise logging GC strategy

This commit is contained in:
ME1312 2019-01-04 23:35:20 -05:00
parent 9cb499c4ec
commit eb70ebaeea
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
7 changed files with 119 additions and 143 deletions

View File

@ -19,8 +19,8 @@ public class Executable {
String[] cmd;
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
if (gitbash != null && (exec.startsWith("bash ") || exec.startsWith("sh ")))
exec = "\"" + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lc \"" +
exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + "\"";
exec = '"' + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lc \"" +
exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + '"';
cmd = new String[]{"cmd.exe", "/q", "/c", '"'+exec+'"'};
} else {
cmd = new String[]{"sh", "-lc", exec};

View File

@ -71,22 +71,8 @@ public class ExternalSubLogger extends SubLogger {
}
}
/**
* Get the External Logger Address
*
* @return External Address
*/
public UUID getExternalAddress() {
return id;
}
/**
* Log a Message
*
* @param line Message
*/
@SuppressWarnings("deprecation")
public void log(String line) {
private void log(String line) {
if (started) {
String msg = line;
Level level;
@ -134,12 +120,18 @@ public class ExternalSubLogger extends SubLogger {
writer.println(line);
writer.flush();
}
gc++;
gc();
}
}
/**
* Get the External Logger Address
*
* @return External Address
*/
public UUID getExternalAddress() {
return id;
}
@Override
public void registerFilter(SubLogFilter filter) {
if (Util.isNull(filter)) throw new NullPointerException();

View File

@ -78,57 +78,7 @@ public class InternalSubLogger extends SubLogger {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
if (!line.startsWith(">")) {
String msg = line;
Level 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 = Level.WARNING;
break;
case "SEVERE":
case "ERROR":
case "ERR":
level = Level.SEVERE;
break;
default:
level = Level.INFO;
}
// Filter Message
boolean allow = log.get() && (!SubAPI.getInstance().getInternals().canSudo || SubAPI.getInstance().getInternals().sudo == null || SubAPI.getInstance().getInternals().sudo == getHandler());
List<SubLogFilter> filters = new ArrayList<SubLogFilter>();
filters.addAll(this.filters);
for (SubLogFilter filter : filters)
try {
allow = (filter.log(level, msg) && allow);
} catch (Throwable e) {
new InvocationTargetException(e, "Exception while running SubLogger Event").printStackTrace();
}
// Log to CONSOLE
if (allow) ProxyServer.getInstance().getLogger().log(level, name + " > " + msg);
// Log to FILE
if (writer != null) {
writer.println(line);
writer.flush();
}
gc++;
gc();
}
log(line);
}
} catch (IOException e) {} finally {
if (isErr) {
@ -141,6 +91,57 @@ public class InternalSubLogger extends SubLogger {
}
}
private void log(String line) {
if (!line.startsWith(">")) {
String msg = line;
Level 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 = Level.WARNING;
break;
case "SEVERE":
case "ERROR":
case "ERR":
level = Level.SEVERE;
break;
default:
level = Level.INFO;
}
// Filter Message
boolean allow = log.get() && (!SubAPI.getInstance().getInternals().canSudo || SubAPI.getInstance().getInternals().sudo == null || SubAPI.getInstance().getInternals().sudo == getHandler());
List<SubLogFilter> filters = new ArrayList<SubLogFilter>();
filters.addAll(this.filters);
for (SubLogFilter filter : filters)
try {
allow = (filter.log(level, msg) && allow);
} catch (Throwable e) {
new InvocationTargetException(e, "Exception while running SubLogger Event").printStackTrace();
}
// Log to CONSOLE
if (allow) ProxyServer.getInstance().getLogger().log(level, name + " > " + msg);
// Log to FILE
if (writer != null) {
writer.println(line);
writer.flush();
}
}
}
@Override
public void stop() {
if (out != null) out.interrupt();

View File

@ -4,17 +4,6 @@ package net.ME1312.SubServers.Bungee.Host;
* SubLogger Layout Class
*/
public abstract class SubLogger {
public static final int MAX_GC = Integer.getInteger("subservers.logging.max_gc", 4096);
private static boolean gc_running = false;
protected static int gc = 0;
protected static void gc() {
if (!gc_running && MAX_GC > 0 && gc >= MAX_GC) {
gc_running = true;
System.gc();
gc = 0;
gc_running = false;
}
}
/**
* Gets the Name of the task logging

View File

@ -7,6 +7,7 @@ import net.ME1312.SubServers.Bungee.Library.Version.Version;
import net.ME1312.SubServers.Bungee.Network.Client;
import net.ME1312.SubServers.Bungee.Network.PacketIn;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.UUID;
@ -25,7 +26,10 @@ public class PacketInExLogMessage implements PacketIn {
public void execute(Client client, YAMLSection data) {
try {
if (data.contains("h") && data.contains("m") && data.getRawString("m").length() != 0 && loggers.keySet().contains(UUID.fromString(data.getRawString("h")))) {
loggers.get(UUID.fromString(data.getRawString("h"))).log(data.getRawString("m"));
Method m = ExternalSubLogger.class.getDeclaredMethod("log", String.class);
m.setAccessible(true);
m.invoke(loggers.get(UUID.fromString(data.getRawString("h"))), data.getRawString("m"));
m.setAccessible(false);
}
} catch (Exception e) {
e.printStackTrace();

View File

@ -19,8 +19,8 @@ public class Executable {
String[] cmd;
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
if (gitbash != null && (exec.startsWith("bash ") || exec.startsWith("sh ")))
exec = "\"" + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lc \"" +
exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + "\"";
exec = '"' + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lc \"" +
exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + '"';
cmd = new String[]{"cmd.exe", "/q", "/c", '"'+exec+'"'};
} else {
cmd = new String[]{"sh", "-lc", exec};

View File

@ -17,7 +17,6 @@ import java.util.regex.Pattern;
* Internal Process Logger Class
*/
public class SubLogger {
public static final int MAX_GC = Integer.getInteger("subservers.logging.max_gc", 4096);
protected Process process;
private Object handle;
protected final Logger logger;
@ -50,17 +49,6 @@ public class SubLogger {
this.file = file;
}
private static boolean gc_running = false;
private static int gc = 0;
private static void gc() {
if (!gc_running && MAX_GC > 0 && gc >= MAX_GC) {
gc_running = true;
System.gc();
gc = 0;
gc_running = false;
}
}
/**
* Start Logger
*/
@ -83,55 +71,12 @@ public class SubLogger {
@SuppressWarnings("deprecation")
private void start(InputStream in, boolean isErr) {
try {
YAMLSection yaml = SubAPI.getInstance().getInternals().config.get().getSection("Settings");
boolean network = SubAPI.getInstance().getInternals().config.get().getSection("Settings").getBoolean("Network-Log", true),
console = SubAPI.getInstance().getInternals().config.get().getSection("Settings").getBoolean("Console-Log", true);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
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() && yaml.getBoolean("Network-Log", true)) SubAPI.getInstance().getSubDataNetwork().sendPacket(new PacketOutExLogMessage(address, line));
// Log to CONSOLE
if (log.get() && yaml.getBoolean("Console-Log", true)) level.println(TextColor.convertColor(msg));
// Log to FILE
if (writer != null) {
writer.println(line);
writer.flush();
}
gc++;
gc();
}
log(line, network, console);
}
} catch (IOException e) {} finally {
if (isErr) {
@ -144,6 +89,51 @@ public class SubLogger {
}
}
private void log(String line, boolean network, boolean console) {
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() && network) SubAPI.getInstance().getSubDataNetwork().sendPacket(new PacketOutExLogMessage(address, line));
// Log to CONSOLE
if (log.get() && console) level.println(TextColor.convertColor(msg));
// Log to FILE
if (writer != null) {
writer.println(line);
writer.flush();
}
}
}
/**
* Stop Logger
*/