Skip to content

Commit

Permalink
Merge pull request #310 from ascopes/task/improve-process-logging
Browse files Browse the repository at this point in the history
Improve process logging
  • Loading branch information
ascopes authored Jul 25, 2024
2 parents 2d3d655 + a90d1c6 commit d42836a
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
public final class CommandLineExecutor {

private static final Logger log = LoggerFactory.getLogger(CommandLineExecutor.class);

private final HostSystem hostSystem;

@Inject
Expand All @@ -48,12 +47,7 @@ public CommandLineExecutor(HostSystem hostSystem) {
}

public boolean execute(List<String> args) throws IOException {
log.info(
"Calling protoc with the following command line:\n{}",
hostSystem.isProbablyWindows()
? Shlex.quoteBatchArgs(args)
: Shlex.quoteShellArgs(args)
);
reportInvocation(args);

var procBuilder = new ProcessBuilder(args);
procBuilder.environment().putAll(System.getenv());
Expand All @@ -62,18 +56,29 @@ public boolean execute(List<String> args) throws IOException {
return run(procBuilder);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
var newEx = new InterruptedIOException("Compilation was interrupted");
var newEx = new InterruptedIOException("Execution was interrupted");
newEx.initCause(ex);
throw newEx;
}
}

private void reportInvocation(List<String> args) {
var renderedCommandLine = hostSystem.isProbablyWindows()
? Shlex.quoteBatchArgs(args)
: Shlex.quoteShellArgs(args);
log.info("Calling protoc with the following invocation:");
renderedCommandLine.lines()
.map(" "::concat)
.map(String::stripTrailing)
.forEach(log::info);
}

private boolean run(ProcessBuilder procBuilder) throws InterruptedException, IOException {
var startTimeNs = System.nanoTime();
var proc = procBuilder.start();

var stdoutThread = redirectOutput(proc.getInputStream(), line -> log.info("[OUT] {}", line));
var stderrThread = redirectOutput(proc.getErrorStream(), line -> log.warn("[ERR] {}", line));
var stdoutThread = redirectOutput(proc.getInputStream(), log::info);
var stderrThread = redirectOutput(proc.getErrorStream(), log::warn);

var exitCode = proc.waitFor();
var elapsedTimeMs = (System.nanoTime() - startTimeNs) / 1_000_000L;
Expand All @@ -83,10 +88,10 @@ private boolean run(ProcessBuilder procBuilder) throws InterruptedException, IOE
stderrThread.join();

if (exitCode == 0) {
log.info("Protoc returned exit code 0 (success) after {}ms", elapsedTimeMs);
log.info("protoc returned exit code 0 (success) after {}ms", elapsedTimeMs);
return true;
} else {
log.error("Protoc returned exit code {} (error) after {}ms", exitCode, elapsedTimeMs);
log.error("protoc returned exit code {} (error) after {}ms", exitCode, elapsedTimeMs);
return false;
}
}
Expand Down

0 comments on commit d42836a

Please sign in to comment.