Skip to content

Commit

Permalink
add optional color to labels, fix extra newline on debug
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-szarkowicz committed May 22, 2024
1 parent 3ed2bca commit fe111b7
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 27 deletions.
29 changes: 20 additions & 9 deletions test/core/src/temalab/communicator/Communicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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());
Expand Down Expand Up @@ -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) {
Expand All @@ -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": {
Expand All @@ -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) {
Expand Down
25 changes: 20 additions & 5 deletions test/core/src/temalab/logger/ConsoleLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions test/core/src/temalab/logger/Label.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
30 changes: 27 additions & 3 deletions test/core/src/temalab/logger/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 4 additions & 4 deletions test/core/src/temalab/logger/Logger.java
Original file line number Diff line number Diff line change
@@ -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);
}
12 changes: 6 additions & 6 deletions test/core/src/temalab/logger/PrintStreamLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit fe111b7

Please sign in to comment.