From fdc0a9e1a1b6ace98aca2e5d717bd07e4cceb48f Mon Sep 17 00:00:00 2001 From: Raven Szewczyk Date: Sat, 21 Jan 2023 13:40:08 +0000 Subject: [PATCH] Shield logs against closing --- .../mods/fml/common/TracingPrintStream.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/forgePatches/java/cpw/mods/fml/common/TracingPrintStream.java diff --git a/src/forgePatches/java/cpw/mods/fml/common/TracingPrintStream.java b/src/forgePatches/java/cpw/mods/fml/common/TracingPrintStream.java new file mode 100644 index 0000000..b838914 --- /dev/null +++ b/src/forgePatches/java/cpw/mods/fml/common/TracingPrintStream.java @@ -0,0 +1,55 @@ +/* + * Forge Mod Loader + * Copyright (c) 2012-2013 cpw. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v2.1 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + */ + +package cpw.mods.fml.common; + +import java.io.PrintStream; +import org.apache.logging.log4j.Logger; + +/** + * PrintStream which redirects it's output to a given logger. + * + * @author Arkan + */ +public class TracingPrintStream extends PrintStream { + + private Logger logger; + private int BASE_DEPTH = 3; + + public TracingPrintStream(Logger logger, PrintStream original) { + super(original); + this.logger = logger; + } + + @Override + public void println(Object o) { + logger.info(getPrefix() + o); + } + + @Override + public void println(String s) { + logger.info(getPrefix() + s); + } + + private String getPrefix() { + StackTraceElement[] elems = Thread.currentThread().getStackTrace(); + StackTraceElement elem = elems[BASE_DEPTH]; // The caller is always at BASE_DEPTH, including this call. + if (elem.getClassName().startsWith("kotlin.io.")) { + elem = elems[BASE_DEPTH + 2]; // Kotlins IoPackage masks origins 2 deeper in the stack. + } + return "[" + elem.getClassName() + ":" + elem.getMethodName() + ":" + elem.getLineNumber() + "]: "; + } + + @Override + public void close() { + // no-op + println("Something tried to close the main output stream"); + new Throwable().printStackTrace(); + } +}