Use stackwalker for sout catcher

This commit is contained in:
William Blake Galbreath
2021-07-28 04:04:40 -05:00
parent b2056c256f
commit b37dab3be2
3 changed files with 12 additions and 102 deletions

View File

@@ -5,7 +5,7 @@ Subject: [PATCH] Redirect System.out calls to plugin loggers
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
index 4a3226b58ee3c11830dabba988b0665dc069559a..cb41bf68e8b24352d8bab6bfe43df4a75ef02c9a 100644
index 7b66b301b10158e9c715834b24bdfab1610f4a94..d6502173bebba1a1c62deea2e14b40d7942736f6 100644
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
@@ -178,8 +178,8 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
@@ -21,10 +21,10 @@ index 4a3226b58ee3c11830dabba988b0665dc069559a..cb41bf68e8b24352d8bab6bfe43df4a7
thread.setDaemon(true);
diff --git a/src/main/java/net/pl3x/purpur/PurpurPrintStream.java b/src/main/java/net/pl3x/purpur/PurpurPrintStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..17030b88d80bfdfc5790b3f63ea006756ef26d90
index 0000000000000000000000000000000000000000..42dfcb704dd3aeb1843db368054925ed1962971a
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/PurpurPrintStream.java
@@ -0,0 +1,70 @@
@@ -0,0 +1,65 @@
+/*
+ * MIT License
+ *
@@ -56,7 +56,6 @@ index 0000000000000000000000000000000000000000..17030b88d80bfdfc5790b3f63ea00675
+
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.util.Arrays;
+import java.util.function.BiConsumer;
+import java.util.logging.Logger;
+
@@ -67,6 +66,7 @@ index 0000000000000000000000000000000000000000..17030b88d80bfdfc5790b3f63ea00675
+ */
+
+public class PurpurPrintStream extends PrintStream {
+ private static final StackWalker STACKWALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
+ private final BiConsumer<Logger, String> consumer;
+
+ public PurpurPrintStream(@NotNull OutputStream out, BiConsumer<Logger, String> consumer) {
@@ -76,22 +76,17 @@ index 0000000000000000000000000000000000000000..17030b88d80bfdfc5790b3f63ea00675
+
+ @Override
+ public void println(String line) {
+ // Get the current stack trace and the calling method (index 2)
+ StackTraceElement element = Thread.currentThread().getStackTrace()[2];
+ try {
+ // Get the class name at that index and the JavaPlugin that "owns" it
+ JavaPlugin plugin = JavaPlugin.getProvidingPlugin(element.getClassName());
+ // Get the the JavaPlugin that "owns" the calling class
+ JavaPlugin plugin = JavaPlugin.getProvidingPlugin(STACKWALKER.getCallerClass());
+
+ if (plugin != null) {
+ // Instead of just printing the message, send it to the plugin's logger
+ consumer.accept(plugin.getLogger(), line);
+ return;
+ }
+ // Instead of just printing the message, send it to the plugin's logger
+ consumer.accept(plugin.getLogger(), line);
+ } catch (Throwable ignore) {
+ // If anything happens, the calling class doesn't exist, there is no JavaPlugin that "owns" the calling class, etc
+ // Just print out normally, with some added information
+ StackTraceElement element = Thread.currentThread().getStackTrace()[2];
+ consumer.accept(Bukkit.getLogger(), String.format("(%s:%d) %s", element.getClassName(), element.getLineNumber(), line));
+ }
+
+ // If anything happens, the calling class doesn't exist, there is no JavaPlugin that "owns" the calling class, etc
+ // Just print to Bukkit logger, with some added information
+ consumer.accept(Bukkit.getLogger(), String.format("(%s:%d) %s", element.getClassName(), element.getLineNumber(), line));
+ }
+}