- added Logger

- added logging capabilities to ProgressHandler classes
This commit is contained in:
fszimnau
2025-10-01 11:17:39 +02:00
parent b63326b0ac
commit bad7fa46eb

View File

@@ -11,9 +11,13 @@ import java.time.Duration;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.logging.Level;
public class FabianUtil {
private static final Logger LOG = new Logger(FabianUtil.class);
private static final String STD_DIR_TEMP = "/FIXME/temp/";
private static final String TEST_OUTPUT_DIR = "testOutput/";
public static final String ENDING_TXT = "txt";
@@ -26,8 +30,170 @@ public class FabianUtil {
private FabianUtil() {}
public static class Logger {
private final java.util.logging.Logger logger;
protected Logger(Class<?> clazz) {
this(java.util.logging.Logger.getLogger(clazz.getName()));
}
protected Logger(java.util.logging.Logger logger) {
this.logger = logger;
}
public boolean isLoggable(Level level) {
return logger.isLoggable(level);
}
public void severe(String msg) {
logger.severe(msg);
}
public void severe(String msg, Object param) {
logger.log(Level.SEVERE, msg, param);
}
public void severe(String msg, Object... params) {
logger.log(Level.SEVERE, msg, params);
}
public void severe(Supplier<String> msgSupplier) {
logger.severe(msgSupplier);
}
public void warning(String msg) {
logger.warning(msg);
}
public void warning(String msg, Object param) {
logger.log(Level.WARNING, msg, param);
}
public void warning(String msg, Object... params) {
logger.log(Level.WARNING, msg, params);
}
public void warning(Supplier<String> msgSupplier) {
logger.warning(msgSupplier);
}
public void info(String msg) {
logger.info(msg);
}
public void info(String msg, Object param) {
logger.log(Level.INFO, msg, param);
}
public void info(String msg, Object... params) {
logger.log(Level.INFO, msg, params);
}
public void info(Supplier<String> msgSupplier) {
logger.info(msgSupplier);
}
public void config(String msg) {
logger.config(msg);
}
public void config(String msg, Object param) {
logger.log(Level.CONFIG, msg, param);
}
public void config(String msg, Object... params) {
logger.log(Level.CONFIG, msg, params);
}
public void config(Supplier<String> msgSupplier) {
logger.config(msgSupplier);
}
public void fine(String msg) {
logger.fine(msg);
}
public void fine(String msg, Object param) {
logger.log(Level.FINE, msg, param);
}
public void fine(String msg, Object... params) {
logger.log(Level.FINE, msg, params);
}
public void fine(Supplier<String> msgSupplier) {
logger.fine(msgSupplier);
}
public void finer(String msg) {
logger.finer(msg);
}
public void finer(String msg, Object param) {
logger.log(Level.FINER, msg, param);
}
public void finer(String msg, Object... params) {
logger.log(Level.FINER, msg, params);
}
public void finer(Supplier<String> msgSupplier) {
logger.finer(msgSupplier);
}
public void finest(String msg) {
logger.finest(msg);
}
public void finest(String msg, Object param) {
logger.log(Level.FINEST, msg, param);
}
public void finest(String msg, Object... params) {
logger.log(Level.FINEST, msg, params);
}
public void finest(Supplier<String> msgSupplier) {
logger.finest(msgSupplier);
}
}
public static class ProgressHandler {
private static final Logger LOG = new Logger(ProgressHandler.class);
private static final DecimalFormat TIME_DECIMAL_FORMAT = new DecimalFormat("00");
protected static final int DEFAULT_LOG_DISTANCE = 50;
protected static final int DEFAULT_TIME_LOG_DISTANCE = 15;
@@ -37,22 +203,29 @@ public class FabianUtil {
private final int timeLogDistance;
private BigDecimal counter = BigDecimal.ZERO;
private ZonedDateTime lastLoggedAt;
protected final PrintStream printer;
public ProgressHandler() {
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE);
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE, null);
}
public ProgressHandler(int logDistance) {
this(logDistance, DEFAULT_TIME_LOG_DISTANCE);
this(logDistance, DEFAULT_TIME_LOG_DISTANCE, null);
}
public ProgressHandler(int logDistance, int timeLogDistance) {
this(logDistance, timeLogDistance, null);
}
public ProgressHandler(int logDistance, int timeLogDistance, PrintStream printer) {
startTime = ZonedDateTime.now();
this.logDistance = validateBD(logDistance, "Log-Distanz");
this.timeLogDistance = validateInt(timeLogDistance, "Zeitbasierte Log-Distanz");
this.printer = printer;
}
@@ -69,12 +242,17 @@ public class FabianUtil {
}
protected synchronized BigDecimal getCounter() {
protected Logger logger() {
return LOG;
}
protected final synchronized BigDecimal counter() {
return counter;
}
protected synchronized int getTimeLogDistance() {
protected final synchronized int timeLogDistance() {
return timeLogDistance;
}
@@ -104,7 +282,7 @@ public class FabianUtil {
protected synchronized boolean shouldLog() {
return counter.remainder(logDistance).signum() == 0 || isBored();
return (printer != null || logger().isLoggable(Level.INFO)) && (counter.remainder(logDistance).signum() == 0 || isBored());
}
@@ -121,7 +299,11 @@ public class FabianUtil {
public synchronized void log() {
Duration timePassed = getTimePassed();
System.out.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
if (printer != null) {
printer.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
} else {
logger().info("{} Stück erledigt. {} vergangen.", counter, formatDuration(timePassed));
}
updateLastLoggedAt();
}
@@ -148,44 +330,61 @@ public class FabianUtil {
public synchronized void logFinal() {
Duration timePassed = getTimePassed();
System.out.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
if (printer != null) {
printer.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
} else {
logger().info("{} Stück (100 %) erledigt. {} vergangen.", counter, formatDuration(timePassed));
}
}
}
public static class ProgressHandlerWithTotal extends ProgressHandler {
private static final Logger LOG = new Logger(ProgressHandlerWithTotal.class);
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
private static final BigDecimal ONE_HUNDRED_PERCENT = BigDecimal.valueOf(100);
private final BigDecimal total;
public ProgressHandlerWithTotal(int total) {
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE, total);
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE, total, null);
}
public ProgressHandlerWithTotal(int logDistance, int total) {
this(logDistance, DEFAULT_TIME_LOG_DISTANCE, total);
this(logDistance, DEFAULT_TIME_LOG_DISTANCE, total, null);
}
public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total) {
super(logDistance, timeLogDistance);
this(logDistance, timeLogDistance, total, null);
}
public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total, PrintStream printer) {
super(logDistance, timeLogDistance, printer);
this.total = validateBD(total, "Gesamt-Anzahl");
}
@Override
protected Logger logger() {
return LOG;
}
@Override
protected synchronized boolean shouldLog() {
return super.shouldLog() && getCounter().compareTo(total) <= 0;
return super.shouldLog() && counter().compareTo(total) <= 0;
}
@Override
public synchronized void log() {
BigDecimal counter = getCounter();
BigDecimal percentage = BigDecimal.valueOf(100).divide(total, MathContext.DECIMAL64).multiply(counter);
BigDecimal counter = counter();
BigDecimal percentage = ONE_HUNDRED_PERCENT.divide(total, MathContext.DECIMAL64).multiply(counter);
Duration timePassed = getTimePassed();
String remainingTimePart = "";
@@ -198,8 +397,13 @@ public class FabianUtil {
}
String counterPrint = total.compareTo(BigDecimal.TEN) >= 0 ? formatNumber(counter) : counter.toString();
System.out.println(counterPrint + " Stück von " + total + " (" + PERCENTAGE_FORMAT.format(percentage) + " %) erledigt. "
+ formatDuration(timePassed) + " vergangen" + remainingTimePart + ".");
if (printer != null) {
printer.println(counterPrint + " Stück von " + total + " (" + PERCENTAGE_FORMAT.format(percentage) + " %) erledigt. "
+ formatDuration(timePassed) + " vergangen" + remainingTimePart + ".");
} else {
logger().info("{} Stück von {} ({} %) erledigt. {} vergangen{}.",
counterPrint, total, PERCENTAGE_FORMAT.format(percentage), formatDuration(timePassed), remainingTimePart);
}
updateLastLoggedAt();
}
@@ -210,11 +414,11 @@ public class FabianUtil {
private synchronized void logTimeBased() {
while (getCounter().compareTo(total) != 0) {
while (counter().compareTo(total) != 0) {
logIf();
long timeSinceLastLogged = getTimeSinceLastLogged();
try {
TimeUnit.SECONDS.sleep(timeSinceLastLogged - getTimeLogDistance());
TimeUnit.SECONDS.sleep(timeSinceLastLogged - timeLogDistance());
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
return;
@@ -301,6 +505,7 @@ public class FabianUtil {
public static void writeToFile(String path, String filename, String ending, Reader content) {
Path toFile = Paths.get(path + filename + "." + ending);
LOG.fine("Schreibe in Datei {}", toFile);
try (content) {
BufferedWriter out = Files.newBufferedWriter(toFile);
content.transferTo(out);