Skip to content

Commit

Permalink
Merge pull request #25 from denizt/logging-ftw
Browse files Browse the repository at this point in the history
Replace System.out and System.err with logging
  • Loading branch information
lucav76 authored Sep 24, 2024
2 parents e2c2bee + 22e8e17 commit ad4c30d
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 135 deletions.
7 changes: 0 additions & 7 deletions src/main/java/eu/lucaventuri/collections/ClassifiedMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@ public <T> T removeHeadConverted(Function<T, ?> converter) {
}
}

private void showDebug(String descr, Object value) {
System.out.println(descr + " - Sizes: " + mapByClass.size() + " vs " + list.asListFromHead().size() + " - Class: " + value.getClass());

for (var clz : mapByClass.keySet())
System.out.println(" " + clz);
}

private void verify() {
assert (mapByClass.isEmpty() && list.isEmpty()) || (!mapByClass.isEmpty() && !list.isEmpty());
}
Expand Down
63 changes: 22 additions & 41 deletions src/main/java/eu/lucaventuri/common/Exceptions.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package eu.lucaventuri.common;

//import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

//@Slf4j
final public class Exceptions {

private static final Logger logger = Logger.getLogger(Exceptions.class.getName());
private Exceptions() { /* Static only*/ }

public static void silence(RunnableEx run) {
try {
run.run();
} catch (Throwable t) {
/* */
logger.log(Level.FINEST, t.getMessage(), t);
}
}

public static void silence(RunnableEx run, RunnableEx finalizer) {
try {
run.run();
} catch (Throwable t) {
/* */
logger.log(Level.FINEST, t.getMessage(), t);
} finally {
if (finalizer != null)
Exceptions.silence(finalizer::run);
Expand All @@ -36,7 +38,7 @@ public static Runnable silentRunnable(RunnableEx run) {
try {
run.run();
} catch (Throwable t) {
/* */
logger.log(Level.FINEST, t.getMessage(), t);
}
};
}
Expand All @@ -53,10 +55,11 @@ public static <T> T silence(CallableEx<T, ? extends Throwable> call, T valueOnEx
try {
return call.call();
} catch (Throwable t) {
logger.log(Level.FINEST, t.getMessage(), t);
return valueOnException;
} finally {
if (finalizer != null)
Exceptions.silence(finalizer::run);
Exceptions.silence(finalizer);
}
}

Expand All @@ -65,6 +68,7 @@ public static <T> Callable<T> silentCallable(CallableEx<T, ? extends Throwable>
try {
return call.call();
} catch (Throwable t) {
logger.log(Level.FINEST, t.getMessage(), t);
return valueOnException;
}
};
Expand All @@ -74,7 +78,8 @@ public static <T> Consumer<T> silentConsumer(ConsumerEx<T, ? extends Throwable>
return input -> {
try {
consumer.accept(input);
} catch (Throwable throwable) {
} catch (Throwable t) {
logger.log(Level.FINEST, t.getMessage(), t);
}
};
}
Expand All @@ -99,19 +104,15 @@ public static void log(RunnableEx run) {
try {
run.run();
} catch (Throwable t) {
// log.error(t.getMessage(), t); Not working...
// FIXME: log for real
t.printStackTrace();
logger.log(Level.INFO, t.getMessage(), t);
}
}

public static void log(RunnableEx run, RunnableEx finalRun) {
try {
run.run();
} catch (Throwable t) {
// log.error(t.getMessage(), t); Not working...
// FIXME: log for real
t.printStackTrace();
logger.log(Level.INFO, t.getMessage(), t);
}
finally {
Exceptions.log(finalRun);
Expand All @@ -122,9 +123,7 @@ public static <T> T log(CallableEx<T, ? extends Throwable> call, T valueOnExcept
try {
return call.call();
} catch (Throwable t) {
// FIXME: log for real
t.printStackTrace();

logger.log(Level.INFO, t.getMessage());
return valueOnException;
}
}
Expand All @@ -133,9 +132,7 @@ public static <T> T log(CallableEx<T, ? extends Throwable> call, T valueOnExcept
try {
return call.call();
} catch (Throwable t) {
// FIXME: log for real
t.printStackTrace();

logger.log(Level.INFO, t.getMessage());
return valueOnException;
}
finally {
Expand All @@ -147,19 +144,15 @@ public static void logShort(RunnableEx run) {
try {
run.run();
} catch (Throwable t) {
// log.error(t.getMessage(), t); Not working...
// FIXME: log for real
System.err.println(t);
logger.log(Level.INFO, t.getMessage());
}
}

public static void logShort(RunnableEx run, RunnableEx finalRun) {
try {
run.run();
} catch (Throwable t) {
// log.error(t.getMessage(), t); Not working...
// FIXME: log for real
System.err.println(t);
logger.log(Level.INFO, t.getMessage());
}
finally {
Exceptions.log(finalRun);
Expand All @@ -170,9 +163,7 @@ public static <T> T logShort(CallableEx<T, ? extends Throwable> call, T valueOnE
try {
return call.call();
} catch (Throwable t) {
// FIXME: log for real
System.err.println(t);

logger.log(Level.INFO, t.getMessage());
return valueOnException;
}
}
Expand All @@ -181,9 +172,7 @@ public static <T> T logShort(CallableEx<T, ? extends Throwable> call, T valueOnE
try {
return call.call();
} catch (Throwable t) {
// FIXME: log for real
System.err.println(t);

logger.log(Level.INFO, t.getMessage());
return valueOnException;
}
finally {
Expand All @@ -204,8 +193,7 @@ public static <T, E extends Throwable> T orElse(CallableEx<T, E> callable, Suppl
try {
return callable.call();
} catch (Throwable e) {
System.err.println(e);

logger.log(Level.INFO, e.getMessage(), e);
return defaultSupplier.get();
}
}
Expand All @@ -223,8 +211,7 @@ public static <T, E extends Throwable> T orElseValue(CallableEx<T, E> callable,
try {
return callable.call();
} catch (Throwable e) {
System.err.println(e);

logger.log(Level.INFO, e.getMessage(), e);
return defaultValue;
}
}
Expand Down Expand Up @@ -254,12 +241,6 @@ public static Stream<StackTraceElement> getCompactStackTrace(Throwable ex, boole
return stream;
}

public static void printCompactStackTrace(Throwable ex, boolean skippAllJava) {
Stream<StackTraceElement> stream = getCompactStackTrace(ex, skippAllJava);

stream.forEach(System.err::println);
}

private static boolean isJavaClass(StackTraceElement elem) {
String cls = elem.getClassName();

Expand Down
32 changes: 16 additions & 16 deletions src/main/java/eu/lucaventuri/common/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import java.util.stream.Stream.Builder;

Expand All @@ -18,7 +19,9 @@
*/
public final class SystemUtils {
private static final boolean assertsEnabled;
public static final MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();;
public static final MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();

private static final Logger logger = Logger.getLogger(SystemUtils.class.getName());

static {
// Check if the asserts are enabled
Expand Down Expand Up @@ -47,7 +50,7 @@ public static void sleep(long ms) {
/**
* Sleeps some ms
*
* @param done return true if the sleep is over
* @param done return true if the sleep is over
* @param msToSleepBetweenTests ms to sleep between each test
*/
public static void sleepUntil(Supplier<Boolean> done, int msToSleepBetweenTests) {
Expand Down Expand Up @@ -136,7 +139,7 @@ public static void close(AutoCloseable clo) {
try {
clo.close();
} catch (Exception e) {
System.err.println(e);
logger.log(Level.FINEST, "Error closing " + clo, e);
}
}

Expand All @@ -152,7 +155,7 @@ public static void close(Closeable clo) {
try {
clo.close();
} catch (IOException e) {
System.err.println(e);
logger.log(Level.FINEST, "Error closing " + clo, e);
}
}

Expand Down Expand Up @@ -182,6 +185,7 @@ public static Class findClassByName(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
logger.log(Level.FINEST, "Class not found" , e);
return null;
}
}
Expand All @@ -207,10 +211,10 @@ public static <E extends Exception> BenchmarkResult benchmarkEx(RunnableEx<E> ru
}

public static <E extends Exception> BenchmarkResult benchmarkEx(RunnableEx<E> run, RunnableEx<E> cleanup, int numRunning) throws E {
long times[] = new long[numRunning];
long[] times = new long[numRunning];

for (int i = 0; i < numRunning; i++) {
System.out.println("Round " + (i + 1) + " of " + numRunning);
logger.log(Level.FINE, "Round " + (i + 1) + " of " + numRunning);
times[i] = timeEx(run);

if (cleanup != null)
Expand All @@ -222,23 +226,19 @@ public static <E extends Exception> BenchmarkResult benchmarkEx(RunnableEx<E> ru

public static long printTime(Runnable run, String description) {
long start = System.currentTimeMillis();

run.run();

long time = System.currentTimeMillis() - start;

System.out.println(description + " : " + time + " ms");

return time;
}

public static <E extends Exception> long printTimeEx(RunnableEx<E> run, String description) throws E {
long start = System.currentTimeMillis();

run.run();

long time = System.currentTimeMillis() - start;

System.out.println(description + " : " + time + " ms");

return time;
Expand Down Expand Up @@ -269,14 +269,14 @@ public static void keepReadingStream(InputStream is, byte ar[]) throws IOExcepti
}

public static void keepReadingStream(InputStream is, byte ar[], int offset, int len) throws IOException {
assert offset>=0;
assert len>=0;
assert offset+len<=ar.length;
assert offset >= 0;
assert len >= 0;
assert offset + len <= ar.length;

int cur = 0;

while (cur < len) {
int read = is.read(ar, cur+offset, len - cur);
int read = is.read(ar, cur + offset, len - cur);

if (read < 0)
throw new EOFException("Stream terminated after " + cur + " bytes!");
Expand All @@ -286,7 +286,7 @@ public static void keepReadingStream(InputStream is, byte ar[], int offset, int

/**
* @param multiCastRequired True if the interface needs to support multicast
* @param siteLocalOnly true if only siteLocal IP addresses are required
* @param siteLocalOnly true if only siteLocal IP addresses are required
* @return all the local IP addresses of the computer
* @throws SocketException
*/
Expand Down
Loading

0 comments on commit ad4c30d

Please sign in to comment.