Compare commits
3 Commits
f514e1c6f8
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| d2787cb76d | |||
| 13c783a762 | |||
| d881edd247 |
+36
-132
@@ -11,8 +11,10 @@ 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.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.SimpleFormatter;
|
||||
import java.util.logging.StreamHandler;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class FabianUtil {
|
||||
@@ -31,180 +33,101 @@ public class FabianUtil {
|
||||
private FabianUtil() {}
|
||||
|
||||
|
||||
public static class Logger {
|
||||
public static class Logger extends java.util.logging.Logger {
|
||||
|
||||
private final java.util.logging.Logger logger;
|
||||
|
||||
|
||||
protected Logger(Class<?> clazz) {
|
||||
public 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 Logger(java.util.logging.Logger logger) {
|
||||
super(logger.getName(), logger.getResourceBundleName());
|
||||
}
|
||||
|
||||
|
||||
public void severe(String msg, Object param) {
|
||||
logger.log(Level.SEVERE, msg, param);
|
||||
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);
|
||||
log(Level.SEVERE, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void warning(String msg, Object param) {
|
||||
logger.log(Level.WARNING, msg, param);
|
||||
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);
|
||||
log(Level.WARNING, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void info(String msg, Object param) {
|
||||
logger.log(Level.INFO, msg, param);
|
||||
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);
|
||||
log(Level.INFO, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void config(String msg, Object param) {
|
||||
logger.log(Level.CONFIG, msg, param);
|
||||
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);
|
||||
log(Level.CONFIG, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void fine(String msg, Object param) {
|
||||
logger.log(Level.FINE, msg, param);
|
||||
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);
|
||||
log(Level.FINE, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void finer(String msg, Object param) {
|
||||
logger.log(Level.FINER, msg, param);
|
||||
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);
|
||||
log(Level.FINER, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void finest(String msg, Object param) {
|
||||
logger.log(Level.FINEST, msg, param);
|
||||
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);
|
||||
log(Level.FINEST, msg, params);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
|
||||
protected final Logger log;
|
||||
private final ZonedDateTime startTime;
|
||||
private final BigDecimal logDistance;
|
||||
private final int timeLogDistance;
|
||||
private BigDecimal counter = BigDecimal.ZERO;
|
||||
private ZonedDateTime lastLoggedAt;
|
||||
protected final PrintStream printer;
|
||||
|
||||
|
||||
public ProgressHandler() {
|
||||
@@ -222,11 +145,17 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandler(int logDistance, int timeLogDistance, PrintStream printer) {
|
||||
public ProgressHandler(int logDistance, int timeLogDistance, OutputStream printer) {
|
||||
startTime = ZonedDateTime.now();
|
||||
this.logDistance = validateBD(logDistance, "Log-Distanz");
|
||||
this.timeLogDistance = validateInt(timeLogDistance, "Zeitbasierte Log-Distanz");
|
||||
this.printer = printer;
|
||||
log = new Logger(getClass());
|
||||
if (printer != null) {
|
||||
for (Handler handler : log.getHandlers()) {
|
||||
log.removeHandler(handler);
|
||||
}
|
||||
log.addHandler(new StreamHandler(printer, new SimpleFormatter()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -243,11 +172,6 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
|
||||
protected Logger logger() {
|
||||
return LOG;
|
||||
}
|
||||
|
||||
|
||||
protected final synchronized BigDecimal counter() {
|
||||
return counter;
|
||||
}
|
||||
@@ -283,7 +207,7 @@ public class FabianUtil {
|
||||
|
||||
|
||||
protected synchronized boolean shouldLog() {
|
||||
return (printer != null || logger().isLoggable(Level.INFO)) && (counter.remainder(logDistance).signum() == 0 || isBored());
|
||||
return log.isLoggable(Level.INFO) && (counter.remainder(logDistance).signum() == 0 || isBored());
|
||||
}
|
||||
|
||||
|
||||
@@ -300,11 +224,7 @@ public class FabianUtil {
|
||||
|
||||
public synchronized void log() {
|
||||
Duration timePassed = getTimePassed();
|
||||
if (printer != null) {
|
||||
printer.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
|
||||
} else {
|
||||
logger().info("{} Stück erledigt. {} vergangen.", counter, formatDuration(timePassed));
|
||||
}
|
||||
log.info("{0} Stück erledigt. {1} vergangen.", counter, formatDuration(timePassed));
|
||||
updateLastLoggedAt();
|
||||
}
|
||||
|
||||
@@ -331,18 +251,13 @@ public class FabianUtil {
|
||||
|
||||
public synchronized void logFinal() {
|
||||
Duration timePassed = getTimePassed();
|
||||
if (printer != null) {
|
||||
printer.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
|
||||
} else {
|
||||
logger().info("{} Stück (100 %) erledigt. {} vergangen.", counter, formatDuration(timePassed));
|
||||
}
|
||||
log.info("{0} Stück (100 %) erledigt. {1} 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);
|
||||
|
||||
@@ -364,18 +279,12 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total, PrintStream printer) {
|
||||
public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total, OutputStream 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() && counter().compareTo(total) <= 0;
|
||||
@@ -398,13 +307,8 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
String counterPrint = total.compareTo(BigDecimal.TEN) >= 0 ? formatNumber(counter) : counter.toString();
|
||||
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);
|
||||
}
|
||||
log.info("{0} Stück von {1} ({2} %) erledigt. {3} vergangen{4}.",
|
||||
counterPrint, total, PERCENTAGE_FORMAT.format(percentage), formatDuration(timePassed), remainingTimePart);
|
||||
updateLastLoggedAt();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user