Enhance SysoutCatcher

This commit is contained in:
BillyGalbreath
2022-01-02 06:25:40 -06:00
parent dafcf334cc
commit f652d4d4ee
2 changed files with 122 additions and 105 deletions

View File

@@ -0,0 +1,122 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Mon, 28 Jun 2021 13:33:12 -0500
Subject: [PATCH] Enhance SysoutCatcher
diff --git a/src/main/java/io/papermc/paper/logging/SysoutCatcher.java b/src/main/java/io/papermc/paper/logging/SysoutCatcher.java
index 76d0d00cd6742991e3f3ec827a75ee87d856b6c9..784076cd05e161a3b496bfb2e2daa82f738b7ee6 100644
--- a/src/main/java/io/papermc/paper/logging/SysoutCatcher.java
+++ b/src/main/java/io/papermc/paper/logging/SysoutCatcher.java
@@ -1,5 +1,8 @@
package io.papermc.paper.logging;
+import net.minecraft.server.MinecraftServer;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
@@ -11,9 +14,13 @@ import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
+import java.util.logging.Handler;
import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
public final class SysoutCatcher {
+ private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); // Purpur - moved from below
private static final boolean SUPPRESS_NAGS = Boolean.getBoolean("io.papermc.paper.suppress.sout.nags");
// Nanoseconds between nag at most; if interval is caught first, this is reset.
// <= 0 for disabling.
@@ -34,10 +41,75 @@ public final class SysoutCatcher {
public SysoutCatcher() {
System.setOut(new WrappedOutStream(System.out, Level.INFO, "[STDOUT] "));
System.setErr(new WrappedOutStream(System.err, Level.SEVERE, "[STDERR] "));
+
+ // Purpur start
+ Logger.getLogger("Minecraft").setUseParentHandlers(false);
+ Logger.getLogger("Minecraft").addHandler(new Handler() {
+ @Override
+ public void publish(LogRecord record) {
+ if (record != null) {
+ Logger logger;
+ try {
+ final Class<?> clazz = STACK_WALKER.walk(stream ->
+ stream.map(StackWalker.StackFrame::getDeclaringClass)
+ .skip(2)
+ .filter(e -> Logger.class != e)
+ .findFirst()
+ ).orElse(null);
+ logger = JavaPlugin.getProvidingPlugin(clazz).getLogger();
+ } catch (Throwable e) {
+ logger = Logger.getLogger("");
+ }
+ logger.log(record.getLevel(), record.getMessage(), record.getParameters());
+ }
+ }
+
+ @Override
+ public void flush() {
+ }
+
+ @Override
+ public void close() throws SecurityException {
+ }
+ });
+ ((org.apache.logging.log4j.core.Logger) MinecraftServer.LOGGER).addFilter(new AbstractFilter() {
+ @Override
+ public Result filter(LogEvent event) {
+ try {
+ final Class<?> clazz = STACK_WALKER.walk(stream ->
+ stream.map(StackWalker.StackFrame::getDeclaringClass)
+ .skip(2)
+ .filter(e -> !e.getPackageName().contains("org.apache.logging.log4j"))
+ .findFirst()
+ ).orElse(null);
+ final Logger logger = JavaPlugin.getProvidingPlugin(clazz).getLogger();
+ Level level = null;
+ // https://github.com/apache/logging-log4j2/blob/5370298f3a549a3502a585e2de997a97101e651a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/DefaultLevelConverter.java#L65-L75
+ if (event.getLevel() == org.apache.logging.log4j.Level.ALL) level = Level.ALL;
+ else if (event.getLevel() == org.apache.logging.log4j.Level.DEBUG) level = Level.FINE;
+ else if (event.getLevel() == org.apache.logging.log4j.Level.ERROR) level = Level.SEVERE;
+ else if (event.getLevel() == org.apache.logging.log4j.Level.FATAL) level = Level.SEVERE;
+ else if (event.getLevel() == org.apache.logging.log4j.Level.INFO) level = Level.INFO;
+ else if (event.getLevel() == org.apache.logging.log4j.Level.OFF) level = Level.OFF;
+ else if (event.getLevel() == org.apache.logging.log4j.Level.TRACE) level = Level.FINER;
+ else if (event.getLevel() == org.apache.logging.log4j.Level.WARN) level = Level.WARNING;
+ logger.log(level, event.getMessage().getFormattedMessage());
+ return Result.DENY;
+ } catch (Throwable ignore) {
+ return Result.NEUTRAL;
+ }
+ }
+
+ @Override
+ protected boolean equalsImpl(Object obj) {
+ return super.equalsImpl(obj);
+ }
+ });
+ // Purpur end
}
private final class WrappedOutStream extends PrintStream {
- private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
+ //private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); // Purpur - move up
private final Level level;
private final String prefix;
@@ -54,9 +126,9 @@ public final class SysoutCatcher {
final JavaPlugin plugin = JavaPlugin.getProvidingPlugin(clazz);
// Instead of just printing the message, send it to the plugin's logger
- plugin.getLogger().log(this.level, this.prefix + line);
+ plugin.getLogger().log(this.level, /*this.prefix +*/ line); // Purpur - prefix not needed
- if (SysoutCatcher.SUPPRESS_NAGS) {
+ if (true || SysoutCatcher.SUPPRESS_NAGS) { // Purpur - nagging is annoying
return;
}
if (SysoutCatcher.NAG_INTERVAL > 0 || SysoutCatcher.NAG_TIMEOUT > 0) {

View File

@@ -1,105 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
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 455776981274e64724376a3182b60784b56db9cf..cd859eec6b0bee44bce83614e55709381baf5e6b 100644
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
@@ -176,8 +176,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 org.purpurmc.purpur.PurpurPrintStream(IoBuilder.forLogger(logger).setLevel(Level.INFO).buildPrintStream(), java.util.logging.Logger::info)); // Purpur
+ System.setErr(new org.purpurmc.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/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index f0f3e9d0645e882da7d71e0a1051da30f1e6a41f..71ba48fee88aad08883ae65e0b6eb4a674343f32 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -294,7 +294,7 @@ public final class CraftServer implements Server {
public int reloadCount;
private final io.papermc.paper.datapack.PaperDatapackManager datapackManager; // Paper
public static Exception excessiveVelEx; // Paper - Velocity warnings
- private final SysoutCatcher sysoutCatcher = new SysoutCatcher(); // Paper
+ //private final SysoutCatcher sysoutCatcher = new SysoutCatcher(); // Paper // Purpur - we have our own implementation that doesnt nag or prefix output
static {
ConfigurationSerialization.registerClass(CraftOfflinePlayer.class);
diff --git a/src/main/java/org/purpurmc/purpur/PurpurPrintStream.java b/src/main/java/org/purpurmc/purpur/PurpurPrintStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..f39c451f8877c7cb66fd4761006176107eceeac6
--- /dev/null
+++ b/src/main/java/org/purpurmc/purpur/PurpurPrintStream.java
@@ -0,0 +1,65 @@
+/*
+ * 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 org.purpurmc.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.function.BiConsumer;
+import java.util.logging.Logger;
+
+/**
+ * Logic borrowed from SysoutCatcher
+ * <p>
+ * https://www.spigotmc.org/resources/sysoutcatcher.79076/
+ */
+
+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) {
+ super(out);
+ this.consumer = consumer;
+ }
+
+ @Override
+ public void println(String line) {
+ try {
+ // Get the JavaPlugin that "owns" the calling class
+ JavaPlugin plugin = JavaPlugin.getProvidingPlugin(STACKWALKER.getCallerClass());
+
+ // 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));
+ }
+ }
+}