From 5a143c9855a7f1c9bcfb51a62b63f7627f867897 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Sat, 9 Apr 2022 18:56:52 +0800 Subject: [PATCH] Log LRU (#1822) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 残页 <31466456+canyie@users.noreply.github.com> --- .../lsposed/lspd/service/LogcatService.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/daemon/src/main/java/org/lsposed/lspd/service/LogcatService.java b/daemon/src/main/java/org/lsposed/lspd/service/LogcatService.java index 84eaff6a3..6f6157987 100644 --- a/daemon/src/main/java/org/lsposed/lspd/service/LogcatService.java +++ b/daemon/src/main/java/org/lsposed/lspd/service/LogcatService.java @@ -14,6 +14,7 @@ import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.LinkedHashMap; public class LogcatService implements Runnable { private static final String TAG = "LSPosedLogcat"; @@ -25,6 +26,28 @@ public class LogcatService implements Runnable { private int verboseFd = -1; private Thread thread = null; + static class LogLRU extends LinkedHashMap { + private static final int MAX_ENTRIES = 10; + + public LogLRU() { + super(MAX_ENTRIES, 1f, false); + } + + @Override + synchronized protected boolean removeEldestEntry(Entry eldest) { + if (size() > MAX_ENTRIES && eldest.getKey().delete()) { + Log.d(TAG, "Deleted old log " + eldest.getKey().getAbsolutePath()); + return true; + } + return false; + } + } + + @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") + private final LinkedHashMap moduleLogs = new LogLRU(); + @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") + private final LinkedHashMap verboseLogs = new LogLRU(); + @SuppressLint("UnsafeDynamicallyLoadedCode") public LogcatService() { String libraryPath = System.getProperty("lsp.library.path"); @@ -89,8 +112,17 @@ private int refreshFd(boolean isVerboseLog) { Log.i(TAG, "New log file: " + log); ConfigFileManager.chattr0(log.toPath().getParent()); int fd = ParcelFileDescriptor.open(log, mode).detachFd(); - if (isVerboseLog) verboseFd = fd; - else modulesFd = fd; + if (isVerboseLog) { + synchronized (verboseLogs) { + verboseLogs.put(log, new Object()); + } + verboseFd = fd; + } else { + synchronized (moduleLogs) { + moduleLogs.put(log, new Object()); + } + modulesFd = fd; + } return fd; } catch (IOException e) { if (isVerboseLog) verboseFd = -1;