2021-08-02 09:07:48 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: underscore11code <minecrafter11mrt@gmail.com>
|
|
|
|
Date: Fri, 23 Jul 2021 23:01:42 -0700
|
2021-08-21 17:01:42 +02:00
|
|
|
Subject: [PATCH] Add System.out/err catcher
|
2021-08-02 09:07:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/logging/SysoutCatcher.java b/src/main/java/io/papermc/paper/logging/SysoutCatcher.java
|
|
|
|
new file mode 100644
|
2023-04-16 18:49:13 +02:00
|
|
|
index 0000000000000000000000000000000000000000..a8e813ca89b033f061e695288b3383bdcf128531
|
2021-08-02 09:07:48 +02:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/io/papermc/paper/logging/SysoutCatcher.java
|
2021-08-21 17:01:42 +02:00
|
|
|
@@ -0,0 +1,94 @@
|
2021-08-02 09:07:48 +02:00
|
|
|
+package io.papermc.paper.logging;
|
|
|
|
+
|
|
|
|
+import org.bukkit.Bukkit;
|
|
|
|
+import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
|
+import org.jetbrains.annotations.Nullable;
|
|
|
|
+
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.io.PrintStream;
|
2021-08-21 17:01:42 +02:00
|
|
|
+import java.util.Objects;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+import java.util.concurrent.ConcurrentMap;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
2021-08-02 09:07:48 +02:00
|
|
|
+import java.util.logging.Level;
|
|
|
|
+
|
|
|
|
+public final class SysoutCatcher {
|
|
|
|
+ private static final boolean SUPPRESS_NAGS = Boolean.getBoolean("io.papermc.paper.suppress.sout.nags");
|
2021-08-21 17:01:42 +02:00
|
|
|
+ // Nanoseconds between nag at most; if interval is caught first, this is reset.
|
|
|
|
+ // <= 0 for disabling.
|
|
|
|
+ private static final long NAG_TIMEOUT = TimeUnit.MILLISECONDS.toNanos(
|
|
|
|
+ Long.getLong("io.papermc.paper.sout.nags.timeout", TimeUnit.MINUTES.toMillis(5L)));
|
|
|
|
+ // Count since last nag; if timeout is first, this is reset.
|
|
|
|
+ // <= 0 for disabling.
|
|
|
|
+ private static final long NAG_INTERVAL = Long.getLong("io.papermc.paper.sout.nags.interval", 200L);
|
|
|
|
+
|
|
|
|
+ // We don't particularly care about how correct this is at any given moment; let's do it on a best attempt basis.
|
|
|
|
+ // The records are also pretty small, so let's just go for a size of 64 to start...
|
|
|
|
+ //
|
|
|
|
+ // Content: Plugin name => nag object
|
|
|
|
+ // Why plugin name?: This doesn't store a reference to the plugin; keeps the reload ability.
|
|
|
|
+ // Why not clean on reload?: Effort.
|
|
|
|
+ private final ConcurrentMap<String, PluginNag> nagRecords = new ConcurrentHashMap<>(64);
|
2021-08-02 09:07:48 +02:00
|
|
|
+
|
|
|
|
+ public SysoutCatcher() {
|
|
|
|
+ System.setOut(new WrappedOutStream(System.out, Level.INFO, "[STDOUT] "));
|
|
|
|
+ System.setErr(new WrappedOutStream(System.err, Level.SEVERE, "[STDERR] "));
|
|
|
|
+ }
|
|
|
|
+
|
2021-08-21 17:01:42 +02:00
|
|
|
+ private final class WrappedOutStream extends PrintStream {
|
2021-08-02 09:07:48 +02:00
|
|
|
+ private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
|
|
|
|
+ private final Level level;
|
|
|
|
+ private final String prefix;
|
|
|
|
+
|
|
|
|
+ public WrappedOutStream(@NotNull final OutputStream out, final Level level, final String prefix) {
|
|
|
|
+ super(out);
|
|
|
|
+ this.level = level;
|
|
|
|
+ this.prefix = prefix;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void println(@Nullable final String line) {
|
|
|
|
+ final Class<?> clazz = STACK_WALKER.getCallerClass();
|
|
|
|
+ try {
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ if (SysoutCatcher.SUPPRESS_NAGS) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
2021-08-21 17:01:42 +02:00
|
|
|
+ if (SysoutCatcher.NAG_INTERVAL > 0 || SysoutCatcher.NAG_TIMEOUT > 0) {
|
|
|
|
+ final PluginNag nagRecord = SysoutCatcher.this.nagRecords.computeIfAbsent(plugin.getName(), k -> new PluginNag());
|
|
|
|
+ final boolean hasTimePassed = SysoutCatcher.NAG_TIMEOUT > 0
|
|
|
|
+ && (nagRecord.lastNagTimestamp == Long.MIN_VALUE
|
|
|
|
+ || nagRecord.lastNagTimestamp + SysoutCatcher.NAG_TIMEOUT <= System.nanoTime());
|
|
|
|
+ final boolean hasMessagesPassed = SysoutCatcher.NAG_INTERVAL > 0
|
|
|
|
+ && (nagRecord.messagesSinceNag == Long.MIN_VALUE
|
|
|
|
+ || ++nagRecord.messagesSinceNag >= SysoutCatcher.NAG_INTERVAL);
|
|
|
|
+ if (!hasMessagesPassed && !hasTimePassed) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ nagRecord.lastNagTimestamp = System.nanoTime();
|
|
|
|
+ nagRecord.messagesSinceNag = 0;
|
|
|
|
+ }
|
2021-08-02 09:07:48 +02:00
|
|
|
+ Bukkit.getLogger().warning(
|
|
|
|
+ String.format("Nag author(s): '%s' of '%s' about their usage of System.out/err.print. "
|
|
|
|
+ + "Please use your plugin's logger instead (JavaPlugin#getLogger).",
|
2023-04-16 18:49:13 +02:00
|
|
|
+ plugin.getPluginMeta().getAuthors(),
|
|
|
|
+ plugin.getPluginMeta().getDisplayName())
|
2021-08-02 09:07:48 +02:00
|
|
|
+ );
|
2021-08-04 05:04:12 +02:00
|
|
|
+ } catch (final IllegalArgumentException | IllegalStateException e) {
|
2021-08-02 09:07:48 +02:00
|
|
|
+ // 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
|
2021-09-14 19:23:56 +02:00
|
|
|
+ Bukkit.getLogger().log(this.level, String.format("%s[%s] %s", this.prefix, clazz.getName(), line));
|
2021-08-02 09:07:48 +02:00
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
2021-08-21 17:01:42 +02:00
|
|
|
+
|
|
|
|
+ private static class PluginNag {
|
|
|
|
+ private long lastNagTimestamp = Long.MIN_VALUE;
|
|
|
|
+ private long messagesSinceNag = Long.MIN_VALUE;
|
|
|
|
+ }
|
2021-08-02 09:07:48 +02:00
|
|
|
+}
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
2024-01-19 17:54:05 +01:00
|
|
|
index b28212bbf7678d73282d160bfeb0d246e93a8492..f4b1d6ed1e1065e6be175b7b68e29517ebfa2da8 100644
|
2021-08-02 09:07:48 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
Updated Upstream (Bukkit/CraftBukkit) (#10034)
Upstream has released updates that appear to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing
Bukkit Changes:
f29cb801 Separate checkstyle-suppressions file is not required
86f99bbe SPIGOT-7540, PR-946: Add ServerTickManager API
d4119585 SPIGOT-6903, PR-945: Add BlockData#getMapColor
b7a2ed41 SPIGOT-7530, PR-947: Add Player#removeResourcePack
9dd56255 SPIGOT-7527, PR-944: Add WindCharge#explode()
994a6163 Attempt upgrade of resolver libraries
CraftBukkit Changes:
b3b43a6ad Add Checkstyle check for unused imports
13fb3358e SPIGOT-7544: Scoreboard#getEntries() doesn't get entries but class names
3dda99c06 SPIGOT-7540, PR-1312: Add ServerTickManager API
2ab4508c0 SPIGOT-6903, PR-1311: Add BlockData#getMapColor
1dbdbbed4 PR-1238: Remove unnecessary sign ticking
659728d2a MC-264285, SPIGOT-7439, PR-1237: Fix unbreakable flint and steel is completely consumed while igniting creeper
e37e29ce0 Increase outdated build delay
c00438b39 SPIGOT-7530, PR-1313: Add Player#removeResourcePack
492dd80ce SPIGOT-7527, PR-1310: Add WindCharge#explode()
e11fbb9d7 Upgrade MySQL driver
9f3a0bd2a Attempt upgrade of resolver libraries
60d16d7ca PR-1306: Centralize Bukkit and Minecraft entity conversion
Spigot Changes:
06d602e7 Rebuild patches
2023-12-17 03:09:28 +01:00
|
|
|
@@ -303,6 +303,7 @@ public final class CraftServer implements Server {
|
2021-08-02 09:07:48 +02:00
|
|
|
public int reloadCount;
|
|
|
|
private final io.papermc.paper.datapack.PaperDatapackManager datapackManager; // Paper
|
|
|
|
public static Exception excessiveVelEx; // Paper - Velocity warnings
|
2022-02-12 14:20:33 +01:00
|
|
|
+ private final io.papermc.paper.logging.SysoutCatcher sysoutCatcher = new io.papermc.paper.logging.SysoutCatcher(); // Paper
|
2021-08-02 09:07:48 +02:00
|
|
|
|
|
|
|
static {
|
|
|
|
ConfigurationSerialization.registerClass(CraftOfflinePlayer.class);
|