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

Additional info level logging.

This commit is contained in:
Felix Ableitner 2014-05-27 01:24:37 +02:00
parent a8b84a21e5
commit fa749da4a6
4 changed files with 35 additions and 23 deletions

View file

@ -15,30 +15,27 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
/** /**
* Performs a GET request with no parameters to the URL in uri[0] and * Performs a GET request with no parameters to the URL in uri[0] with the path in uri[1] and
* returns the result as a String. * returns the result as a String.
*/ */
public class GetTask extends AsyncTask<String, Void, String> { public class GetTask extends AsyncTask<String, Void, String> {
private static final String TAG = "GetTask"; private static final String TAG = "GetTask";
/**
* URI to call to get version name.
*/
public static final String URI_VERSION = "/rest/version"; public static final String URI_VERSION = "/rest/version";
@Override @Override
protected String doInBackground(String... uri) { protected String doInBackground(String... uri) {
String fullUri = uri[0] + uri[1];
Log.i(TAG, "Sending GET request to " + fullUri);
HttpClient httpclient = new DefaultHttpClient(); HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(uri[0] + uri[1]); HttpGet get = new HttpGet(fullUri);
String responseString = null; String responseString = null;
try { try {
HttpResponse response = httpclient.execute(get); HttpResponse response = httpclient.execute(get);
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
if (entity != null) { if (entity != null) {
// A Simple JSON Response Read
InputStream is = entity.getContent(); InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is)); BufferedReader br = new BufferedReader(new InputStreamReader(is));
@ -52,7 +49,7 @@ public class GetTask extends AsyncTask<String, Void, String> {
} }
} }
catch (IOException e) { catch (IOException e) {
Log.w(TAG, "Failed to call Rest API at " + uri[0], e); Log.w(TAG, "Failed to call Rest API at " + fullUri, e);
} }
return null; return null;
} }

View file

@ -11,27 +11,26 @@ import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException; import java.io.IOException;
/** /**
* Performs a POST request with no parameters to the URL in uri[0]. * Performs a POST request with no parameters to the URL in uri[0] with the path in uri[1].
*/ */
public class PostTask extends AsyncTask<String, Void, Void> { public class PostTask extends AsyncTask<String, Void, Void> {
private static final String TAG = "PostTask"; private static final String TAG = "PostTask";
/**
* URI to call for shutdown.
*/
public static final String URI_SHUTDOWN = "/rest/shutdown"; public static final String URI_SHUTDOWN = "/rest/shutdown";
@Override @Override
protected Void doInBackground(String... uri) { protected Void doInBackground(String... uri) {
String fullUri = uri[0] + uri[1];
Log.i(TAG, "Sending POST request to " + fullUri);
HttpClient httpclient = new DefaultHttpClient(); HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(uri[0] + uri[1]); HttpPost post = new HttpPost(fullUri);
String responseString = null; String responseString = null;
try { try {
HttpResponse response = httpclient.execute(post); HttpResponse response = httpclient.execute(post);
} }
catch (IOException e) { catch (IOException e) {
Log.w(TAG, "Failed to call Rest API at " + uri[0], e); Log.w(TAG, "Failed to call Rest API at " + fullUri, e);
} }
return null; return null;
} }

View file

@ -1,6 +1,7 @@
package com.nutomic.syncthingandroid.syncthing; package com.nutomic.syncthingandroid.syncthing;
import android.content.Context; import android.content.Context;
import android.util.Log;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
@ -9,6 +10,8 @@ import com.nutomic.syncthingandroid.R;
*/ */
public class RestApi implements SyncthingService.OnWebGuiAvailableListener { public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
private static final String TAG = "RestApi";
private Context mContext; private Context mContext;
private String mVersion; private String mVersion;
@ -31,10 +34,9 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
public void onWebGuiAvailable() { public void onWebGuiAvailable() {
new GetTask() { new GetTask() {
@Override @Override
protected void onPostExecute(String versionName) { protected void onPostExecute(String version) {
mVersion = (versionName != null) mVersion = version;
? versionName Log.i(TAG, "Syncthing version is " + mVersion);
: mContext.getString(R.string.syncthing_version_error);
} }
}.execute(mUrl, GetTask.URI_VERSION); }.execute(mUrl, GetTask.URI_VERSION);
} }
@ -43,7 +45,9 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
* Returns the version name, or a (text) error message on failure. * Returns the version name, or a (text) error message on failure.
*/ */
public String getVersion() { public String getVersion() {
return mVersion; return (mVersion != null)
? mVersion
: mContext.getString(R.string.syncthing_version_error);
} }
/** /**

View file

@ -201,6 +201,7 @@ public class SyncthingService extends Service {
@Override @Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
Log.i(TAG, "Web GUI has come online at " + mApi.getUrl());
mIsWebGuiAvailable = true; mIsWebGuiAvailable = true;
for (OnWebGuiAvailableListener listener : mOnWebGuiAvailableListeners) { for (OnWebGuiAvailableListener listener : mOnWebGuiAvailableListeners) {
listener.onWebGuiAvailable(); listener.onWebGuiAvailable();
@ -229,6 +230,8 @@ public class SyncthingService extends Service {
@Override @Override
public void run() { public void run() {
if (isFirstStart(SyncthingService.this)) { if (isFirstStart(SyncthingService.this)) {
Log.i(TAG, "App started for the first time. " +
"Copying default config, keys will be generated automatically");
copyDefaultConfig(); copyDefaultConfig();
} }
updateConfig(); updateConfig();
@ -240,14 +243,19 @@ public class SyncthingService extends Service {
Element options = (Element) Element options = (Element)
d.getDocumentElement().getElementsByTagName("gui").item(0); d.getDocumentElement().getElementsByTagName("gui").item(0);
syncthingUrl = options.getElementsByTagName("address").item(0).getTextContent(); syncthingUrl = options.getElementsByTagName("address").item(0).getTextContent();
} catch (SAXException e) { }
catch (SAXException e) {
throw new RuntimeException("Failed to read gui url, aborting", e); throw new RuntimeException("Failed to read gui url, aborting", e);
} catch (ParserConfigurationException e) { }
catch (ParserConfigurationException e) {
throw new RuntimeException("Failed to read gui url, aborting", e); throw new RuntimeException("Failed to read gui url, aborting", e);
} catch (IOException e) { }
catch (IOException e) {
throw new RuntimeException("Failed to read gui url, aborting", e); throw new RuntimeException("Failed to read gui url, aborting", e);
} finally { }
finally {
mApi = new RestApi(SyncthingService.this, "http://" + syncthingUrl); mApi = new RestApi(SyncthingService.this, "http://" + syncthingUrl);
Log.i(TAG, "Web GUI will be available at " + mApi.getUrl());
registerOnWebGuiAvailableListener(mApi); registerOnWebGuiAvailableListener(mApi);
} }
new PollWebGuiAvailableTask().execute(); new PollWebGuiAvailableTask().execute();
@ -267,6 +275,7 @@ public class SyncthingService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
Log.i(TAG, "Shutting down service");
mApi.shutdown(); mApi.shutdown();
} }
@ -299,6 +308,7 @@ public class SyncthingService extends Service {
*/ */
private void updateConfig() { private void updateConfig() {
try { try {
Log.i(TAG, "Checking for needed config updates");
boolean changed = false; boolean changed = false;
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document d = db.parse(getConfigFile()); Document d = db.parse(getConfigFile());
@ -321,6 +331,7 @@ public class SyncthingService extends Service {
Element r = (Element) repos.item(i); Element r = (Element) repos.item(i);
if (!r.hasAttribute("ignorePerms") || if (!r.hasAttribute("ignorePerms") ||
!Boolean.parseBoolean(r.getAttribute("ignorePerms"))) { !Boolean.parseBoolean(r.getAttribute("ignorePerms"))) {
Log.i(TAG, "Set 'ignorePerms' on repository " + r.getAttribute("id"));
r.setAttribute("ignorePerms", Boolean.toString(true)); r.setAttribute("ignorePerms", Boolean.toString(true));
changed = true; changed = true;
} }
@ -328,6 +339,7 @@ public class SyncthingService extends Service {
// Write the changes back to file. // Write the changes back to file.
if (changed) { if (changed) {
Log.i(TAG, "Writing updated config back to file");
TransformerFactory transformerFactory = TransformerFactory.newInstance(); TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(); Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(d); DOMSource domSource = new DOMSource(d);