diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java index ea79242cf1..e10fcedd59 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java @@ -848,7 +848,8 @@ private final class HMCLProcessListener implements ProcessListener { private final CountDownLatch launchingLatch; private final String forbiddenAccessToken; private Thread submitLogThread; - private LinkedBlockingQueue logBuffer; + /// Pending logs for the log window. + private final BlockingDeque logBuffer = new LinkedBlockingDeque<>(); public HMCLProcessListener(HMCLGameRepository repository, GameInstanceManifest manifest, AuthInfo authInfo, LaunchOptions launchOptions, CountDownLatch launchingLatch, boolean detectWindow) { this.repository = repository; @@ -881,7 +882,6 @@ public void setProcess(ManagedProcess process) { logWindowLatch.countDown(); }); - logBuffer = new LinkedBlockingQueue<>(); submitLogThread = Lang.thread(new Runnable() { private final ArrayList currentLogs = new ArrayList<>(); private final Semaphore semaphore = new Semaphore(0); @@ -980,7 +980,7 @@ public void onLog(String log, boolean isErrorStream) { if (showLogs) { if (level == null) level = Lang.requireNonNullElse(Log4jLevel.guessLevel(log), Log4jLevel.INFO); - logBuffer.add(new Log(log, level)); + enqueueLog(new Log(log, level)); } else { lock.lock(); try { @@ -1011,7 +1011,7 @@ public void onLog(String log, boolean isErrorStream) { @Override public void onExit(int exitCode, ExitType exitType) { if (showLogs) { - logBuffer.add(new Log(String.format("[%s] [HMCL ProcessListener] Minecraft exit with code %d(0x%x), type is %s.", TIME_FORMATTER.format(Instant.now()), exitCode, exitCode, exitType), Log4jLevel.INFO)); + enqueueLog(new Log(String.format("[%s] [HMCL ProcessListener] Minecraft exit with code %d(0x%x), type is %s.", TIME_FORMATTER.format(Instant.now()), exitCode, exitCode, exitType), Log4jLevel.INFO)); submitLogThread.interrupt(); try { submitLogThread.join(); @@ -1044,6 +1044,15 @@ public void onExit(int exitCode, ExitType exitType) { checkExit(); } + /// Adds a log to the display queue, retaining at most the configured number of lines. + private void enqueueLog(Log log) { + synchronized (logBuffer) { + while (logBuffer.size() >= Log.getLogLines()) + logBuffer.pollFirst(); + logBuffer.addLast(log); + } + } + } private static final Queue> PROCESSES = new ConcurrentLinkedQueue<>();