From c871ee3a57f907ef5d3b2b99d79230201adba42e Mon Sep 17 00:00:00 2001 From: Progwml6 Date: Sun, 23 Mar 2014 23:27:25 -0400 Subject: [PATCH] add in way of redirecting all sysout & syserr to loggers by class name... will work on doing via modID, and redirecting java.util loggers this week --- src/mantle/Mantle.java | 7 +++++ src/mantle/debug/LogIdentifier.java | 46 +++++++++++++++++++++++++++++ src/mantle/lib/CoreConfig.java | 6 +++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/mantle/debug/LogIdentifier.java diff --git a/src/mantle/Mantle.java b/src/mantle/Mantle.java index 6389282..fb66e17 100644 --- a/src/mantle/Mantle.java +++ b/src/mantle/Mantle.java @@ -11,6 +11,7 @@ import mantle.books.BookData; import mantle.books.BookDataStore; import mantle.common.IDDumps; import mantle.common.MProxyCommon; +import mantle.debug.LogIdentifier; import mantle.items.Manual; import mantle.lib.CoreConfig; import mantle.lib.CoreRepo; @@ -43,6 +44,7 @@ public class Mantle @SidedProxy(clientSide = "mantle.client.MProxyClient", serverSide = "mantle.common.MProxyCommon") public static MProxyCommon proxy; public static Manual mantleBook; + public static LogIdentifier logID; /** * Method Loaded once FML loads mod containers @@ -69,6 +71,11 @@ public class Mantle logger.info("Mantle (" + modVersion + ") -- Preparing for launch."); logger.info("Entering preinitialization phase."); CoreConfig.loadConfiguration(evt.getModConfigurationDirectory()); + + if(CoreConfig.identifyLogs){ + logID = new LogIdentifier(); + logID.preinit(); + } NetworkRegistry.INSTANCE.registerGuiHandler(instance, proxy); mantleBook = (Manual) new Manual().setUnlocalizedName("mantle.manual"); diff --git a/src/mantle/debug/LogIdentifier.java b/src/mantle/debug/LogIdentifier.java new file mode 100644 index 0000000..f7f0c10 --- /dev/null +++ b/src/mantle/debug/LogIdentifier.java @@ -0,0 +1,46 @@ +package mantle.debug; + +import java.io.PrintStream; +import static mantle.lib.CoreRepo.logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class LogIdentifier +{ + private Logger outLogger = LogManager.getLogger("Mantle-STDOUT"); + private Logger errLogger = LogManager.getLogger("Mantle-STDERR"); + + public void preinit () + { + logger.info("Inserting TracingPrintStream..."); + System.setOut(new TracingPrintStream(outLogger, "STDOUT", System.out)); + System.setErr(new TracingPrintStream(errLogger, "STDERR", System.err)); + logger.info("TracingPrintStream inserted on STDOUT/STDERR. These will now be redirected to the Mantle-STDOUT/Mantle-STDERR loggers."); + //logger.info("Forcing Java.util.Logger logging into log4j2"); + //JavaLoggingRedirector.activate(); + // logger.info("Successfully redirected java.util.logging"); + } + +} + +class TracingPrintStream extends PrintStream +{ + static Logger l; + + public TracingPrintStream(Logger l, String Stream, PrintStream original) + { + super(original); + } + + @Override + public void println (String x) + { + StackTraceElement[] stack = Thread.currentThread().getStackTrace(); + // stack(2) is calling object. + String name = stack[2].getClassName(); + l.info("[" + name + "]: " + x); + } + +} + + diff --git a/src/mantle/lib/CoreConfig.java b/src/mantle/lib/CoreConfig.java index ac719ef..6440452 100644 --- a/src/mantle/lib/CoreConfig.java +++ b/src/mantle/lib/CoreConfig.java @@ -49,11 +49,14 @@ public class CoreConfig debug_enableConsole = config.get("DebugHelpers", "enableConsole", debug_enableConsole).getBoolean(debug_enableConsole); debug_enableChat = config.get("DebugHelpers", "enableChat", debug_enableChat).getBoolean(debug_enableChat); - //check for debugging overrides in system environment + dumpBiomeIDs = config.get("DebugHelpers", "Dump BIOME ID's in log", dumpBiomeIDs).getBoolean(dumpBiomeIDs); dumpPotionIDs = config.get("DebugHelpers", "Dump POTION ID's in log", dumpPotionIDs).getBoolean(dumpPotionIDs); dumpEnchantIDs = config.get("DebugHelpers", "Dump ENCHANT ID's in log", dumpEnchantIDs).getBoolean(dumpEnchantIDs); + identifyLogs = config.get("DebugHelpers", "force all non identified logging entries through loggers", identifyLogs).getBoolean(identifyLogs); + + //check for debugging overrides in system environment checkSysOverrides(); config.save(); logger.info("Configuration load completed."); @@ -95,6 +98,7 @@ public class CoreConfig public static boolean dumpBiomeIDs = false; public static boolean dumpPotionIDs = false; public static boolean dumpEnchantIDs = false; + public static boolean identifyLogs = false; }