Skip to content

Commit

Permalink
Log LRU (#1822)
Browse files Browse the repository at this point in the history
Co-authored-by: 残页 <31466456+canyie@users.noreply.github.com>
  • Loading branch information
yujincheng08 and canyie authored Apr 9, 2022
1 parent d1c5283 commit 5a143c9
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions daemon/src/main/java/org/lsposed/lspd/service/LogcatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -25,6 +26,28 @@ public class LogcatService implements Runnable {
private int verboseFd = -1;
private Thread thread = null;

static class LogLRU extends LinkedHashMap<File, Object> {
private static final int MAX_ENTRIES = 10;

public LogLRU() {
super(MAX_ENTRIES, 1f, false);
}

@Override
synchronized protected boolean removeEldestEntry(Entry<File, Object> 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<File, Object> moduleLogs = new LogLRU();
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private final LinkedHashMap<File, Object> verboseLogs = new LogLRU();

@SuppressLint("UnsafeDynamicallyLoadedCode")
public LogcatService() {
String libraryPath = System.getProperty("lsp.library.path");
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5a143c9

Please sign in to comment.