From e86873967594483784ed4c809fabbde8c36d87ee Mon Sep 17 00:00:00 2001 From: ichttt Date: Tue, 16 Apr 2019 23:08:08 +0200 Subject: [PATCH] Readd redirecting of stdout and stderr (#5678) --- .../fml/loading/FMLServiceProvider.java | 3 + .../fml/loading/TracingPrintStream.java | 115 ++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 src/fmllauncher/java/net/minecraftforge/fml/loading/TracingPrintStream.java diff --git a/src/fmllauncher/java/net/minecraftforge/fml/loading/FMLServiceProvider.java b/src/fmllauncher/java/net/minecraftforge/fml/loading/FMLServiceProvider.java index 176d377d64..5513b75d85 100644 --- a/src/fmllauncher/java/net/minecraftforge/fml/loading/FMLServiceProvider.java +++ b/src/fmllauncher/java/net/minecraftforge/fml/loading/FMLServiceProvider.java @@ -93,6 +93,9 @@ public class FMLServiceProvider implements ITransformationService @Override public void onLoad(IEnvironment environment, Set otherServices) throws IncompatibleEnvironmentException { + LOGGER.debug("Injecting tracing printstreams for STDOUT/STDERR."); + System.setOut(new TracingPrintStream(LogManager.getLogger("STDOUT"), System.out)); + System.setErr(new TracingPrintStream(LogManager.getLogger("STDERR"), System.err)); FMLLoader.onInitialLoad(environment, otherServices); } diff --git a/src/fmllauncher/java/net/minecraftforge/fml/loading/TracingPrintStream.java b/src/fmllauncher/java/net/minecraftforge/fml/loading/TracingPrintStream.java new file mode 100644 index 0000000000..087b8c62c6 --- /dev/null +++ b/src/fmllauncher/java/net/minecraftforge/fml/loading/TracingPrintStream.java @@ -0,0 +1,115 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2019. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.fml.loading; + +import org.apache.logging.log4j.Logger; + +import java.io.PrintStream; + +/** + * PrintStream which redirects it's output to a given logger. + * + */ +public class TracingPrintStream extends PrintStream +{ + + private static final int BASE_DEPTH = 4; + private final Logger logger; + + public TracingPrintStream(Logger logger, PrintStream original) + { + super(original); + this.logger = logger; + } + + private void log(String s) + { + logger.info("{}{}", getPrefix(), s); + } + + private static String getPrefix() + { + StackTraceElement[] elems = Thread.currentThread().getStackTrace(); + StackTraceElement elem = elems[Math.min(BASE_DEPTH, elems.length - 1)]; // The caller is always at BASE_DEPTH, including this call. + if (elem.getClassName().startsWith("kotlin.io.")) + { + elem = elems[Math.min(BASE_DEPTH + 2, elems.length - 1)]; // Kotlins IoPackage masks origins 2 deeper in the stack. + } + else if (elem.getClassName().startsWith("java.lang.Throwable")) + { + elem = elems[Math.min(BASE_DEPTH + 4, elems.length - 1)]; + } + return "[" + elem.getClassName() + ":" + elem.getMethodName() + ":" + elem.getLineNumber() + "]: "; + } + + @Override + public void println(Object o) + { + log(String.valueOf(o)); + } + + @Override + public void println(String s) + { + log(s); + } + + @Override + public void println(boolean x) + { + log(String.valueOf(x)); + } + + @Override + public void println(char x) + { + log(String.valueOf(x)); + } + + @Override + public void println(int x) + { + log(String.valueOf(x)); + } + + @Override + public void println(long x) + { + log(String.valueOf(x)); + } + + @Override + public void println(float x) + { + log(String.valueOf(x)); + } + + @Override + public void println(double x) + { + log(String.valueOf(x)); + } + + @Override + public void println(char[] x) + { + log(String.valueOf(x)); + } +}