Skip to content

Commit

Permalink
#4 fixed, stderr is logged
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Oct 27, 2013
1 parent 14b7ebf commit fb74922
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 35 deletions.
16 changes: 13 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<parent>
<groupId>com.jcabi</groupId>
<artifactId>parent</artifactId>
<version>0.11</version>
<version>0.12</version>
</parent>
<artifactId>jcabi-log</artifactId>
<version>1.0-SNAPSHOT</version>
Expand All @@ -54,8 +54,8 @@
</scm>
<distributionManagement>
<site>
<id>www.jcabi.com</id>
<url>s3://www.jcabi.com/jcabi-log</url>
<id>github-pages</id>
<url>http://log.jcabi.com/</url>
</site>
</distributionManagement>
<dependencies>
Expand Down Expand Up @@ -103,4 +103,14 @@
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- https://github.com/github/maven-plugins/issues/54 -->
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.8</version>
</plugin>
</plugins>
</build>
</project>
104 changes: 72 additions & 32 deletions src/main/java/com/jcabi/log/VerboseProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import javax.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.ToString;
Expand Down Expand Up @@ -87,7 +89,7 @@ public VerboseProcess(@NotNull final ProcessBuilder builder) {
try {
this.process = builder.start();
this.process.getOutputStream().close();
} catch (java.io.IOException ex) {
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
Expand Down Expand Up @@ -132,7 +134,7 @@ public String stdoutQuietly() {
*/
private String stdout(final boolean check) {
final long start = System.currentTimeMillis();
String stdout;
final String stdout;
try {
stdout = this.waitFor();
} catch (InterruptedException ex) {
Expand All @@ -159,30 +161,84 @@ private String stdout(final boolean check) {
* @return Stdout produced by the process
* @throws InterruptedException If interrupted in between
*/
@SuppressWarnings("PMD.DoNotUseThreads")
private String waitFor() throws InterruptedException {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(
final CountDownLatch done = new CountDownLatch(2);
final StringBuffer stdout = new StringBuffer(0);
final StringBuffer stderr = new StringBuffer(0);
Logger.debug(
this,
"#waitFor(): waiting for stdout of %s in %s...",
this.process,
this.monitor(
this.process.getInputStream(),
Charset.forName(CharEncoding.UTF_8)
done, stdout, Level.INFO
)
);
Logger.debug(
this,
"#waitFor(): waiting for stderr of %s in %s...",
this.process,
this.monitor(
this.process.getErrorStream(),
done, stderr, Level.WARNING
)
);
final CountDownLatch done = new CountDownLatch(1);
final StringBuffer stdout = new StringBuffer();
try {
this.process.waitFor();
} finally {
Logger.debug(this, "#waitFor(): process finished", this.process);
done.await(2L, TimeUnit.SECONDS);
}
return stdout.toString();
}

/**
* Monitor this input stream.
* @param stream Stream to monitor
* @param done Count down latch to signal when done
* @param buffer Buffer to write to
* @param level Logging level
* @return Thread which is monitoring
* @checkstyle ParameterNumber (5 lines)
*/
@SuppressWarnings("PMD.DoNotUseThreads")
private Thread monitor(final InputStream stream, final CountDownLatch done,
final StringBuffer buffer, final Level level) {
final Thread thread = new Thread(
new VerboseRunnable(
// @checkstyle AnonInnerLength (100 lines)
new Callable<Void>() {
@Override
public Void call() throws Exception {
while (true) {
final String line = reader.readLine();
if (line == null) {
break;
final BufferedReader reader = new BufferedReader(
new InputStreamReader(
stream,
Charset.forName(CharEncoding.UTF_8)
)
);
try {
while (true) {
final String line = reader.readLine();
if (line == null) {
break;
}
Logger.log(
level, VerboseProcess.class,
">> %s", line
);
buffer.append(line);
}
done.countDown();
} finally {
try {
reader.close();
} catch (IOException ex) {
Logger.error(
this,
"failed to close reader: %[exception]s", ex
);
}
Logger.info(VerboseProcess.class, ">> %s", line);
stdout.append(line);
}
done.countDown();
return null;
}
},
Expand All @@ -192,23 +248,7 @@ public Void call() throws Exception {
thread.setName("VerboseProcess");
thread.setDaemon(true);
thread.start();
Logger.debug(
this,
"#waitFor(): waiting for stdout of %s in %s...",
this.process, thread
);
try {
this.process.waitFor();
} finally {
Logger.debug(this, "#waitFor(): process finished", this.process);
done.await(1, TimeUnit.SECONDS);
try {
reader.close();
} catch (IOException ex) {
Logger.error(this, "failed to close reader: %[exception]s", ex);
}
}
return stdout.toString();
return thread;
}

}

0 comments on commit fb74922

Please sign in to comment.