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

Add log to exception thrown on syncthing error code.

This commit is contained in:
Felix Ableitner 2014-05-18 20:27:31 +02:00
parent 2cd100e4bb
commit 22f44dffa5

View file

@ -73,15 +73,34 @@ public class SyncthingService extends Service {
return START_STICKY; return START_STICKY;
} }
/**
* Thrown when execution of the native syncthing binary returns an error.
* Prints the syncthing log.
*/
public static class NativeExecutionException extends RuntimeException {
private final String mLog;
public NativeExecutionException(String message, String log) {
super(message);
mLog = log;
}
@Override
public String getMessage() {
return super.getMessage() + "\n" + mLog;
}
}
/** /**
* Runs the syncthing binary from command line, and prints its output to logcat. * Runs the syncthing binary from command line, and prints its output to logcat.
*/ */
private class NativeSyncthingRunnable implements Runnable { private class NativeSyncthingRunnable implements Runnable {
@Override @Override
public void run() { public void run() throws NativeExecutionException {
DataOutputStream dos = null; DataOutputStream dos = null;
InputStreamReader isr = null; InputStreamReader isr = null;
int ret = 1; int ret = 1;
String log = "";
try { try {
Process p = Runtime.getRuntime().exec("sh"); Process p = Runtime.getRuntime().exec("sh");
dos = new DataOutputStream(p.getOutputStream()); dos = new DataOutputStream(p.getOutputStream());
@ -102,11 +121,13 @@ public class SyncthingService extends Service {
BufferedReader stdout = new BufferedReader(isr); BufferedReader stdout = new BufferedReader(isr);
String line; String line;
while((line = stdout.readLine()) != null) { while((line = stdout.readLine()) != null) {
log += "stderr: " + line + "\n";
Log.w(TAG, "stderr: " + line); Log.w(TAG, "stderr: " + line);
} }
isr = new InputStreamReader(p.getErrorStream()); isr = new InputStreamReader(p.getErrorStream());
BufferedReader stderr = new BufferedReader(isr); BufferedReader stderr = new BufferedReader(isr);
while((line = stderr.readLine()) != null) { while((line = stderr.readLine()) != null) {
log += "stdout: " + line + "\n";
Log.i(TAG, "stdout: " + line); Log.i(TAG, "stdout: " + line);
} }
} }
@ -117,10 +138,11 @@ public class SyncthingService extends Service {
Log.e(TAG, "Failed to execute syncthing binary or read output", e); Log.e(TAG, "Failed to execute syncthing binary or read output", e);
} }
finally { finally {
if (ret != 0) { if (ret != 1) {
stopSelf(); stopSelf();
throw new RuntimeException("Syncthing binary returned error code " + // Include the log for Play Store crash reports.
Integer.toString(ret)); throw new NativeExecutionException("Syncthing binary returned error code " +
Integer.toString(ret), log);
} }
try { try {
dos.close(); dos.close();