diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2017-12-12 09:26:47 +0100 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2017-12-12 17:25:27 +0100 |
commit | 7c06eda515ded2728594377610f75f309c22e913 (patch) | |
tree | f49c3906a764c8f8b7bd2502813e24cf1a9e3bfc | |
parent | cf026d1932ca380a799dd4263e02dce67d22806b (diff) | |
download | qtlocation-mapboxgl-7c06eda515ded2728594377610f75f309c22e913.tar.gz |
[android] - allow configurable http logging
3 files changed, 46 insertions, 14 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java index 9c7fe4ee63..8463814794 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java @@ -33,6 +33,7 @@ import timber.log.Timber; class HTTPRequest implements Callback { private static OkHttpClient mClient = new OkHttpClient.Builder().dispatcher(getDispatcher()).build(); + private static boolean logEnabled = true; private String USER_AGENT_STRING = null; private static final int CONNECTION_ERROR = 0; @@ -118,12 +119,15 @@ class HTTPRequest implements Callback { @Override public void onResponse(Call call, Response response) throws IOException { - if (response.isSuccessful()) { - Timber.v("[HTTP] Request was successful (code = %s).", response.code()); - } else { - // We don't want to call this unsuccessful because a 304 isn't really an error - String message = !TextUtils.isEmpty(response.message()) ? response.message() : "No additional information"; - Timber.d("[HTTP] Request with response code = %s: %s", response.code(), message); + + if (logEnabled) { + if (response.isSuccessful()) { + Timber.v("[HTTP] Request was successful (code = %s).", response.code()); + } else { + // We don't want to call this unsuccessful because a 304 isn't really an error + String message = !TextUtils.isEmpty(response.message()) ? response.message() : "No additional information"; + Timber.d("[HTTP] Request with response code = %s: %s", response.code(), message); + } } byte[] body; @@ -167,13 +171,15 @@ class HTTPRequest implements Callback { String errorMessage = e.getMessage() != null ? e.getMessage() : "Error processing the request"; - if (type == TEMPORARY_ERROR) { - Timber.d("Request failed due to a temporary error: %s", errorMessage); - } else if (type == CONNECTION_ERROR) { - Timber.i("Request failed due to a connection error: %s", errorMessage); - } else { - // PERMANENT_ERROR - Timber.w("Request failed due to a permanent error: %s", errorMessage); + if (logEnabled) { + if (type == TEMPORARY_ERROR) { + Timber.d("Request failed due to a temporary error: %s", errorMessage); + } else if (type == CONNECTION_ERROR) { + Timber.i("Request failed due to a connection error: %s", errorMessage); + } else { + // PERMANENT_ERROR + Timber.w("Request failed due to a permanent error: %s", errorMessage); + } } mLock.lock(); @@ -207,4 +213,8 @@ class HTTPRequest implements Callback { return ""; } } + + static void enableLog(boolean enabled) { + logEnabled = enabled; + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequestUtil.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequestUtil.java new file mode 100644 index 0000000000..af39faeded --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequestUtil.java @@ -0,0 +1,19 @@ +package com.mapbox.mapboxsdk.http; + +/** + * Utility class for setting HttpRequest configurations + */ +public class HttpRequestUtil { + + /** + * Set the log state of HttpRequest. + * <p> + * This configuration will outlast the lifecycle of the Map. + * </p> + * + * @param enabled True will enable logging, false will disable + */ + public static void setLogEnabled(boolean enabled) { + HTTPRequest.enableLog(enabled); + } +}
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java index 6134b4cb7a..9220a30c36 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java @@ -18,6 +18,7 @@ import android.widget.TextView; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.constants.Style; +import com.mapbox.mapboxsdk.http.HttpRequestUtil; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; @@ -56,6 +57,7 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + HttpRequestUtil.setLogEnabled(false); setContentView(R.layout.activity_debug_mode); setupToolbar(); setupMapView(savedInstanceState); @@ -113,7 +115,7 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa mapboxMap.setOnFpsChangedListener(new MapboxMap.OnFpsChangedListener() { @Override public void onFpsChanged(double fps) { - fpsView.setText(String.format(Locale.US,"FPS: %4.2f", fps)); + fpsView.setText(String.format(Locale.US, "FPS: %4.2f", fps)); } }); } @@ -229,6 +231,7 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); + HttpRequestUtil.setLogEnabled(true); } @Override |