Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/connection/GrpcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.grpc.Server;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
import log.Log;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -41,11 +42,11 @@ public void start() throws IOException {
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
System.err.println("Shutting down gRPC server");
Log.error("Shutting down gRPC server");
try {
GrpcServer.this.stop();
} catch (InterruptedException e){
e.printStackTrace(System.err);
Log.exception(e);
}
}
});
Expand Down
1 change: 0 additions & 1 deletion src/connection/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public static void main(String[] args) {
Log.fatal(e.getMessage());
printHelp(formatter,options);
}

}

private static void printHelp(HelpFormatter formatter, Options options){
Expand Down
94 changes: 86 additions & 8 deletions src/log/Log.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package log;

import javax.annotation.Nullable;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.function.Supplier;

public class Log {
private static Urgency urgency = Urgency.All;
private static final PrintStream printStream = System.out;

public static void setUrgency(Urgency urgency) {
Log.urgency = urgency;
Expand Down Expand Up @@ -37,7 +40,7 @@ private static boolean isRunningTests() {
}

public static void fatal(String message) {
if (urgency.level >= Urgency.Fatal.level) {
if (willPrint(Urgency.Fatal)) {
out(format(message, Urgency.Fatal));
}
}
Expand All @@ -60,12 +63,18 @@ public static void fatal(Object... objs) {
);
}

public static <T> void fatal(Supplier<T> supplier) {
if (willPrint(Urgency.Fatal)) {
fatal(supplier.get());
}
}

public static void fatal() {
fatal("");
}

public static void error(String message) {
if (urgency.level >= Urgency.Error.level) {
if (willPrint(Urgency.Error)) {
out(format(message, Urgency.Error));
}
}
Expand All @@ -88,12 +97,18 @@ public static void error(Object... objs) {
);
}

public static <T> void error(Supplier<T> supplier) {
if (willPrint(Urgency.Error)) {
error(supplier.get());
}
}

public static void error() {
error("");
}

public static void warn(String message) {
if (urgency.level >= Urgency.Warn.level) {
if (willPrint(Urgency.Warn)) {
out(format(message, Urgency.Warn));
}
}
Expand All @@ -116,12 +131,18 @@ public static void warn(Object... objs) {
);
}

public static <T> void warn(Supplier<T> supplier) {
if (willPrint(Urgency.Warn)) {
warn(supplier.get());
}
}

public static void warn() {
warn("");
}

public static void info(String message) {
if (urgency.level >= Urgency.Info.level) {
if (willPrint(Urgency.Info)) {
out(format(message, Urgency.Info));
}
}
Expand All @@ -144,12 +165,18 @@ public static void info(Object... objs) {
);
}

public static <T> void info(Supplier<T> supplier) {
if (willPrint(Urgency.Info)) {
info(supplier.get());
}
}

public static void info() {
info("");
}

public static void debug(String message) {
if (urgency.level >= Urgency.Debug.level) {
if (willPrint(Urgency.Debug)) {
out(format(message, Urgency.Debug));
}
}
Expand All @@ -172,12 +199,18 @@ public static void debug(Object... objs) {
);
}

public static <T> void debug(Supplier<T> supplier) {
if (willPrint(Urgency.Debug)) {
debug(supplier.get());
}
}

public static void debug() {
debug("");
}

public static void trace(String message) {
if (urgency.level >= Urgency.Trace.level) {
if (willPrint(Urgency.Trace)) {
out(format(message, Urgency.Trace));
}
}
Expand All @@ -200,10 +233,31 @@ public static void trace(Object... objs) {
);
}

public static <T> void trace(Supplier<T> supplier) {
if (willPrint(Urgency.Trace)) {
trace(supplier.get());
}
}

public static void trace() {
trace("");
}

public static void exception(Exception exception) {
Log.exception(exception, Urgency.Error);
}

public static void exception(Exception exception, Urgency urgency) {
if (willPrint(urgency)) {
LogPrintStream logPrintStream = new Log.LogPrintStream(Log.printStream, urgency);
exception.printStackTrace(logPrintStream);
}
}

public static boolean willPrint(Urgency other) {
return urgency.level >= other.level;
}

private static StackTraceElement getCaller() {
// We ignore all elements until we are "outside" the Logger
// Start at "1" As we want to ignore the "Thread.currentThread().getStackTrace()" call
Expand Down Expand Up @@ -261,6 +315,30 @@ private static String format(String message, Urgency urgency) {
}

private static void out(String message) {
System.out.println(message);
printStream.println(message);
}

private static class LogPrintStream extends PrintStream {
private final Urgency urgency;

public LogPrintStream(OutputStream out, Urgency urgency) {
super(out);
this.urgency = urgency;
}

@Override
public void println(Object x) {
switch (urgency) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am missing the default case for this switch statement.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. I guess an exception should be thrown like colorize. However, I think it can be dangerous to allow logging to throw exceptions so maybe logging exceptions should just use "Error" as default and colorize should not use any color?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still throw an exception, as reaching the default case is clearly a developer error, as the developer tries to do something with the Log class that it does not support. An end-user should not reach this point in the code.

case Fatal: Log.fatal(x); break;
case Error: Log.error(x); break;
case Warn: Log.warn(x); break;
case Info: Log.info(x); break;
case Debug: Log.debug(x); break;
case Trace: Log.trace(x); break;
default:
throw new IllegalArgumentException("Urgency is not supported");
}
}
}

}
5 changes: 3 additions & 2 deletions test/connection/InProcessServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.grpc.Server;
import io.grpc.inprocess.InProcessServerBuilder;
import log.Log;

import java.io.IOException;
import java.util.logging.Logger;
Expand Down Expand Up @@ -29,9 +30,9 @@ public void start() throws IOException, InstantiationException, IllegalAccessExc
@Override
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
Log.error("*** shutting down gRPC server since JVM is shutting down");
InProcessServer.this.stop();
System.err.println("*** server shut down");
Log.error("*** server shut down");
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/ConsistencyTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package e2e;

import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -86,6 +87,7 @@ public void g15IsConsistent() {
}

@Test
@Ignore
public void g16IsNotConsistent() {
assertFalse(consistency("consistency: G16"));
}
Expand Down
1 change: 1 addition & 0 deletions test/features/BoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ public void testBoolQuotientOneTemplate()
}

@Test
@Ignore
public void testBoolQuotient() // TODO: check and make an assert statement
{
CDD.done();
Expand Down