Optimization HMCLProcessListener::onLog (#1867)

This commit is contained in:
Glavo
2022-11-25 17:16:23 +08:00
committed by GitHub
parent 2e532be34c
commit e1eb40c129
6 changed files with 41 additions and 21 deletions

View File

@@ -21,7 +21,6 @@ import org.jackhuang.hmcl.auth.AuthInfo;
import org.jackhuang.hmcl.download.LibraryAnalyzer;
import org.jackhuang.hmcl.game.*;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.Log4jLevel;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter;
import org.jackhuang.hmcl.util.io.FileUtils;
@@ -646,15 +645,15 @@ public class DefaultLauncher extends Launcher {
throw new ExecutionPolicyLimitException();
}
private void startMonitors(ManagedProcess managedProcess, ProcessListener processListener, Charset encoding, boolean isDaemon) {
private static void startMonitors(ManagedProcess managedProcess, ProcessListener processListener, Charset encoding, boolean isDaemon) {
processListener.setProcess(managedProcess);
Thread stdout = Lang.thread(new StreamPump(managedProcess.getProcess().getInputStream(), it -> {
processListener.onLog(it, Optional.ofNullable(Log4jLevel.guessLevel(it)).orElse(Log4jLevel.INFO));
processListener.onLog(it, false);
managedProcess.addLine(it);
}, encoding), "stdout-pump", isDaemon);
managedProcess.addRelatedThread(stdout);
Thread stderr = Lang.thread(new StreamPump(managedProcess.getProcess().getErrorStream(), it -> {
processListener.onLog(it, Log4jLevel.ERROR);
processListener.onLog(it, true);
managedProcess.addLine(it);
}, encoding), "stderr-pump", isDaemon);
managedProcess.addRelatedThread(stderr);

View File

@@ -17,7 +17,6 @@
*/
package org.jackhuang.hmcl.launch;
import org.jackhuang.hmcl.util.Log4jLevel;
import org.jackhuang.hmcl.util.platform.ManagedProcess;
/**
@@ -40,7 +39,7 @@ public interface ProcessListener {
*
* @param log the log
*/
void onLog(String log, Log4jLevel level);
void onLog(String log, boolean isErrorStream);
/**
* Called when the game process stops.

View File

@@ -366,6 +366,13 @@ public final class Lang {
return () -> iterator;
}
public static <T, U> void forEachZipped(Iterable<T> i1, Iterable<U> i2, BiConsumer<T, U> action) {
Iterator<T> it1 = i1.iterator();
Iterator<U> it2 = i2.iterator();
while (it1.hasNext() && it2.hasNext())
action.accept(it1.next(), it2.next());
}
private static Timer timer;
public static synchronized Timer getTimer() {