From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: William Blake Galbreath Date: Mon, 28 Jun 2021 13:33:12 -0500 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 --- 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 */ // Paper end - System.setOut(IoBuilder.forLogger(logger).setLevel(Level.INFO).buildPrintStream()); - System.setErr(IoBuilder.forLogger(logger).setLevel(Level.WARN).buildPrintStream()); + System.setOut(new net.pl3x.purpur.PurpurPrintStream(IoBuilder.forLogger(logger).setLevel(Level.INFO).buildPrintStream(), java.util.logging.Logger::info)); // Purpur + System.setErr(new net.pl3x.purpur.PurpurPrintStream(IoBuilder.forLogger(logger).setLevel(Level.WARN).buildPrintStream(), java.util.logging.Logger::severe)); // Purpur // CraftBukkit end 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 --- /dev/null +++ b/src/main/java/net/pl3x/purpur/PurpurPrintStream.java @@ -0,0 +1,70 @@ +/* + * MIT License + * + * Copyright (c) 2020 Josh (Vicarious) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.pl3x.purpur; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; + +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.Arrays; +import java.util.function.BiConsumer; +import java.util.logging.Logger; + +/** + * Logic borrowed from SysoutCatcher + *

+ * https://www.spigotmc.org/resources/sysoutcatcher.79076/ + */ + +public class PurpurPrintStream extends PrintStream { + private final BiConsumer consumer; + + public PurpurPrintStream(@NotNull OutputStream out, BiConsumer consumer) { + super(out); + this.consumer = consumer; + } + + @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()); + + if (plugin != null) { + // Instead of just printing the message, send it to the plugin's logger + consumer.accept(plugin.getLogger(), line); + return; + } + } catch (Throwable ignore) { + } + + // 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)); + } +}