Co-authored-by: 残页 <31466456+canyie@users.noreply.github.com>
This commit is contained in:
LoveSy 2022-04-09 18:56:52 +08:00 committed by GitHub
parent d1c5283661
commit 5a143c9855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 2 deletions

View File

@ -14,6 +14,7 @@ import java.nio.file.Files;
import java.nio.file.LinkOption; import java.nio.file.LinkOption;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.LinkedHashMap;
public class LogcatService implements Runnable { public class LogcatService implements Runnable {
private static final String TAG = "LSPosedLogcat"; private static final String TAG = "LSPosedLogcat";
@ -25,6 +26,28 @@ public class LogcatService implements Runnable {
private int verboseFd = -1; private int verboseFd = -1;
private Thread thread = null; 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") @SuppressLint("UnsafeDynamicallyLoadedCode")
public LogcatService() { public LogcatService() {
String libraryPath = System.getProperty("lsp.library.path"); String libraryPath = System.getProperty("lsp.library.path");
@ -89,8 +112,17 @@ public class LogcatService implements Runnable {
Log.i(TAG, "New log file: " + log); Log.i(TAG, "New log file: " + log);
ConfigFileManager.chattr0(log.toPath().getParent()); ConfigFileManager.chattr0(log.toPath().getParent());
int fd = ParcelFileDescriptor.open(log, mode).detachFd(); int fd = ParcelFileDescriptor.open(log, mode).detachFd();
if (isVerboseLog) verboseFd = fd; if (isVerboseLog) {
else modulesFd = fd; synchronized (verboseLogs) {
verboseLogs.put(log, new Object());
}
verboseFd = fd;
} else {
synchronized (moduleLogs) {
moduleLogs.put(log, new Object());
}
modulesFd = fd;
}
return fd; return fd;
} catch (IOException e) { } catch (IOException e) {
if (isVerboseLog) verboseFd = -1; if (isVerboseLog) verboseFd = -1;