From 86fc781adc483a555aa091ed341c813e0111abfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szombati=20Oliv=C3=A9r?= Date: Wed, 22 May 2024 11:31:04 +0200 Subject: [PATCH] Fix: Fixed imprecision --- .../src/temalab/logger/ConsoleLogger.java | 14 ++++---- test/core/src/temalab/logger/LoggerBase.java | 21 +++-------- .../src/temalab/logger/PrintStreamLogger.java | 36 +++++++++++++++++++ 3 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 test/core/src/temalab/logger/PrintStreamLogger.java diff --git a/test/core/src/temalab/logger/ConsoleLogger.java b/test/core/src/temalab/logger/ConsoleLogger.java index 14827cd..12d1a42 100644 --- a/test/core/src/temalab/logger/ConsoleLogger.java +++ b/test/core/src/temalab/logger/ConsoleLogger.java @@ -1,5 +1,5 @@ package temalab.logger; -public class ConsoleLogger extends LoggerBase{ +public class ConsoleLogger extends PrintStreamLogger{ static final String ANSI_RESET = "\u001B[0m"; static final String ANSI_RED = "\u001B[31m"; static final String ANSI_YELLOW = "\u001B[33m"; @@ -12,32 +12,32 @@ public ConsoleLogger(){ //TODO maybe can be placed elsewhere, e.g. Base or Logger interface and then it could be overriden private void printLines(String label, String message){ for (String line : message.split("\n")) { - getPrintStream().println(String.format("\t%s - %s", label, line)); + printStream.println(String.format("\t%s - %s", label, line)); } - getPrintStream().print(ANSI_RESET); + printStream.print(ANSI_RESET); } @Override public void debug(String label, String message){ - getPrintStream().println(ANSI_RESET + getInfos("DEBUG")); + printStream.println(ANSI_RESET + getInfos("DEBUG")); printLines(label, message); } @Override public void info(String label, String message){ - getPrintStream().print(ANSI_BLUE + getInfos("INFO")); + printStream.print(ANSI_BLUE + getInfos("INFO")); printLines(label, message); } @Override public void warning(String label, String message){ - getPrintStream().print(ANSI_YELLOW + getInfos("WARNING")); + printStream.print(ANSI_YELLOW + getInfos("WARNING")); printLines(label, message); } @Override public void error(String label, String message){ - getPrintStream().print(ANSI_RED + getInfos("ERROR")); + printStream.print(ANSI_RED + getInfos("ERROR")); printLines(label, message); } } diff --git a/test/core/src/temalab/logger/LoggerBase.java b/test/core/src/temalab/logger/LoggerBase.java index ae8c1ac..dc1f8c4 100644 --- a/test/core/src/temalab/logger/LoggerBase.java +++ b/test/core/src/temalab/logger/LoggerBase.java @@ -1,20 +1,8 @@ package temalab.logger; -import java.io.PrintStream; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public abstract class LoggerBase implements Logger { - private PrintStream printStream = null; - protected PrintStream getPrintStream() {return printStream;} - - public void setPrintStream(PrintStream p){ - printStream.close(); //TODO Cleaner - printStream = p; - } - - public LoggerBase(){ - printStream = System.err; - } protected String getInfos(String level) { String res = ""; @@ -43,14 +31,13 @@ public String getCallerMethod(){ return Thread.currentThread().getStackTrace()[5].getMethodName(); } - //TODO any better way to do it? @Override - public void debug(String label, String message){} + public abstract void debug(String label, String message); @Override - public void info(String label, String message){} + public abstract void info(String label, String message); @Override - public void warning(String label, String message){} + public abstract void warning(String label, String message); @Override - public void error(String label, String message){} + public abstract void error(String label, String message); } diff --git a/test/core/src/temalab/logger/PrintStreamLogger.java b/test/core/src/temalab/logger/PrintStreamLogger.java new file mode 100644 index 0000000..60c5bb4 --- /dev/null +++ b/test/core/src/temalab/logger/PrintStreamLogger.java @@ -0,0 +1,36 @@ +package temalab.logger; + +import java.io.PrintStream; + +public class PrintStreamLogger extends LoggerBase { + protected PrintStream printStream = null; + + public void setPrintStream(PrintStream p){ + printStream.close(); //TODO Cleaner + printStream = p; + } + + public PrintStreamLogger(){ + printStream = System.err; + } + + @Override + public void debug(String label, String message){ + printStream.println(getInfos("DEBUG")); + } + + @Override + public void info(String label, String message){ + printStream.print(getInfos("INFO")); + } + + @Override + public void warning(String label, String message){ + printStream.print(getInfos("WARNING")); + } + + @Override + public void error(String label, String message){ + printStream.print(getInfos("ERROR")); + } +}