Skip to content

Commit

Permalink
Add LogEvent List to capture Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
alfeilex committed Nov 19, 2024
1 parent 4d5a556 commit daf4b45
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ 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 @@ -170,16 +167,17 @@ public ProcessResult run(ProcessMode processMode) {

this.processBuilder.command(args);

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

Process process = this.processBuilder.start();

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

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

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

performLogging(result, exitCode, interpreter);
return result;
Expand All @@ -216,17 +214,17 @@ public ProcessResult run(ProcessMode processMode) {
* @param is {@link InputStream}.
* @return {@link CompletableFuture}.
*/
private static CompletableFuture<List<String>> readInputStream(InputStream is, boolean errorStream, List<String> logs) {
private static CompletableFuture<List<String>> readInputStream(InputStream is, boolean errorStream, List<LogEvent> 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);
LogEvent logEvent = new LogEvent(errorStream, line);
logs.add(logEvent);
}
}
return br.lines().toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ public class ProcessResultImpl implements ProcessResult {

private final List<String> err;

private final List<LogEvent> logEvents;

/**
* The constructor.
*
* @param exitCode the {@link #getExitCode() exit code}.
* @param out the {@link #getOut() out}.
* @param err the {@link #getErr() err}.
* @param logEvents the {@link #getLogEvents()} () logEvents}.
*/
public ProcessResultImpl(int exitCode, List<String> out, List<String> err) {
public ProcessResultImpl(int exitCode, List<String> out, List<String> err, List<LogEvent> logEvents) {

super();
this.exitCode = exitCode;
this.out = Objects.requireNonNullElse(out, Collections.emptyList());
this.err = Objects.requireNonNullElse(err, Collections.emptyList());
this.logEvents = Objects.requireNonNullElse(logEvents, Collections.emptyList());
}

@Override
Expand All @@ -51,6 +55,11 @@ public List<String> getErr() {
return this.err;
}

public List<LogEvent> getLogEvents() {

return this.logEvents;
}

@Override
public void log(IdeLogLevel level, IdeContext context) {
log(level, context, level);
Expand Down

0 comments on commit daf4b45

Please sign in to comment.