Skip to content

Commit

Permalink
https://github.com/jcabi/jcabi-log/issues/87
Browse files Browse the repository at this point in the history
  • Loading branch information
dean-e-clark committed Sep 8, 2016
1 parent 45752b6 commit 3bd48e8
Showing 1 changed file with 66 additions and 17 deletions.
83 changes: 66 additions & 17 deletions src/main/java/com/jcabi/log/VerboseProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import lombok.EqualsAndHashCode;
import lombok.ToString;

Expand Down Expand Up @@ -106,10 +107,15 @@ public final class VerboseProcess implements Closeable {
*/
private transient boolean closed;

/**
* Public ctor.
* @param prc The process to work with
*/
/**
* Maximum number of log lines for a stack trace
*/
public volatile static int maxStackLength = 1000;

/**
* Public ctor.
* @param prc The process to work with
*/
public VerboseProcess(final Process prc) {
this(prc, Level.INFO, Level.WARNING);
}
Expand Down Expand Up @@ -281,7 +287,7 @@ private String stdout(final boolean check) {
this.process, result.code(), result.stdout().length(),
System.currentTimeMillis() - start
);
if (check && result.code() != 0) {
if (check && (result.code() != 0)) {
throw new IllegalArgumentException(
Logger.format(
"Non-zero exit code %d: %[text]s",
Expand Down Expand Up @@ -389,9 +395,14 @@ private static void close(final Closeable res) {
* Stream monitor.
*/
private static final class Monitor implements Callable<Void> {
/**
* Stream to read.
*/

private static final String PREFIX_AT = "at ";
private static final String PREFIX_CB = "Caused by";
private static final String PREFIX_DOTS = "... ";

/**
* Stream to read.
*/
private final transient InputStream input;
/**
* Latch to count down when done.
Expand Down Expand Up @@ -420,7 +431,25 @@ private static final class Monitor implements Callable<Void> {
this.output = out;
this.level = lvl;
}
@Override

private static boolean shouldAppend(final String string) {
final String leftStrip = stripStart(string);
return leftStrip.startsWith(PREFIX_AT) || leftStrip.startsWith(PREFIX_CB) || leftStrip.startsWith(PREFIX_DOTS);
}

private static String stripStart(final String string) {
if ((string == null) || string.isEmpty()) {
return null;
}
final int stringLength = string.length();
int start = 0;
while ((start != stringLength) && Character.isWhitespace(string.charAt(start))) {
start++;
}
return string.substring(start);
}

@Override
public Void call() throws Exception {
final BufferedReader reader = new BufferedReader(
Channels.newReader(
Expand All @@ -433,6 +462,9 @@ public Void call() throws Exception {
new OutputStreamWriter(this.output, VerboseProcess.UTF_8)
);
try {
StringBuilder sb = new StringBuilder();
String previousLine = null;
int lineCount = 0;
while (true) {
if (Thread.interrupted()) {
Logger.debug(
Expand All @@ -441,16 +473,33 @@ public Void call() throws Exception {
);
break;
}
final String line = reader.readLine();
if (previousLine != null) {
sb.append(previousLine).append(System.getProperty("line.separator"));
previousLine = null;
}

final String line = reader.readLine();
if (line == null) {
if (sb.length() > 0) {
final String logText = sb.toString();
Logger.log(this.level, VerboseProcess.class, ">> %s", logText);
writer.write(logText);
}
break;
}
Logger.log(
this.level, VerboseProcess.class,
">> %s", line
);
writer.write(line);
writer.newLine();

if (shouldAppend(line) && (++lineCount < maxStackLength)) {
sb.append(line).append(System.getProperty("line.separator"));
} else {
if (sb.length() > 0) {
final String logText = sb.toString();
Logger.log(this.level, VerboseProcess.class, ">> %s", logText);
writer.write(logText);
sb = new StringBuilder();
}
lineCount = 1;
previousLine = line;
}
}
} catch (final ClosedByInterruptException ex) {
Thread.interrupted();
Expand Down Expand Up @@ -531,4 +580,4 @@ public String stderr() {
return this.err;
}
}
}
}

0 comments on commit 3bd48e8

Please sign in to comment.