Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#734: Improve Process Result Model #773

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e25575f
Add single log list
alfeilex Nov 19, 2024
8e66739
Merge branch '#734-improve-process-result' of https://github.com/alfe…
alfeilex Nov 19, 2024
5c3cccb
Merge branch 'devonfw:main' into #734-improve-process-result
alfeilex Nov 19, 2024
4d5a556
Add LogEvent Record
alfeilex Nov 19, 2024
daf4b45
Add LogEvent List to capture Logs
alfeilex Nov 19, 2024
05879d1
Merge branch '#734-improve-process-result' of https://github.com/alfe…
alfeilex Nov 19, 2024
292aa5b
Merge branch 'devonfw:main' into #734-improve-process-result
alfeilex Nov 20, 2024
41a42ce
Merge remote-tracking branch 'origin/main' into #734-improve-process-…
alfeilex Dec 10, 2024
3da75c7
add general List of output messages and rename logevent to outputmess…
alfeilex Dec 10, 2024
540a9b0
Merge branch 'devonfw:main' into #734-improve-process-result
alfeilex Dec 10, 2024
c2b7021
refactor
alfeilex Dec 10, 2024
4530860
refactor processResultImpl in tooldummy
alfeilex Dec 10, 2024
05da168
merge
alfeilex Dec 10, 2024
88c8ef1
change List of Strings for getOutputMessages
alfeilex Dec 10, 2024
4700583
update Test with output message list
alfeilex Dec 11, 2024
f0c5b9e
add test
alfeilex Dec 11, 2024
4daec09
remove forgotten outs in ProcessContextGitMock
alfeilex Dec 11, 2024
4bbb603
add CHANGELOG.adoc
alfeilex Dec 11, 2024
af6add1
Merge branch 'main' into #734-improve-process-result
jan-vcapgemini Dec 12, 2024
8491143
Merge branch 'devonfw:main' into #734-improve-process-result
alfeilex Dec 16, 2024
d7e176e
refactor to lazy getter and remove err and out attributes
alfeilex Dec 17, 2024
ffd6ced
change from synchronized approach to ConcurrentLinkedQueue
alfeilex Dec 17, 2024
4c5f98a
refactor doLog function to output level based stdout and stderr
alfeilex Dec 17, 2024
dce5d5a
add ProcessResult to ProcessContextGitMock and remove redundant
alfeilex Dec 17, 2024
39de3a4
Merge branch 'main' into #734-improve-process-result
alfeilex Dec 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/process/LogEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.devonfw.tools.ide.process;

/**
* Represent a log event.
*
* @param error A boolean flag that indicates whether the log event represents and error or standard output
* @param message A string containing the log message
*/
public record LogEvent(boolean error, String message) {

}
alfeilex marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,15 @@ public ProcessResult run(ProcessMode processMode) {

this.processBuilder.command(args);

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());
CompletableFuture<List<String>> errFut = readInputStream(process.getErrorStream());
CompletableFuture<List<String>> outFut = readInputStream(process.getInputStream(), false, logs);
CompletableFuture<List<String>> errFut = readInputStream(process.getErrorStream(), true, logs);
out = outFut.get();
err = errFut.get();
alfeilex marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -187,10 +188,9 @@ public ProcessResult run(ProcessMode processMode) {
exitCode = process.waitFor();
}

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

performLogging(result, exitCode, interpreter);

return result;

} catch (CliProcessException | IllegalStateException e) {
Expand All @@ -214,11 +214,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<LogEvent> logs) {

return CompletableFuture.supplyAsync(() -> {

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

String line;
while ((line = br.readLine()) != null) {
synchronized (logs) {
LogEvent logEvent = new LogEvent(errorStream, line);
logs.add(logEvent);
}
alfeilex marked this conversation as resolved.
Show resolved Hide resolved
}
return br.lines().toList();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we try to read the lines again from the buffered reader after it has already been consumed to the end?
IMHO you should change the signature from CompletableFuture<List<String>> to CompletableFuture<Void> and simply return null here.

} catch (Throwable e) {
throw new RuntimeException("There was a problem while executing the program", e);
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
Loading