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

@ -23,7 +23,6 @@ import javafx.application.Platform;
import org.jackhuang.hmcl.Metadata; import org.jackhuang.hmcl.Metadata;
import org.jackhuang.hmcl.event.Event; import org.jackhuang.hmcl.event.Event;
import org.jackhuang.hmcl.event.EventManager; import org.jackhuang.hmcl.event.EventManager;
import org.jackhuang.hmcl.launch.StreamPump;
import org.jackhuang.hmcl.task.FileDownloadTask; import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.FXUtils;
@ -358,8 +357,8 @@ public final class MultiplayerManager {
this.type = type; this.type = type;
addRelatedThread(Lang.thread(this::waitFor, "CatoExitWaiter", true)); addRelatedThread(Lang.thread(this::waitFor, "CatoExitWaiter", true));
addRelatedThread(Lang.thread(new StreamPump(process.getInputStream(), this::checkCatoLog), "CatoInputStreamPump", true)); pumpInputStream(this::checkCatoLog);
addRelatedThread(Lang.thread(new StreamPump(process.getErrorStream(), this::checkCatoLog), "CatoErrorStreamPump", true)); pumpErrorStream(this::checkCatoLog);
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8)); writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8));
} }

View File

@ -17,8 +17,13 @@
*/ */
package org.jackhuang.hmcl.util.platform; 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.*;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
/** /**
* The managed process. * The managed process.
@ -36,6 +41,12 @@ public class ManagedProcess {
private final Queue<String> lines = new ConcurrentLinkedQueue<>(); private final Queue<String> lines = new ConcurrentLinkedQueue<>();
private final List<Thread> relatedThreads = new ArrayList<>(); 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. * Constructor.
* *
@ -119,6 +130,14 @@ public class ManagedProcess {
relatedThreads.add(thread); 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. * True if the managed process is running.
*/ */

View File

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