From ca0e8c0299069053f942cb7f37d29c50ca138740 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sun, 17 Feb 2019 18:14:19 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D0=B0?= =?UTF-8?q?=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B3=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launchserver/LaunchServer.java | 4 +- .../command/handler/JLineCommandHandler.java | 2 +- .../response/admin/ExecCommandResponse.java | 2 +- .../json/admin/AddLogListenerResponse.java | 3 +- .../ru/gravit/utils/helper/LogHelper.java | 104 +++++++++++++++--- 5 files changed, 96 insertions(+), 19 deletions(-) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index 47e3cdbe..df4cb8c7 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -576,9 +576,9 @@ private void generateConfigIfNotExists() throws IOException { newConfig.isWarningMissArchJava = true; // Set server address - LogHelper.println("LaunchServer address: "); + System.out.println("LaunchServer address: "); newConfig.setAddress(commandHandler.readLine()); - LogHelper.println("LaunchServer projectName: "); + System.out.println("LaunchServer projectName: "); newConfig.setProjectName(commandHandler.readLine()); // Write LaunchServer config diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/JLineCommandHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/JLineCommandHandler.java index 7455b026..039afe3d 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/JLineCommandHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/JLineCommandHandler.java @@ -32,7 +32,7 @@ public JLineCommandHandler(LaunchServer server) throws IOException { // Replace writer LogHelper.removeStdOutput(); - LogHelper.addOutput(new JLineOutput()); + LogHelper.addOutput(new JLineOutput(), LogHelper.OutputTypes.JANSI); } @Override diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/admin/ExecCommandResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/admin/ExecCommandResponse.java index 2e633828..e47be8ba 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/response/admin/ExecCommandResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/admin/ExecCommandResponse.java @@ -30,7 +30,7 @@ public void reply() throws Exception { LogHelper.error(e); } }; - LogHelper.addOutput(loutput); + LogHelper.addOutput(loutput, LogHelper.OutputTypes.PLAIN); try { server.commandHandler.eval(cmd, false); output.writeBoolean(false); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/admin/AddLogListenerResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/admin/AddLogListenerResponse.java index 9576f484..f507a7e8 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/admin/AddLogListenerResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/admin/AddLogListenerResponse.java @@ -9,6 +9,7 @@ import ru.gravit.utils.helper.LogHelper; public class AddLogListenerResponse implements JsonResponseInterface { + public LogHelper.OutputTypes outputType = LogHelper.OutputTypes.PLAIN; @Override public String getType() { return "addLogListener"; @@ -43,7 +44,7 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client } }; client.logOutput = output; - LogHelper.addOutput(output); + LogHelper.addOutput(output, outputType); } } } diff --git a/libLauncher/src/main/java/ru/gravit/utils/helper/LogHelper.java b/libLauncher/src/main/java/ru/gravit/utils/helper/LogHelper.java index 4e76fa66..e86530ad 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/helper/LogHelper.java +++ b/libLauncher/src/main/java/ru/gravit/utils/helper/LogHelper.java @@ -35,21 +35,39 @@ public final class LogHelper { private static final AtomicBoolean DEBUG_ENABLED = new AtomicBoolean(Boolean.getBoolean(DEBUG_PROPERTY)); private static final AtomicBoolean STACKTRACE_ENABLED = new AtomicBoolean(Boolean.getBoolean(STACKTRACE_PROPERTY)); private static final AtomicBoolean DEV_ENABLED = new AtomicBoolean(Boolean.getBoolean(DEV_PROPERTY)); - private static final Set OUTPUTS = Collections.newSetFromMap(new ConcurrentHashMap<>(2)); + public static class OutputEnity + { + public Output output; + public OutputTypes type; + + public OutputEnity(Output output, OutputTypes type) { + this.output = output; + this.type = type; + } + } + public enum OutputTypes + { + PLAIN, JANSI, HTML + } + private static final Set OUTPUTS = Collections.newSetFromMap(new ConcurrentHashMap<>(2)); private static final Output STD_OUTPUT; private LogHelper() { } @LauncherAPI - public static void addOutput(Output output) { + public static void addOutput(OutputEnity output) { OUTPUTS.add(Objects.requireNonNull(output, "output")); } + @LauncherAPI + public static void addOutput(Output output, OutputTypes type) { + OUTPUTS.add(new OutputEnity(Objects.requireNonNull(output, "output"),type)); + } @LauncherAPI public static void addOutput(Path file) throws IOException { if (JANSI) { - addOutput(new JAnsiOutput(IOHelper.newOutput(file, true))); + addOutput(new JAnsiOutput(IOHelper.newOutput(file, true)),OutputTypes.JANSI); } else { addOutput(IOHelper.newWriter(file, true)); } @@ -57,7 +75,7 @@ public static void addOutput(Path file) throws IOException { @LauncherAPI public static void addOutput(Writer writer) { - addOutput(new WriterOutput(writer)); + addOutput(new WriterOutput(writer), OutputTypes.PLAIN); } @LauncherAPI @@ -142,24 +160,82 @@ public static void setDevEnabled(boolean stacktraceEnabled) { @LauncherAPI public static void log(Level level, String message, boolean sub) { String dateTime = DATE_TIME_FORMATTER.format(LocalDateTime.now()); - println(JANSI ? ansiFormatLog(level, dateTime, message, sub) : - formatLog(level, message, dateTime, sub)); + String jansiString = null, plainString = null; + for (OutputEnity output : OUTPUTS) { + if(output.type == OutputTypes.JANSI && JANSI) + { + if(jansiString != null){ + output.output.println(jansiString); + continue; + } + + jansiString = ansiFormatLog(level, dateTime, message, sub); + output.output.println(jansiString); + } + else + { + if(plainString != null){ + output.output.println(plainString); + continue; + } + + plainString = formatLog(level, message, dateTime, sub); + output.output.println(plainString); + } + } } @LauncherAPI public static void printVersion(String product) { - println(JANSI ? ansiFormatVersion(product) : formatVersion(product)); + String jansiString = null, plainString = null; + for (OutputEnity output : OUTPUTS) { + if(output.type == OutputTypes.JANSI && JANSI) + { + if(jansiString != null){ + output.output.println(jansiString); + continue; + } + + jansiString = ansiFormatVersion(product); + output.output.println(jansiString); + } + else + { + if(plainString != null){ + output.output.println(plainString); + continue; + } + + plainString = formatVersion(product); + output.output.println(plainString); + } + } } @LauncherAPI public static void printLicense(String product) { - println(JANSI ? ansiFormatLicense(product) : formatLicense(product)); - } + String jansiString = null, plainString = null; + for (OutputEnity output : OUTPUTS) { + if(output.type == OutputTypes.JANSI && JANSI) + { + if(jansiString != null){ + output.output.println(jansiString); + continue; + } - @LauncherAPI - public static synchronized void println(String message) { - for (Output output : OUTPUTS) { - output.println(message); + jansiString = ansiFormatLicense(product); + output.output.println(jansiString); + } + else + { + if(plainString != null){ + output.output.println(plainString); + continue; + } + + plainString = formatLicense(product); + output.output.println(plainString); + } } } @@ -322,7 +398,7 @@ private static String formatLicense(String product) { // Add std writer STD_OUTPUT = System.out::println; - addOutput(STD_OUTPUT); + addOutput(STD_OUTPUT, JANSI ? OutputTypes.JANSI : OutputTypes.PLAIN); // Add file log writer String logFile = System.getProperty("launcher.logFile");