From 5b40c4aabb7c44fbaa69a28cfff267b511a493bc Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 17 Mar 2023 15:41:39 +0100 Subject: [PATCH] Try a cleaner stdout/stderr -> logger redirector --- .../fr/pandacube/lib/cli/log/CLILogger.java | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/pandalib-cli/src/main/java/fr/pandacube/lib/cli/log/CLILogger.java b/pandalib-cli/src/main/java/fr/pandacube/lib/cli/log/CLILogger.java index f5e76ea..7c9edb6 100644 --- a/pandalib-cli/src/main/java/fr/pandacube/lib/cli/log/CLILogger.java +++ b/pandalib-cli/src/main/java/fr/pandacube/lib/cli/log/CLILogger.java @@ -1,15 +1,18 @@ package fr.pandacube.lib.cli.log; -import java.io.PrintStream; -import java.util.logging.Handler; -import java.util.logging.Level; -import java.util.logging.Logger; - import fr.pandacube.lib.cli.CLI; import fr.pandacube.lib.util.Log; +import fr.pandacube.lib.util.ThrowableUtil; import net.md_5.bungee.log.ColouredWriter; import net.md_5.bungee.log.ConciseFormatter; -import net.md_5.bungee.log.LoggingOutputStream; + +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.io.PrintStream; +import java.util.Scanner; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Initializer for the logging system of a CLI application. @@ -38,12 +41,33 @@ public class CLILogger { fileHandler.setFormatter(new ConciseFormatter(false)); logger.addHandler(fileHandler); - System.setErr(new PrintStream(new LoggingOutputStream(logger, Level.SEVERE), true)); - System.setOut(new PrintStream(new LoggingOutputStream(logger, Level.INFO), true)); + System.setErr(newRedirector(logger, Level.SEVERE)); + System.setOut(newRedirector(logger, Level.INFO)); Log.setLogger(logger); } return logger; } + + + + + private static PrintStream newRedirector(Logger logger, Level level) { + PipedOutputStream pos = new PipedOutputStream(); + PrintStream ps = new PrintStream(pos); + PipedInputStream pis = new PipedInputStream(); + ThrowableUtil.wrapEx(() -> pos.connect(pis)); + Scanner s = new Scanner(pis); + + Thread t = new Thread(() -> { + while(s.hasNextLine()) { + logger.logp(level, "", "", s.nextLine()); + } + s.close(); + }, "Logging Redirector Thread (" + level + ")"); + t.setDaemon(true); + t.start(); + return ps; + } }