From 2c5ee201f66b00d37929ee8384a2c7b5af59e5b4 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 23 Aug 2022 20:10:49 +0200 Subject: [PATCH] Improved error and crash reporting --- .../src/de/steamwar/core/Core.java | 6 ++-- .../src/de/steamwar/core/CrashDetector.java | 2 +- .../src/de/steamwar/core/ErrorHandler.java | 29 +++++++------------ .../src/de/steamwar/sql/SWException.java | 24 +++++++++------ 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index c510a6b..2e9b2c5 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -104,8 +104,7 @@ public class Core extends JavaPlugin{ public void onEnable() { setSqlConfig(); errorHandler = new ErrorHandler(); - if(VERSION > 15) - crashDetector = new CrashDetector(); + crashDetector = new CrashDetector(); SWCommandUtils.init((SWTypeMapperCreator, CommandSender, Object>) (mapper, tabCompleter) -> new TypeMapper() { @Override @@ -142,8 +141,7 @@ public class Core extends JavaPlugin{ @Override public void onDisable() { TinyProtocol.instance.close(); - if(VERSION > 15) - crashDetector.stop(); + crashDetector.stop(); errorHandler.unregister(); if(!standalone) { Statement.close(); diff --git a/SpigotCore_Main/src/de/steamwar/core/CrashDetector.java b/SpigotCore_Main/src/de/steamwar/core/CrashDetector.java index 76cd8a8..92dd8dd 100644 --- a/SpigotCore_Main/src/de/steamwar/core/CrashDetector.java +++ b/SpigotCore_Main/src/de/steamwar/core/CrashDetector.java @@ -51,7 +51,7 @@ public class CrashDetector { while (run) { long curTime = System.currentTimeMillis(); 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); } try { diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java index deba82f..0f41ff6 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java +++ b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java @@ -19,6 +19,7 @@ package de.steamwar.core; +import com.comphenix.tinyprotocol.Reflection; import de.steamwar.sql.SWException; import de.steamwar.sql.Statement; @@ -34,10 +35,15 @@ import java.util.logging.Logger; public class ErrorHandler extends Handler { - private boolean logDisabled = false; + private final long watchdogThreadId; public ErrorHandler(){ 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() { @@ -45,16 +51,15 @@ public class ErrorHandler extends Handler { } @Override + @SuppressWarnings("deprecation") public void publish(LogRecord logRecord) { if(logRecord.getLevel().intValue() < Level.WARNING.intValue()) return; - if(logDisabled) + if(watchdogThreadId == logRecord.getThreadID()) return; - String message = logRecord.getMessage(); - if(message == null) - message = ""; + String message = logRecord.getMessage() != null ? logRecord.getMessage() : ""; for(String reason : ignoreStartsWith) if(message.startsWith(reason)) return; @@ -62,20 +67,6 @@ public class ErrorHandler extends Handler { if(message.contains(reason)) 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(); if(logRecord.getThrown() != null) logRecord.getThrown().printStackTrace(new PrintStream(stacktraceOutput)); diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index d3703a6..7aa6cf7 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -20,22 +20,28 @@ package de.steamwar.sql; import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.entity.Player; +import java.io.File; + public class 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){ - message += "\n"; + StringBuilder msgBuilder = new StringBuilder(message).append("\nPlayers: "); 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; - if(Bukkit.getWorlds().isEmpty()) - server = Bukkit.getName(); - else - server = Bukkit.getWorlds().get(0).getName(); - - Provider.impl.logException(server, message, stacktrace); + Provider.impl.logException(server, msgBuilder.toString(), stacktrace); } }