From daf4b45d569d811edbc6a2f02ae871191051a531 Mon Sep 17 00:00:00 2001 From: alfeilex <101652401+alfeilex@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:15:14 +0100 Subject: [PATCH] Add LogEvent List to capture Logs --- .../tools/ide/process/ProcessContextImpl.java | 18 ++++++++---------- .../tools/ide/process/ProcessResultImpl.java | 11 ++++++++++- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java index 07f360065..ed6a7c90f 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java @@ -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); } @@ -170,7 +167,8 @@ public ProcessResult run(ProcessMode processMode) { this.processBuilder.command(args); - List logs = new ArrayList<>(); + List logs = new ArrayList<>(); + List out = null; List err = null; Process process = this.processBuilder.start(); @@ -178,8 +176,8 @@ public ProcessResult run(ProcessMode processMode) { if (processMode == ProcessMode.DEFAULT_CAPTURE) { CompletableFuture> outFut = readInputStream(process.getInputStream(), false, logs); CompletableFuture> errFut = readInputStream(process.getErrorStream(), true, logs); - outFut.get(); - errFut.get(); + out = outFut.get(); + err = errFut.get(); } int exitCode; @@ -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; @@ -216,17 +214,17 @@ public ProcessResult run(ProcessMode processMode) { * @param is {@link InputStream}. * @return {@link CompletableFuture}. */ - private static CompletableFuture> readInputStream(InputStream is, boolean errorStream, List logs) { + private static CompletableFuture> readInputStream(InputStream is, boolean errorStream, List 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(); diff --git a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessResultImpl.java b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessResultImpl.java index c4786d54b..980ce6ae2 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessResultImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessResultImpl.java @@ -18,19 +18,23 @@ public class ProcessResultImpl implements ProcessResult { private final List err; + private final List 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 out, List err) { + public ProcessResultImpl(int exitCode, List out, List err, List 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 @@ -51,6 +55,11 @@ public List getErr() { return this.err; } + public List getLogEvents() { + + return this.logEvents; + } + @Override public void log(IdeLogLevel level, IdeContext context) { log(level, context, level);