From fa749da4a6af931eb9f14e570d414b4607e474f9 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 27 May 2014 01:24:37 +0200 Subject: [PATCH] Additional info level logging. --- .../syncthingandroid/syncthing/GetTask.java | 13 +++++------- .../syncthingandroid/syncthing/PostTask.java | 11 +++++----- .../syncthingandroid/syncthing/RestApi.java | 14 ++++++++----- .../syncthing/SyncthingService.java | 20 +++++++++++++++---- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/GetTask.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/GetTask.java index cc066337..498b68ac 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/GetTask.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/GetTask.java @@ -15,30 +15,27 @@ import java.io.InputStream; 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. */ public class GetTask extends AsyncTask { private static final String TAG = "GetTask"; - /** - * URI to call to get version name. - */ public static final String URI_VERSION = "/rest/version"; @Override protected String doInBackground(String... uri) { + String fullUri = uri[0] + uri[1]; + Log.i(TAG, "Sending GET request to " + fullUri); HttpClient httpclient = new DefaultHttpClient(); - HttpGet get = new HttpGet(uri[0] + uri[1]); + HttpGet get = new HttpGet(fullUri); String responseString = null; try { HttpResponse response = httpclient.execute(get); HttpEntity entity = response.getEntity(); if (entity != null) { - - // A Simple JSON Response Read InputStream is = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); @@ -52,7 +49,7 @@ public class GetTask extends AsyncTask { } } 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; } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/PostTask.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/PostTask.java index a44790e1..74743504 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/PostTask.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/PostTask.java @@ -11,27 +11,26 @@ import org.apache.http.impl.client.DefaultHttpClient; 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 { private static final String TAG = "PostTask"; - /** - * URI to call for shutdown. - */ public static final String URI_SHUTDOWN = "/rest/shutdown"; @Override protected Void doInBackground(String... uri) { + String fullUri = uri[0] + uri[1]; + Log.i(TAG, "Sending POST request to " + fullUri); HttpClient httpclient = new DefaultHttpClient(); - HttpPost post = new HttpPost(uri[0] + uri[1]); + HttpPost post = new HttpPost(fullUri); String responseString = null; try { HttpResponse response = httpclient.execute(post); } 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; } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index 190bd56c..20aa89f3 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -1,6 +1,7 @@ package com.nutomic.syncthingandroid.syncthing; import android.content.Context; +import android.util.Log; import com.nutomic.syncthingandroid.R; @@ -9,6 +10,8 @@ import com.nutomic.syncthingandroid.R; */ public class RestApi implements SyncthingService.OnWebGuiAvailableListener { + private static final String TAG = "RestApi"; + private Context mContext; private String mVersion; @@ -31,10 +34,9 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener { public void onWebGuiAvailable() { new GetTask() { @Override - protected void onPostExecute(String versionName) { - mVersion = (versionName != null) - ? versionName - : mContext.getString(R.string.syncthing_version_error); + protected void onPostExecute(String version) { + mVersion = version; + Log.i(TAG, "Syncthing version is " + mVersion); } }.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. */ public String getVersion() { - return mVersion; + return (mVersion != null) + ? mVersion + : mContext.getString(R.string.syncthing_version_error); } /** diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index c9f2dcbb..bba36d93 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -201,6 +201,7 @@ public class SyncthingService extends Service { @Override protected void onPostExecute(Void aVoid) { + Log.i(TAG, "Web GUI has come online at " + mApi.getUrl()); mIsWebGuiAvailable = true; for (OnWebGuiAvailableListener listener : mOnWebGuiAvailableListeners) { listener.onWebGuiAvailable(); @@ -229,6 +230,8 @@ public class SyncthingService extends Service { @Override public void run() { if (isFirstStart(SyncthingService.this)) { + Log.i(TAG, "App started for the first time. " + + "Copying default config, keys will be generated automatically"); copyDefaultConfig(); } updateConfig(); @@ -240,14 +243,19 @@ public class SyncthingService extends Service { Element options = (Element) d.getDocumentElement().getElementsByTagName("gui").item(0); syncthingUrl = options.getElementsByTagName("address").item(0).getTextContent(); - } catch (SAXException e) { + } + catch (SAXException 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); - } catch (IOException e) { + } + catch (IOException e) { throw new RuntimeException("Failed to read gui url, aborting", e); - } finally { + } + finally { mApi = new RestApi(SyncthingService.this, "http://" + syncthingUrl); + Log.i(TAG, "Web GUI will be available at " + mApi.getUrl()); registerOnWebGuiAvailableListener(mApi); } new PollWebGuiAvailableTask().execute(); @@ -267,6 +275,7 @@ public class SyncthingService extends Service { @Override public void onDestroy() { super.onDestroy(); + Log.i(TAG, "Shutting down service"); mApi.shutdown(); } @@ -299,6 +308,7 @@ public class SyncthingService extends Service { */ private void updateConfig() { try { + Log.i(TAG, "Checking for needed config updates"); boolean changed = false; DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document d = db.parse(getConfigFile()); @@ -321,6 +331,7 @@ public class SyncthingService extends Service { Element r = (Element) repos.item(i); if (!r.hasAttribute("ignorePerms") || !Boolean.parseBoolean(r.getAttribute("ignorePerms"))) { + Log.i(TAG, "Set 'ignorePerms' on repository " + r.getAttribute("id")); r.setAttribute("ignorePerms", Boolean.toString(true)); changed = true; } @@ -328,6 +339,7 @@ public class SyncthingService extends Service { // Write the changes back to file. if (changed) { + Log.i(TAG, "Writing updated config back to file"); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(d);