From 94ccf7cc751bb44e08093d4c18d4987b5281abbd Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 10 Feb 2020 13:37:03 +0100 Subject: [PATCH] Adding /bug and automated BungeeCord [ERROR] tracking --- src/de/steamwar/bungeecore/BungeeCore.java | 2 + src/de/steamwar/bungeecore/ErrorLogger.java | 50 +++++++++++++++++++ .../bungeecore/commands/BugCommand.java | 24 +++++++++ .../steamwar/bungeecore/sql/SWException.java | 9 ++++ 4 files changed, 85 insertions(+) create mode 100644 src/de/steamwar/bungeecore/ErrorLogger.java create mode 100644 src/de/steamwar/bungeecore/commands/BugCommand.java create mode 100644 src/de/steamwar/bungeecore/sql/SWException.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 8005f190..a00df544 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -52,6 +52,7 @@ public class BungeeCore extends Plugin { setInstance(this); loadConfig(); + new ErrorLogger(); new ConnectionListener(); new Forge(); new LabyMod(); @@ -101,6 +102,7 @@ public class BungeeCore extends Plugin { @Override public void onDisable(){ + ErrorLogger.stop(); SQL.close(); } diff --git a/src/de/steamwar/bungeecore/ErrorLogger.java b/src/de/steamwar/bungeecore/ErrorLogger.java new file mode 100644 index 00000000..12304db2 --- /dev/null +++ b/src/de/steamwar/bungeecore/ErrorLogger.java @@ -0,0 +1,50 @@ +package de.steamwar.bungeecore; + +import de.steamwar.bungeecore.sql.SWException; +import net.md_5.bungee.api.ProxyServer; + +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +public class ErrorLogger extends Handler { + private static ErrorLogger instance; + + ErrorLogger(){ + ProxyServer.getInstance().getLogger().addHandler(this); + instance = this; + } + + static void stop(){ + ProxyServer.getInstance().getLogger().removeHandler(instance); + } + + @Override + public void publish(LogRecord record) { + if(record.getLevel() != Level.SEVERE) + return; + + StringBuilder stacktrace = new StringBuilder(record.getSourceClassName() + "\n" + record.getSourceMethodName()); + Throwable thrown = record.getThrown(); + while(thrown != null){ + stacktrace.append("\nCaused by").append(thrown.getMessage()); + + for(StackTraceElement ste : thrown.getStackTrace()) + stacktrace.append("\n").append(ste.toString()); + + thrown = thrown.getCause(); + } + + SWException.log("Bungee", record.getMessage(), stacktrace.toString()); + } + + @Override + public void flush() { + //ignored + } + + @Override + public void close() { + //ignored + } +} diff --git a/src/de/steamwar/bungeecore/commands/BugCommand.java b/src/de/steamwar/bungeecore/commands/BugCommand.java new file mode 100644 index 00000000..bc9c9f2b --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/BugCommand.java @@ -0,0 +1,24 @@ +package de.steamwar.bungeecore.commands; + +import de.steamwar.bungeecore.sql.SWException; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +public class BugCommand extends BasicCommand { + public BugCommand() { + super("bug", null); + } + + @Override + public void execute(CommandSender sender, String[] args) { + if(!(sender instanceof ProxiedPlayer)) + return; + + ProxiedPlayer player = (ProxiedPlayer) sender; + String server = player.getServer().getInfo().getName(); + String message = String.join(" ", args); + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + SWException.log(server, message, player.getName() + " " + user.getId()); + } +} diff --git a/src/de/steamwar/bungeecore/sql/SWException.java b/src/de/steamwar/bungeecore/sql/SWException.java new file mode 100644 index 00000000..153b1d86 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/SWException.java @@ -0,0 +1,9 @@ +package de.steamwar.bungeecore.sql; + +public class SWException { + private SWException(){} + + public static void log(String server, String message, String stacktrace){ + SQL.update("INSERT INTO Exception (server, meassage, stacktrace) VALUES (?, ?, ?)", server, message, stacktrace); + } +}