Improved error and crash reporting #208
@ -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<TypeMapper<Object>, CommandSender, Object>) (mapper, tabCompleter) -> new TypeMapper<Object>() {
|
||||
@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();
|
||||
|
@ -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 {
|
||||
|
@ -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));
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren