Skip to content

Commit

Permalink
Fix: added basic console implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Szombatioi authored and daniel-szarkowicz committed May 22, 2024
1 parent 4ef5466 commit a735485
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 39 deletions.
43 changes: 43 additions & 0 deletions test/core/src/temalab/logger/ConsoleLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package temalab.logger;
public class ConsoleLogger extends LoggerBase{
static final String ANSI_RESET = "\u001B[0m";
static final String ANSI_RED = "\u001B[31m";
static final String ANSI_YELLOW = "\u001B[33m";
static final String ANSI_BLUE = "\u001B[34m";

public ConsoleLogger(){
setPrintStream(System.out);
}

//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));
}
getPrintStream().print(ANSI_RESET);
}

@Override
public void debug(String label, String message){
getPrintStream().println(ANSI_RESET + getInfos("DEBUG"));
printLines(label, message);
}

@Override
public void info(String label, String message){
getPrintStream().print(ANSI_BLUE + getInfos("INFO"));
printLines(label, message);
}

@Override
public void warning(String label, String message){
getPrintStream().print(ANSI_YELLOW + getInfos("WARNING"));
printLines(label, message);
}

@Override
public void error(String label, String message){
getPrintStream().print(ANSI_RED + getInfos("ERROR"));
printLines(label, message);
}
}
14 changes: 5 additions & 9 deletions test/core/src/temalab/logger/Log.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package temalab.logger;

import java.io.PrintStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public final class Log {
private static Logger logger = new ConsoleLogger();


/**
* By default the Logger writes to the standard error stream.
*/
Expand All @@ -24,8 +20,8 @@ public void setLogger(Logger newLogger) {
logger = newLogger;
}

public void d(String label, String message){ logger.debug(label, message); }
public void i(String label, String message){ logger.info(label, message); }
public void w(String label, String message){ logger.warning(label, message); }
public void e(String label, String message){ logger.error(label, message); }
public static void d(String label, String message){ logger.debug(label, message); }
public static void i(String label, String message){ logger.info(label, message); }
public static void w(String label, String message){ logger.warning(label, message); }
public static void e(String label, String message){ logger.error(label, message); }
}
4 changes: 0 additions & 4 deletions test/core/src/temalab/logger/Logger.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
package temalab.logger;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public interface Logger {
public void debug(String label, String message);
public void info(String label, String message);
Expand Down
58 changes: 32 additions & 26 deletions test/core/src/temalab/logger/LoggerBase.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
package temalab.logger;

import java.io.PrintStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LoggerBase implements Logger {
public abstract class LoggerBase implements Logger {
private PrintStream printStream = null;
protected PrintStream getPrintStream() {return printStream;}

public void setPrintStream(PrintStream p){
printStream.close(); //TODO
printStream.close(); //TODO Cleaner
printStream = p;
}

protected void getInfos(String level, String label, String message) {
public LoggerBase(){
printStream = System.err;
}

protected String getInfos(String level) {
String res = "";

String timeOfLog = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String header = String.format("[%s - %s]", timeOfLog, level);
String className = Thread.currentThread().getStackTrace()[1].getClass().getName(); //caller.getClass().getPackage().getName();
String function = Thread.currentThread().getStackTrace()[1].getMethodName() + "()";
String className = getCallerClass();
String function = String.format("%s()", getCallerMethod());

this.printStream.print(String.format("%-35s\t%-10s\t%-10s", header, className, function));
this.printStream.println(String.format("%s - %s", label, message));
res += String.format("%-35s\t%-10s\t%-10s", header, className, function);
return res;
}


@Override
public void debug(String label, String message) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'debug'");
//TODO this should be overriden in each implementation
public String getCallerClass(){
//0 - getStackTrace()
//1 - getCallerClass()
//2 - getInfos()
//3 - the logger class
//4 - Log class
//5 - the actual caller
return Thread.currentThread().getStackTrace()[5].getClassName();
}

@Override
public void info(String label, String message) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'info'");
public String getCallerMethod(){
return Thread.currentThread().getStackTrace()[5].getMethodName();
}

//TODO any better way to do it?
@Override
public void warning(String label, String message) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'warning'");
}

public void debug(String label, String message){}
@Override
public void error(String label, String message) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'error'");
}
public void info(String label, String message){}
@Override
public void warning(String label, String message){}
@Override
public void error(String label, String message){}

}

0 comments on commit a735485

Please sign in to comment.