From 1af6632a29dfd7f7b92b8f97c73854eb46c78df0 Mon Sep 17 00:00:00 2001 From: Tobias Hotz Date: Tue, 25 Aug 2020 16:17:22 +0200 Subject: [PATCH] Auto-detect ansi support for log4j2 move to fml.loading subpackage Add license header --- .../fml/loading/log4j/ForgeHighlight.java | 73 +++++++++++++++++++ src/fmllauncher/resources/log4j2.xml | 8 +- 2 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/fmllauncher/java/net/minecraftforge/fml/loading/log4j/ForgeHighlight.java diff --git a/src/fmllauncher/java/net/minecraftforge/fml/loading/log4j/ForgeHighlight.java b/src/fmllauncher/java/net/minecraftforge/fml/loading/log4j/ForgeHighlight.java new file mode 100644 index 0000000000..0f50882da8 --- /dev/null +++ b/src/fmllauncher/java/net/minecraftforge/fml/loading/log4j/ForgeHighlight.java @@ -0,0 +1,73 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2020. + * + * 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.log4j; + +import net.minecrell.terminalconsole.HighlightErrorConverter; +import net.minecrell.terminalconsole.TerminalConsoleAppender; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.pattern.ConverterKeys; +import org.apache.logging.log4j.core.pattern.HighlightConverter; +import org.apache.logging.log4j.core.pattern.PatternConverter; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.util.PerformanceSensitive; + +import javax.annotation.Nullable; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * A wrapper for {@link HighlightConverter} that auto-disables ANSI when the terminal doesn't support it. + * Ansi support is determined by TerminalConsoleAppender + */ +@Plugin(name = "highlightForge", category = PatternConverter.CATEGORY) +@ConverterKeys("highlightForge") +@PerformanceSensitive("allocation") +public class ForgeHighlight { + protected static final Logger LOGGER = StatusLogger.getLogger(); + + /** + * Gets a new instance of the {@link HighlightErrorConverter} with the + * specified options. + * + * @param config The current configuration + * @param options The pattern options + * @return The new instance + */ + public static @Nullable HighlightConverter newInstance(Configuration config, String[] options) { + try { + Method method = TerminalConsoleAppender.class.getDeclaredMethod("initializeTerminal"); + method.setAccessible(true); + method.invoke(null); + } catch (ReflectiveOperationException e) { + LOGGER.warn("Failed to invoke initializeTerminal on TCA", e); + } + if (!TerminalConsoleAppender.isAnsiSupported() && Arrays.stream(options).noneMatch(s -> s.equals("disableAnsi=true"))) { + List optionList = new ArrayList<>(); + optionList.add(options[0]); + optionList.add("disableAnsi=true"); + options = optionList.toArray(new String[0]); + } + return HighlightConverter.newInstance(config, options); + } +} diff --git a/src/fmllauncher/resources/log4j2.xml b/src/fmllauncher/resources/log4j2.xml index 2f417be965..cf06a2029d 100644 --- a/src/fmllauncher/resources/log4j2.xml +++ b/src/fmllauncher/resources/log4j2.xml @@ -1,5 +1,5 @@ - + @@ -20,10 +20,10 @@ - + - - + +