fix(auto-installing): catch logs of forge-installer. Closes #1420.

This commit is contained in:
huanghongxun
2022-05-22 14:24:25 +08:00
parent fd098a65c5
commit 1aa3860706
3 changed files with 32 additions and 9 deletions

View File

@@ -17,8 +17,13 @@
*/
package org.jackhuang.hmcl.util.platform;
import org.jackhuang.hmcl.launch.StreamPump;
import org.jackhuang.hmcl.util.Lang;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
/**
* The managed process.
@@ -36,6 +41,12 @@ public class ManagedProcess {
private final Queue<String> lines = new ConcurrentLinkedQueue<>();
private final List<Thread> relatedThreads = new ArrayList<>();
public ManagedProcess(ProcessBuilder processBuilder) throws IOException {
this.process = processBuilder.start();
this.commands = processBuilder.command();
this.classpath = null;
}
/**
* Constructor.
*
@@ -119,6 +130,14 @@ public class ManagedProcess {
relatedThreads.add(thread);
}
public synchronized void pumpInputStream(Consumer<String> onLogLine) {
addRelatedThread(Lang.thread(new StreamPump(process.getInputStream(), onLogLine), "ProcessInputStreamPump", true));
}
public synchronized void pumpErrorStream(Consumer<String> onLogLine) {
addRelatedThread(Lang.thread(new StreamPump(process.getErrorStream(), onLogLine), "ProcessErrorStreamPump", true));
}
/**
* True if the managed process is running.
*/

View File

@@ -18,10 +18,11 @@
package org.jackhuang.hmcl.util.platform;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.util.Arrays;
import java.util.List;
import static org.jackhuang.hmcl.util.Logging.LOG;
public final class SystemUtils {
private SystemUtils() {}
@@ -30,10 +31,14 @@ public final class SystemUtils {
}
public static int callExternalProcess(List<String> command) throws IOException, InterruptedException {
Process process = new ProcessBuilder(command)
.redirectOutput(Redirect.INHERIT)
.redirectError(Redirect.INHERIT)
.start();
return process.waitFor();
ManagedProcess managedProcess = new ManagedProcess(new ProcessBuilder(command));
managedProcess.pumpInputStream(SystemUtils::onLogLine);
managedProcess.pumpErrorStream(SystemUtils::onLogLine);
return managedProcess.getProcess().waitFor();
}
private static void onLogLine(String log) {
LOG.info(log);
}
}