Skip to content

Commit

Permalink
Add single log list
Browse files Browse the repository at this point in the history
  • Loading branch information
alfeilex committed Nov 19, 2024
1 parent 2b473ff commit e25575f
Showing 1 changed file with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public ProcessContext withPathEntry(Path path) {
@Override
public ProcessResult run(ProcessMode processMode) {

final String ERROR_PREFIX = "ERROR: ";
final String OUTPUT_PREFIX = "OUTPUT: ";

if (processMode == ProcessMode.DEFAULT) {
this.processBuilder.redirectOutput(Redirect.INHERIT).redirectError(Redirect.INHERIT);
}
Expand Down Expand Up @@ -167,16 +170,16 @@ public ProcessResult run(ProcessMode processMode) {

this.processBuilder.command(args);

List<String> out = null;
List<String> logs = new ArrayList<>();
List<String> err = null;

Process process = this.processBuilder.start();

if (processMode == ProcessMode.DEFAULT_CAPTURE) {
CompletableFuture<List<String>> outFut = readInputStream(process.getInputStream());
CompletableFuture<List<String>> errFut = readInputStream(process.getErrorStream());
out = outFut.get();
err = errFut.get();
CompletableFuture<List<String>> outFut = readInputStream(process.getInputStream(), false, logs);
CompletableFuture<List<String>> errFut = readInputStream(process.getErrorStream(), true, logs);
outFut.get();
errFut.get();
}

int exitCode;
Expand All @@ -187,10 +190,9 @@ public ProcessResult run(ProcessMode processMode) {
exitCode = process.waitFor();
}

ProcessResult result = new ProcessResultImpl(exitCode, out, err);
ProcessResult result = new ProcessResultImpl(exitCode, logs, null);

performLogging(result, exitCode, interpreter);

return result;

} catch (CliProcessException | IllegalStateException e) {
Expand All @@ -214,11 +216,19 @@ public ProcessResult run(ProcessMode processMode) {
* @param is {@link InputStream}.
* @return {@link CompletableFuture}.
*/
private static CompletableFuture<List<String>> readInputStream(InputStream is) {
private static CompletableFuture<List<String>> readInputStream(InputStream is, boolean errorStream, List<String> logs) {

return CompletableFuture.supplyAsync(() -> {

try (InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr)) {

String prefix = errorStream ? "- " : "+ ";
String line;
while ((line = br.readLine()) != null) {
synchronized (logs) {
logs.add(prefix + line);
}
}
return br.lines().toList();
} catch (Throwable e) {
throw new RuntimeException("There was a problem while executing the program", e);
Expand Down

0 comments on commit e25575f

Please sign in to comment.