Skip to content
Open
Changes from all commits
Commits
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
17 changes: 13 additions & 4 deletions HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,8 @@ private final class HMCLProcessListener implements ProcessListener {
private final CountDownLatch launchingLatch;
private final String forbiddenAccessToken;
private Thread submitLogThread;
private LinkedBlockingQueue<Log> logBuffer;
/// Pending logs for the log window.
private final BlockingDeque<Log> logBuffer = new LinkedBlockingDeque<>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

你不都改成加锁了吗?为什么还要用 LinkedBlockingDeque


public HMCLProcessListener(HMCLGameRepository repository, GameInstanceManifest manifest, AuthInfo authInfo, LaunchOptions launchOptions, CountDownLatch launchingLatch, boolean detectWindow) {
this.repository = repository;
Expand Down Expand Up @@ -881,7 +882,6 @@ public void setProcess(ManagedProcess process) {
logWindowLatch.countDown();
});

logBuffer = new LinkedBlockingQueue<>();
submitLogThread = Lang.thread(new Runnable() {
private final ArrayList<Log> currentLogs = new ArrayList<>();
private final Semaphore semaphore = new Semaphore(0);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<WeakReference<ManagedProcess>> PROCESSES = new ConcurrentLinkedQueue<>();
Expand Down