1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 22:31:16 +00:00

Store log file on disk for easier access (fixes #811)

This commit is contained in:
Felix Ableitner 2016-12-16 16:06:25 +09:00
parent 92bf84915c
commit 7dde331454
2 changed files with 27 additions and 11 deletions

View file

@ -26,6 +26,7 @@ dependencies {
compile 'com.google.code.gson:gson:2.8.0' compile 'com.google.code.gson:gson:2.8.0'
compile 'org.mindrot:jbcrypt:0.3m' compile 'org.mindrot:jbcrypt:0.3m'
compile 'com.google.guava:guava:20.0' compile 'com.google.guava:guava:20.0'
compile 'com.annimon:stream:1.1.4'
androidTestCompile 'com.android.support.test:rules:0.5' androidTestCompile 'com.android.support.test:rules:0.5'
} }

View file

@ -9,6 +9,9 @@ import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.annimon.stream.Stream;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.nutomic.syncthingandroid.BuildConfig; import com.nutomic.syncthingandroid.BuildConfig;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -30,29 +33,22 @@ import eu.chainfire.libsuperuser.Shell;
public class SyncthingRunnable implements Runnable { public class SyncthingRunnable implements Runnable {
private static final String TAG = "SyncthingRunnable"; private static final String TAG = "SyncthingRunnable";
private static final String TAG_NATIVE = "SyncthingNativeCode"; private static final String TAG_NATIVE = "SyncthingNativeCode";
private static final String TAG_NICE = "SyncthingRunnableIoNice"; private static final String TAG_NICE = "SyncthingRunnableIoNice";
private static final String TAG_KILL = "SyncthingRunnableKill"; private static final String TAG_KILL = "SyncthingRunnableKill";
public static final String UNIT_TEST_PATH = "was running"; public static final String UNIT_TEST_PATH = "was running";
/** /**
* Path to the native, integrated syncthing binary, relative to the data folder * Path to the native, integrated syncthing binary, relative to the data folder
*/ */
public static final String BINARY_NAME = "lib/libsyncthing.so"; public static final String BINARY_NAME = "lib/libsyncthing.so";
private static final int LOG_FILE_MAX_LINES = 1000;
private static final AtomicReference<Process> mSyncthing = new AtomicReference<>(); private static final AtomicReference<Process> mSyncthing = new AtomicReference<>();
private final Context mContext; private final Context mContext;
private final String mSyncthingBinary; private final String mSyncthingBinary;
private String[] mCommand; private String[] mCommand;
private String mErrorLog; private String mErrorLog;
private final File mLogFile;
public enum Command { public enum Command {
generate, // Generate keys, a config file and immediately exit. generate, // Generate keys, a config file and immediately exit.
@ -68,6 +64,7 @@ public class SyncthingRunnable implements Runnable {
public SyncthingRunnable(Context context, Command command) { public SyncthingRunnable(Context context, Command command) {
mContext = context; mContext = context;
mSyncthingBinary = mContext.getApplicationInfo().dataDir + "/" + BINARY_NAME; mSyncthingBinary = mContext.getApplicationInfo().dataDir + "/" + BINARY_NAME;
mLogFile = new File(mContext.getExternalFilesDir(null), "syncthing.log");
switch (command) { switch (command) {
case generate: case generate:
mCommand = new String[]{ mSyncthingBinary, "-generate", mContext.getFilesDir().toString() }; mCommand = new String[]{ mSyncthingBinary, "-generate", mContext.getFilesDir().toString() };
@ -92,10 +89,12 @@ public class SyncthingRunnable implements Runnable {
mContext = context; mContext = context;
mSyncthingBinary = mContext.getApplicationInfo().dataDir + "/" + BINARY_NAME; mSyncthingBinary = mContext.getApplicationInfo().dataDir + "/" + BINARY_NAME;
mCommand = manualCommand; mCommand = manualCommand;
mLogFile = new File(mContext.getExternalFilesDir(null), "syncthing.log");
} }
@Override @Override
public void run() { public void run() {
trimLogFile();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
int ret; int ret;
// Make sure Syncthing is executable // Make sure Syncthing is executable
@ -327,14 +326,16 @@ public class SyncthingRunnable implements Runnable {
private Thread log(final InputStream is, final int priority, final boolean saveLog) { private Thread log(final InputStream is, final int priority, final boolean saveLog) {
Thread t = new Thread(() -> { Thread t = new Thread(() -> {
try { try {
InputStreamReader isr = new InputStreamReader(is, "UTF-8"); InputStreamReader isr = new InputStreamReader(is, Charsets.UTF_8);
BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(isr);
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
Log.println(priority, TAG_NATIVE, line); Log.println(priority, TAG_NATIVE, line);
if (saveLog) if (saveLog) {
mErrorLog += line + "\n"; mErrorLog += line + "\n";
Files.append(line + "\n", mLogFile, Charsets.UTF_8);
}
} }
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, "Failed to read Syncthing's command line output", e); Log.w(TAG, "Failed to read Syncthing's command line output", e);
@ -344,4 +345,18 @@ public class SyncthingRunnable implements Runnable {
return t; return t;
} }
/**
* Only keep last {@link #LOG_FILE_MAX_LINES} lines in log file, to avoid bloat.
*/
private void trimLogFile() {
try {
String lines = Stream.of(Files.readLines(mLogFile, Charsets.UTF_8))
.limit(LOG_FILE_MAX_LINES)
.reduce("", (a, b) -> a + b + "\n");
Files.write(lines, mLogFile, Charsets.UTF_8);
} catch (IOException e) {
Log.w(TAG, "Failed to trim log file", e);
}
}
} }