Browse Source

TestLogger autolinewidth

JNI-96
heck 5 years ago
parent
commit
ec8ce88ce6
  1. 64
      test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java
  2. 6
      test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java

64
test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java

@ -1,14 +1,52 @@
package foundation.pEp.jniadapter.test.framework; package foundation.pEp.jniadapter.test.framework;
import static foundation.pEp.jniadapter.test.framework.TestUtils.TermColor; import java.io.BufferedReader;
import java.io.InputStreamReader;
import static foundation.pEp.jniadapter.test.framework.TestUtils.*;
public class TestLogger { public class TestLogger {
static {
init();
}
// options // options
private static boolean logEnabled = true; private static boolean logEnabled = true;
private static int lineWidth = 80; private static int lineWidth;
// constants // constants
private static int threadStrLen = 10; private static int threadStrLen = 10;
private static String threadSeparator = ": ";
private static boolean initialized = false;
private static void init() {
if (!initialized) {
tryDetermineTermSize();
initialized = true;
}
}
private static void tryDetermineTermSize() {
int nrCols = lineWidth;
try {
Process p = Runtime.getRuntime().exec("tput cols");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String cmdOutput = "";
String buf = null;
while ((buf = stdInput.readLine()) != null) {
cmdOutput += buf;
}
log("TERMSIZE: " + cmdOutput);
nrCols = Integer.valueOf(cmdOutput);
setLineWidth(clip(nrCols, 40, 2000));
} catch (Exception e) {
// something went wrong
}
}
public static void setLoggingEnabled(boolean enabled) { public static void setLoggingEnabled(boolean enabled) {
logEnabled = enabled; logEnabled = enabled;
@ -22,20 +60,22 @@ public class TestLogger {
return lineWidth; return lineWidth;
} }
public static void setLineWidth(int lineWidth) { public static int getMsgWidth() {
TestLogger.lineWidth = lineWidth; return lineWidth - threadStrLen - threadSeparator.length();
}
public static void setLineWidth(int width) {
lineWidth = width;
} }
// Log // Log
public static void log(String msg) { public static void log(String msg) {
if (logEnabled) { if (logEnabled) {
String indent = ""; String threadStr = padOrClipString(Thread.currentThread().getName(), " ", threadStrLen, Alignment.Left, "");
String separator = ": "; int indentStrLen = threadStrLen + threadSeparator.length();
int indentStrLen = threadStrLen + separator.length(); String indent = repeatString(" ", indentStrLen);
String threadStr = String.format("%-" + threadStrLen + "s", Thread.currentThread().getName());
indent = String.format("%" + indentStrLen + "s", " ");
msg = msg.replace("\n", "\n" + indent); msg = msg.replace("\n", "\n" + indent);
String logStr = threadStr + separator + msg; String logStr = threadStr + threadSeparator + msg;
System.out.println(logStr); System.out.println(logStr);
} }
} }
@ -48,7 +88,7 @@ public class TestLogger {
// LogH1 // LogH1
public static void logH1(String msg) { public static void logH1(String msg) {
log(TestUtils.padOrClipString(msg, "=", lineWidth, TestUtils.Alignment.Center, null)); log(TestUtils.padOrClipString(msg, "=", lineWidth - threadSeparator.length() - threadStrLen, TestUtils.Alignment.Center, null));
} }
public static void logH1(String msg, TermColor color) { public static void logH1(String msg, TermColor color) {
@ -59,7 +99,7 @@ public class TestLogger {
// LogH2 // LogH2
public static void logH2(String msg) { public static void logH2(String msg) {
log(TestUtils.padOrClipString(msg, "-", lineWidth, TestUtils.Alignment.Center, null)); log(TestUtils.padOrClipString(msg, "-", lineWidth - threadSeparator.length() - threadStrLen, TestUtils.Alignment.Center, null));
} }
public static void logH2(String msg, TermColor color) { public static void logH2(String msg, TermColor color) {

6
test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java

@ -165,9 +165,9 @@ public class TestUnit<T extends TestContextInterface> implements Runnable {
} }
private void logLayout() { private void logLayout() {
logFmtTestNameLen = (int) Math.floor(TestLogger.getLineWidth() * 0.39); logFmtTestNameLen = (int) Math.floor(TestLogger.getMsgWidth() * 0.39);
logFmtCtxNameLen = (int) Math.floor(TestLogger.getLineWidth() * 0.28); logFmtCtxNameLen = (int) Math.floor(TestLogger.getMsgWidth() * 0.28);
logFmtMsgLen = (int) Math.floor(TestLogger.getLineWidth() * 0.25); logFmtMsgLen = (int) Math.floor(TestLogger.getMsgWidth() * 0.25);
} }
private String makeLogString(String str) { private String makeLogString(String str) {

Loading…
Cancel
Save