From fe111b7b33dbd0452e1c2aa80c1ce413c6846c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szarkowicz=20D=C3=A1niel?= Date: Wed, 22 May 2024 19:27:00 +0200 Subject: [PATCH] add optional color to labels, fix extra newline on debug --- .../temalab/communicator/Communicator.java | 29 +++++++++++----- .../src/temalab/logger/ConsoleLogger.java | 25 +++++++++++--- test/core/src/temalab/logger/Label.java | 34 +++++++++++++++++++ test/core/src/temalab/logger/Log.java | 30 ++++++++++++++-- test/core/src/temalab/logger/Logger.java | 8 ++--- .../src/temalab/logger/PrintStreamLogger.java | 12 +++---- 6 files changed, 111 insertions(+), 27 deletions(-) create mode 100644 test/core/src/temalab/logger/Label.java diff --git a/test/core/src/temalab/communicator/Communicator.java b/test/core/src/temalab/communicator/Communicator.java index 8a28dc1..eaf28b4 100644 --- a/test/core/src/temalab/communicator/Communicator.java +++ b/test/core/src/temalab/communicator/Communicator.java @@ -3,6 +3,7 @@ import temalab.logger.Log; import temalab.model.Position; import temalab.model.Team; +import temalab.logger.Label; import java.io.*; import java.util.Scanner; @@ -20,11 +21,21 @@ public class Communicator { Thread errorThread; Process process; - private final String logLabel; + private final Label javaLogLabel; + private final Label pythonLogLabel; public Communicator(Team team, String fileName, String strategy) { this.team = team; - logLabel = team.getName() + " java"; + javaLogLabel = new Label( + team.getName() + " java", + Label.Color.Black, + team.getName() == "red" ? Label.Color.Red : Label.Color.White + ); + pythonLogLabel = new Label( + team.getName() + " python", + Label.Color.Black, + team.getName() == "red" ? Label.Color.Red : Label.Color.White + ); String currDir = System.getProperty("user.dir"); ProcessBuilder processBuilder = new ProcessBuilder("python3", currDir + '/' + fileName, strategy); try { @@ -40,7 +51,7 @@ public Communicator(Team team, String fileName, String strategy) { out = new PrintWriter(new OutputStreamWriter(outputStream), true); BufferedReader erroReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); errorThread = new Thread(() -> { - erroReader.lines().forEach(s -> Log.d(team.getName() + " python", s)); + erroReader.lines().forEach(s -> Log.d(pythonLogLabel, s)); }); errorThread.start(); out.println(team.getName()); @@ -83,8 +94,8 @@ public void registerUnit() { public void communicate() { team.refillActionPoints(); runCounter++; - Log.d(logLabel, "RUN:" + runCounter); - Log.d(logLabel, "communicating"); + Log.d(javaLogLabel, "RUN:" + runCounter); + Log.d(javaLogLabel, "communicating"); loop: while (true) { @@ -109,13 +120,13 @@ public void communicate() { throw new RuntimeException("No answer from python"); } String answer = sc.nextLine(); - Log.d(logLabel, "anwser from python: " + answer); + Log.d(javaLogLabel, "anwser from python: " + answer); String[] split = answer.split(" "); switch (split[0]) { case "endTurn": break loop; case "reset": - Log.d(logLabel, "reset shuold happen"); + Log.d(javaLogLabel, "reset shuold happen"); team.reset(); break loop; case "move": { @@ -127,11 +138,11 @@ public void communicate() { } break; default: - Log.w(logLabel, "anwser starting with: " + split[0] + " could not be interpreted"); + Log.w(javaLogLabel, "anwser starting with: " + split[0] + " could not be interpreted"); break loop; } } - Log.d(logLabel, "ENDcommunicating"); + Log.d(javaLogLabel, "ENDcommunicating"); } private void unitMove(String[] split) { diff --git a/test/core/src/temalab/logger/ConsoleLogger.java b/test/core/src/temalab/logger/ConsoleLogger.java index b36c61b..58cfb41 100644 --- a/test/core/src/temalab/logger/ConsoleLogger.java +++ b/test/core/src/temalab/logger/ConsoleLogger.java @@ -17,28 +17,43 @@ public ConsoleLogger(PrintStream printStream) { } @Override - public void debug(String label, String message) { - printStream.println(ANSI_RESET); + protected void printLines(Label label, String message) { + for (String line : message.split("\n")) { + printStream.println( + String.format( + "\t%s%s%s - %s", + label.color, + label.label, + ANSI_RESET, + line.stripTrailing() + ) + ); + } + } + + @Override + public void debug(Label label, String message) { + printStream.print(ANSI_RESET); super.debug(label, message); printStream.print(ANSI_RESET); } @Override - public void info(String label, String message) { + public void info(Label label, String message) { printStream.print(ANSI_BLUE); super.info(label, message); printStream.print(ANSI_RESET); } @Override - public void warning(String label, String message) { + public void warning(Label label, String message) { printStream.print(ANSI_YELLOW); super.warning(label, message); printStream.print(ANSI_RESET); } @Override - public void error(String label, String message) { + public void error(Label label, String message) { printStream.print(ANSI_RED); super.error(label, message); printStream.print(ANSI_RESET); diff --git a/test/core/src/temalab/logger/Label.java b/test/core/src/temalab/logger/Label.java new file mode 100644 index 0000000..df6b4cc --- /dev/null +++ b/test/core/src/temalab/logger/Label.java @@ -0,0 +1,34 @@ +package temalab.logger; + +public class Label { + public enum Color { + Black(30, 40), + Red(31, 41), + Green(32, 42), + Yellow(33, 43), + Blue(34, 44), + Magenta(35, 45), + Cyan(36, 46), + White(37, 47), + Default(39, 49), + None(0, 0); + + final String foreground; + final String background; + + Color(int foreground, int background) { + this.foreground = "\u001B[" + foreground + "m"; + this.background = "\u001B[" + background + "m"; + } + } + + public final String label; + public final String color; + + public Label(String label, Label.Color foregroundColor, Label.Color backgroundColor) { + this.label = label; + this.color = + (foregroundColor.equals(Label.Color.None) ? "" : foregroundColor.foreground) + + (backgroundColor.equals(Label.Color.None) ? "" : backgroundColor.background); + } +} diff --git a/test/core/src/temalab/logger/Log.java b/test/core/src/temalab/logger/Log.java index a582283..a972fd2 100644 --- a/test/core/src/temalab/logger/Log.java +++ b/test/core/src/temalab/logger/Log.java @@ -12,18 +12,42 @@ public void setLogger(Logger newLogger) { } public static void d(String label, String message) { - logger.debug(label, message); + logger.debug( + new Label(label, Label.Color.None, Label.Color.None), + message); } public static void i(String label, String message) { - logger.info(label, message); + logger.info( + new Label(label, Label.Color.None, Label.Color.None), + message); } public static void w(String label, String message) { - logger.warning(label, message); + logger.warning( + new Label(label, Label.Color.None, Label.Color.None), + message); } public static void e(String label, String message) { + logger.error( + new Label(label, Label.Color.None, Label.Color.None), + message); + } + + public static void d(Label label, String message) { + logger.debug(label, message); + } + + public static void i(Label label, String message) { + logger.info(label, message); + } + + public static void w(Label label, String message) { + logger.warning(label, message); + } + + public static void e(Label label, String message) { logger.error(label, message); } } diff --git a/test/core/src/temalab/logger/Logger.java b/test/core/src/temalab/logger/Logger.java index 862c71a..bb22546 100644 --- a/test/core/src/temalab/logger/Logger.java +++ b/test/core/src/temalab/logger/Logger.java @@ -1,11 +1,11 @@ package temalab.logger; public interface Logger { - public void debug(String label, String message); + public void debug(Label label, String message); - public void info(String label, String message); + public void info(Label label, String message); - public void warning(String label, String message); + public void warning(Label label, String message); - public void error(String label, String message); + public void error(Label label, String message); } diff --git a/test/core/src/temalab/logger/PrintStreamLogger.java b/test/core/src/temalab/logger/PrintStreamLogger.java index 2af0eb4..cd779fd 100644 --- a/test/core/src/temalab/logger/PrintStreamLogger.java +++ b/test/core/src/temalab/logger/PrintStreamLogger.java @@ -9,32 +9,32 @@ public PrintStreamLogger(PrintStream printStream) { this.printStream = printStream; } - protected void printLines(String label, String message) { + protected void printLines(Label label, String message) { for (String line : message.split("\n")) { - printStream.println(String.format("\t%s - %s", label, line)); + printStream.println(String.format("\t%s - %s", label.label, line.stripTrailing())); } } @Override - public void debug(String label, String message) { + public void debug(Label label, String message) { printStream.println(getInfos("DEBUG")); printLines(label, message); } @Override - public void info(String label, String message) { + public void info(Label label, String message) { printStream.print(getInfos("INFO")); printLines(label, message); } @Override - public void warning(String label, String message) { + public void warning(Label label, String message) { printStream.print(getInfos("WARNING")); printLines(label, message); } @Override - public void error(String label, String message) { + public void error(Label label, String message) { printStream.print(getInfos("ERROR")); printLines(label, message); }