SteamWar/SpigotCore
Archiviert
13
0

Improved error and crash reporting
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
Lixfel 2022-08-23 20:10:49 +02:00
Ursprung 3693db65c7
Commit 2c5ee201f6
4 geänderte Dateien mit 28 neuen und 33 gelöschten Zeilen

Datei anzeigen

@ -104,8 +104,7 @@ public class Core extends JavaPlugin{
public void onEnable() { public void onEnable() {
setSqlConfig(); setSqlConfig();
errorHandler = new ErrorHandler(); errorHandler = new ErrorHandler();
if(VERSION > 15) crashDetector = new CrashDetector();
crashDetector = new CrashDetector();
SWCommandUtils.init((SWTypeMapperCreator<TypeMapper<Object>, CommandSender, Object>) (mapper, tabCompleter) -> new TypeMapper<Object>() { SWCommandUtils.init((SWTypeMapperCreator<TypeMapper<Object>, CommandSender, Object>) (mapper, tabCompleter) -> new TypeMapper<Object>() {
@Override @Override
@ -142,8 +141,7 @@ public class Core extends JavaPlugin{
@Override @Override
public void onDisable() { public void onDisable() {
TinyProtocol.instance.close(); TinyProtocol.instance.close();
if(VERSION > 15) crashDetector.stop();
crashDetector.stop();
errorHandler.unregister(); errorHandler.unregister();
if(!standalone) { if(!standalone) {
Statement.close(); Statement.close();

Datei anzeigen

@ -51,7 +51,7 @@ public class CrashDetector {
while (run) { while (run) {
long curTime = System.currentTimeMillis(); long curTime = System.currentTimeMillis();
if(curTime - TIMEOUT > lastTick.get()) { if(curTime - TIMEOUT > lastTick.get()) {
SWException.log(Bukkit.getServer().getVersion() + " server hung for " + (curTime - lastTick.get()) + "ms, ", Arrays.stream(mainThread.getStackTrace()).map(StackTraceElement::toString).collect(Collectors.joining("\n"))); SWException.log("Server hung for " + (curTime - lastTick.get()) + "ms", Arrays.stream(mainThread.getStackTrace()).map(StackTraceElement::toString).collect(Collectors.joining("\n")));
lastTick.set(curTime); lastTick.set(curTime);
} }
try { try {

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.core; package de.steamwar.core;
import com.comphenix.tinyprotocol.Reflection;
import de.steamwar.sql.SWException; import de.steamwar.sql.SWException;
import de.steamwar.sql.Statement; import de.steamwar.sql.Statement;
@ -34,10 +35,15 @@ import java.util.logging.Logger;
public class ErrorHandler extends Handler { public class ErrorHandler extends Handler {
private boolean logDisabled = false; private final long watchdogThreadId;
public ErrorHandler(){ public ErrorHandler(){
Logger.getLogger("").addHandler(this); Logger.getLogger("").addHandler(this);
Class<?> watchdogThread = Reflection.getClass("org.spigotmc.WatchdogThread");
Reflection.FieldAccessor<?> getInstance = Reflection.getField(watchdogThread, watchdogThread, 0);
Thread watchdog = (Thread) getInstance.get(null);
watchdogThreadId = watchdog.getId();
} }
void unregister() { void unregister() {
@ -45,16 +51,15 @@ public class ErrorHandler extends Handler {
} }
@Override @Override
@SuppressWarnings("deprecation")
public void publish(LogRecord logRecord) { public void publish(LogRecord logRecord) {
if(logRecord.getLevel().intValue() < Level.WARNING.intValue()) if(logRecord.getLevel().intValue() < Level.WARNING.intValue())
return; return;
if(logDisabled) if(watchdogThreadId == logRecord.getThreadID())
return; return;
String message = logRecord.getMessage(); String message = logRecord.getMessage() != null ? logRecord.getMessage() : "";
if(message == null)
message = "";
for(String reason : ignoreStartsWith) for(String reason : ignoreStartsWith)
if(message.startsWith(reason)) if(message.startsWith(reason))
return; return;
@ -62,20 +67,6 @@ public class ErrorHandler extends Handler {
if(message.contains(reason)) if(message.contains(reason))
return; return;
switch (message) {
case "The server has stopped responding!":
logDisabled = true;
return;
case "------------------------------":
message = "Server stopped responding";
logDisabled = true;
break;
case "Exception stopping the server":
logDisabled = true;
break;
default:
}
ByteArrayOutputStream stacktraceOutput = new ByteArrayOutputStream(); ByteArrayOutputStream stacktraceOutput = new ByteArrayOutputStream();
if(logRecord.getThrown() != null) if(logRecord.getThrown() != null)
logRecord.getThrown().printStackTrace(new PrintStream(stacktraceOutput)); logRecord.getThrown().printStackTrace(new PrintStream(stacktraceOutput));

Datei anzeigen

@ -20,22 +20,28 @@
package de.steamwar.sql; package de.steamwar.sql;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.File;
public class SWException { public class SWException {
private SWException(){} private SWException(){}
private static final String serverVersion = Bukkit.getServer().getVersion();
private static final String cwd = System.getProperty("user.dir");
private static final String server = new File(".").getAbsoluteFile().getName();
public static void log(String message, String stacktrace){ public static void log(String message, String stacktrace){
message += "\n"; StringBuilder msgBuilder = new StringBuilder(message).append("\nPlayers: ");
for(Player player : Bukkit.getOnlinePlayers()) for(Player player : Bukkit.getOnlinePlayers())
message += player.getName() + " "; msgBuilder.append(player.getName()).append(" ");
msgBuilder.append("\nWorlds: ");
for(World world : Bukkit.getWorlds())
msgBuilder.append(world.getName()).append(" ");
msgBuilder.append("\nServer: ").append(serverVersion);
msgBuilder.append("\nCWD: ").append(cwd);
String server; Provider.impl.logException(server, msgBuilder.toString(), stacktrace);
if(Bukkit.getWorlds().isEmpty())
server = Bukkit.getName();
else
server = Bukkit.getWorlds().get(0).getName();
Provider.impl.logException(server, message, stacktrace);
} }
} }