Load the current and previous session's logs.

This commit is contained in:
akwizgran
2020-07-03 11:10:28 +01:00
parent 2d88819e80
commit a63619ab47
3 changed files with 46 additions and 26 deletions

View File

@@ -95,16 +95,11 @@ public class BriarReportPrimer implements ReportPrimer {
} }
customData.put("Log", sb.toString()); customData.put("Log", sb.toString());
sb = new StringBuilder(); customData.put("Persisted log",
try { getPersistedLog(ctx, logManager, false));
File logDir = ctx.getDir("log", MODE_PRIVATE);
for (String line : logManager.getPersistedLog(logDir)) { customData.put("Previous persisted log",
sb.append(line).append('\n'); getPersistedLog(ctx, logManager, true));
}
} catch (IOException e) {
sb.append("Could not recover persisted log: ").append(e);
}
customData.put("Persisted log", sb.toString());
// System memory // System memory
Object o = ctx.getSystemService(ACTIVITY_SERVICE); Object o = ctx.getSystemService(ACTIVITY_SERVICE);
@@ -270,6 +265,20 @@ public class BriarReportPrimer implements ReportPrimer {
return unmodifiableMap(customData); return unmodifiableMap(customData);
} }
private String getPersistedLog(Context ctx,
PersistentLogManager logManager, boolean old) {
File logDir = ctx.getDir("log", MODE_PRIVATE);
StringBuilder sb = new StringBuilder();
try {
for (String line : logManager.getPersistedLog(logDir, old)) {
sb.append(line).append('\n');
}
} catch (IOException e) {
sb.append("Could not recover persisted log: ").append(e);
}
return sb.toString();
}
} }
private static class SingleShotAndroidExecutor extends Thread { private static class SingleShotAndroidExecutor extends Thread {

View File

@@ -5,7 +5,7 @@ import org.briarproject.bramble.api.settings.Settings;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.List;
import java.util.logging.Handler; import java.util.logging.Handler;
@NotNullByDefault @NotNullByDefault
@@ -32,6 +32,9 @@ public interface PersistentLogManager {
/** /**
* Loads and returns the persistent log entries stored in the given * Loads and returns the persistent log entries stored in the given
* directory, or an empty list if no log entries are found. * directory, or an empty list if no log entries are found.
*
* @param old True if the previous session's log should be loaded, or false
* if the current session's log should be loaded
*/ */
Collection<String> getPersistedLog(File dir) throws IOException; List<String> getPersistedLog(File dir, boolean old) throws IOException;
} }

View File

@@ -22,9 +22,8 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Collection;
import java.util.Deque;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@@ -134,22 +133,31 @@ class PersistentLogManagerImpl implements PersistentLogManager,
} }
@Override @Override
public Collection<String> getPersistedLog(File dir) throws IOException { public List<String> getPersistedLog(File dir, boolean old)
SecretKey oldLogKey = this.oldLogKey; throws IOException {
if (oldLogKey == null) { if (old) {
LOG.info("Old log key has not been loaded"); SecretKey oldLogKey = this.oldLogKey;
return emptyList(); if (oldLogKey == null) {
LOG.info("Old log key has not been loaded");
return emptyList();
}
return getPersistedLog(new File(dir, OLD_LOG_FILE), oldLogKey);
} else {
return getPersistedLog(new File(dir, LOG_FILE), logKey);
} }
File oldLogFile = new File(dir, OLD_LOG_FILE); }
if (oldLogFile.exists()) {
LOG.info("Reading old log file"); private List<String> getPersistedLog(File logFile, SecretKey key)
Deque<String> lines = new LinkedList<>(); throws IOException {
if (logFile.exists()) {
LOG.info("Reading log file");
LinkedList<String> lines = new LinkedList<>();
int numLines = 0; int numLines = 0;
InputStream in = new FileInputStream(oldLogFile); InputStream in = new FileInputStream(logFile);
//noinspection TryFinallyCanBeTryWithResources //noinspection TryFinallyCanBeTryWithResources
try { try {
InputStream reader = streamReaderFactory InputStream reader = streamReaderFactory
.createLogStreamReader(in, oldLogKey); .createLogStreamReader(in, key);
Scanner s = new Scanner(reader); Scanner s = new Scanner(reader);
while (s.hasNextLine()) { while (s.hasNextLine()) {
lines.add(s.nextLine()); lines.add(s.nextLine());
@@ -162,7 +170,7 @@ class PersistentLogManagerImpl implements PersistentLogManager,
in.close(); in.close();
} }
} else { } else {
LOG.info("Old log file does not exist"); LOG.info("Log file does not exist");
return emptyList(); return emptyList();
} }
} }