From 9833cb5e9bf24b506527378c55496716512e1962 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Tue, 12 Dec 2017 09:26:47 +0100 Subject: [android] - allow configurable http logging --- .../com/mapbox/mapboxsdk/http/HTTPRequest.java | 36 ++++++++++++++-------- .../com/mapbox/mapboxsdk/http/HttpRequestUtil.java | 19 ++++++++++++ .../activity/maplayout/DebugModeActivity.java | 5 ++- 3 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequestUtil.java 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. + *

+ * This configuration will outlast the lifecycle of the Map. + *

+ * + * @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 -- cgit v1.2.1