Skip to content

Commit 9164374

Browse files
committed
Added LogInterface and default used implementation LogHandler, which wrap logging enabled status and verbosity level
1 parent af9a1f5 commit 9164374

18 files changed

+302
-54
lines changed

library/src/main/java/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ public class AsyncHttpClient {
141141
private final Map<String, String> clientHeaderMap;
142142
private boolean isUrlEncodingEnabled = true;
143143

144+
public static LogInterface log = new LogHandler();
145+
144146
/**
145147
* Creates a new AsyncHttpClient with default constructor arguments values
146148
*/
@@ -187,17 +189,17 @@ public AsyncHttpClient(boolean fixNoHttpResponseException, int httpPort, int htt
187189
*/
188190
private static SchemeRegistry getDefaultSchemeRegistry(boolean fixNoHttpResponseException, int httpPort, int httpsPort) {
189191
if (fixNoHttpResponseException) {
190-
Log.d(LOG_TAG, "Beware! Using the fix is insecure, as it doesn't verify SSL certificates.");
192+
log.d(LOG_TAG, "Beware! Using the fix is insecure, as it doesn't verify SSL certificates.");
191193
}
192194

193195
if (httpPort < 1) {
194196
httpPort = 80;
195-
Log.d(LOG_TAG, "Invalid HTTP port number specified, defaulting to 80");
197+
log.d(LOG_TAG, "Invalid HTTP port number specified, defaulting to 80");
196198
}
197199

198200
if (httpsPort < 1) {
199201
httpsPort = 443;
200-
Log.d(LOG_TAG, "Invalid HTTPS port number specified, defaulting to 443");
202+
log.d(LOG_TAG, "Invalid HTTPS port number specified, defaulting to 443");
201203
}
202204

203205
// Fix to SSL flaw in API < ICS
@@ -254,7 +256,7 @@ public void process(HttpRequest request, HttpContext context) {
254256
for (String header : clientHeaderMap.keySet()) {
255257
if (request.containsHeader(header)) {
256258
Header overwritten = request.getFirstHeader(header);
257-
Log.d(LOG_TAG,
259+
log.d(LOG_TAG,
258260
String.format("Headers were overwritten! (%s | %s) overwrites (%s | %s)",
259261
header, clientHeaderMap.get(header),
260262
overwritten.getName(), overwritten.getValue())
@@ -342,6 +344,67 @@ public HttpContext getHttpContext() {
342344
return this.httpContext;
343345
}
344346

347+
/**
348+
* Will set logging enabled flag on underlying LogInterface instance.
349+
* Default setting is logging enabled.
350+
*
351+
* @param loggingEnabled whether the logging should be enabled or not
352+
*/
353+
public void setLoggingEnabled(boolean loggingEnabled) {
354+
log.setLoggingEnabled(loggingEnabled);
355+
}
356+
357+
/**
358+
* Returns logging enabled flag from underlying LogInterface instance
359+
* Default setting is logging enabled.
360+
*
361+
* @return boolean whether is logging across the library currently enabled
362+
*/
363+
public boolean isLoggingEnabled() {
364+
return log.isLoggingEnabled();
365+
}
366+
367+
/**
368+
* Sets log level to be used across all library default implementation
369+
* Default setting is VERBOSE log level.
370+
*
371+
* @param logLevel int log level, either from LogInterface interface or from {@link android.util.Log}
372+
*/
373+
public void setLoggingLevel(int logLevel) {
374+
log.setLoggingLevel(logLevel);
375+
}
376+
377+
/**
378+
* Retrieves current log level from underlying LogInterface instance.
379+
* Default setting is VERBOSE log level.
380+
*
381+
* @return int log level currently in effect
382+
*/
383+
public int getLoggingLevel() {
384+
return log.getLoggingLevel();
385+
}
386+
387+
/**
388+
* Will return current LogInterface used in AsyncHttpClient instance
389+
*
390+
* @return LogInterface currently used by AsyncHttpClient instance
391+
*/
392+
public LogInterface getLogInterface() {
393+
return log;
394+
}
395+
396+
/**
397+
* Sets default LogInterface (similar to std Android Log util class) instance,
398+
* to be used in AsyncHttpClient instance
399+
*
400+
* @param logInterfaceInstance LogInterface instance, if null, nothing is done
401+
*/
402+
public void setLogInterface(LogInterface logInterfaceInstance) {
403+
if (logInterfaceInstance != null) {
404+
log = logInterfaceInstance;
405+
}
406+
}
407+
345408
/**
346409
* Sets an optional CookieStore to use when making requests
347410
*
@@ -669,7 +732,7 @@ public void setBasicAuth(String username, String password, AuthScope scope, bool
669732

670733
public void setCredentials(AuthScope authScope, Credentials credentials) {
671734
if (credentials == null) {
672-
Log.d(LOG_TAG, "Provided credentials are null, not setting");
735+
log.d(LOG_TAG, "Provided credentials are null, not setting");
673736
return;
674737
}
675738
this.httpClient.getCredentialsProvider().setCredentials(authScope == null ? AuthScope.ANY : authScope, credentials);
@@ -718,7 +781,7 @@ public void clearCredentialsProvider() {
718781
*/
719782
public void cancelRequests(final Context context, final boolean mayInterruptIfRunning) {
720783
if (context == null) {
721-
Log.e(LOG_TAG, "Passed null Context to cancelRequests");
784+
log.e(LOG_TAG, "Passed null Context to cancelRequests");
722785
return;
723786
}
724787

@@ -1304,10 +1367,10 @@ protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpCo
13041367
if (responseHandler.getUseSynchronousMode() && !responseHandler.getUsePoolThread()) {
13051368
throw new IllegalArgumentException("Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
13061369
}
1307-
1370+
13081371
if (contentType != null) {
13091372
if (uriRequest instanceof HttpEntityEnclosingRequestBase && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null && uriRequest.containsHeader(HEADER_CONTENT_TYPE)) {
1310-
Log.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type");
1373+
log.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type");
13111374
} else {
13121375
uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType);
13131376
}
@@ -1384,7 +1447,7 @@ public static String getUrlWithQueryString(boolean shouldEncodeUrl, String url,
13841447
url = _uri.toASCIIString();
13851448
} catch (Exception ex) {
13861449
// Should not really happen, added just for sake of validity
1387-
Log.e(LOG_TAG, "getUrlWithQueryString encoding URL", ex);
1450+
log.e(LOG_TAG, "getUrlWithQueryString encoding URL", ex);
13881451
}
13891452
}
13901453

@@ -1433,7 +1496,7 @@ public static void silentCloseInputStream(InputStream is) {
14331496
is.close();
14341497
}
14351498
} catch (IOException e) {
1436-
Log.w(LOG_TAG, "Cannot close input stream", e);
1499+
log.w(LOG_TAG, "Cannot close input stream", e);
14371500
}
14381501
}
14391502

@@ -1448,7 +1511,7 @@ public static void silentCloseOutputStream(OutputStream os) {
14481511
os.close();
14491512
}
14501513
} catch (IOException e) {
1451-
Log.w(LOG_TAG, "Cannot close output stream", e);
1514+
log.w(LOG_TAG, "Cannot close output stream", e);
14521515
}
14531516
}
14541517

@@ -1521,7 +1584,7 @@ public static void endEntityViaReflection(HttpEntity entity) {
15211584
}
15221585
}
15231586
} catch (Throwable t) {
1524-
Log.e(LOG_TAG, "wrappedEntity consume", t);
1587+
log.e(LOG_TAG, "wrappedEntity consume", t);
15251588
}
15261589
}
15271590
}

library/src/main/java/com/loopj/android/http/AsyncHttpRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void run() {
110110
if (!isCancelled()) {
111111
responseHandler.sendFailureMessage(0, null, null, e);
112112
} else {
113-
Log.e("AsyncHttpRequest", "makeRequestWithRetries returned error", e);
113+
AsyncHttpClient.log.e("AsyncHttpRequest", "makeRequestWithRetries returned error", e);
114114
}
115115
}
116116

@@ -204,7 +204,7 @@ private void makeRequestWithRetries() throws IOException {
204204
}
205205
} catch (Exception e) {
206206
// catch anything else to ensure failure message is propagated
207-
Log.e("AsyncHttpRequest", "Unhandled exception origin cause", e);
207+
AsyncHttpClient.log.e("AsyncHttpRequest", "Unhandled exception origin cause", e);
208208
cause = new IOException("Unhandled exception: " + e.getMessage());
209209
}
210210

library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public void setUseSynchronousMode(boolean sync) {
195195
// A looper must be prepared before setting asynchronous mode.
196196
if (!sync && looper == null) {
197197
sync = true;
198-
Log.w(LOG_TAG, "Current thread has not called Looper.prepare(). Forcing synchronous mode.");
198+
AsyncHttpClient.log.w(LOG_TAG, "Current thread has not called Looper.prepare(). Forcing synchronous mode.");
199199
}
200200

201201
// If using asynchronous mode.
@@ -248,7 +248,7 @@ public String getCharset() {
248248
* @param totalSize total size of file
249249
*/
250250
public void onProgress(long bytesWritten, long totalSize) {
251-
Log.v(LOG_TAG, String.format("Progress %d from %d (%2.0f%%)", bytesWritten, totalSize, (totalSize > 0) ? (bytesWritten * 1.0 / totalSize) * 100 : -1));
251+
AsyncHttpClient.log.v(LOG_TAG, String.format("Progress %d from %d (%2.0f%%)", bytesWritten, totalSize, (totalSize > 0) ? (bytesWritten * 1.0 / totalSize) * 100 : -1));
252252
}
253253

254254
/**
@@ -301,15 +301,15 @@ public void onPostProcessResponse(ResponseHandlerInterface instance, HttpRespons
301301
* @param retryNo number of retry
302302
*/
303303
public void onRetry(int retryNo) {
304-
Log.d(LOG_TAG, String.format("Request retry no. %d", retryNo));
304+
AsyncHttpClient.log.d(LOG_TAG, String.format("Request retry no. %d", retryNo));
305305
}
306306

307307
public void onCancel() {
308-
Log.d(LOG_TAG, "Request got cancelled");
308+
AsyncHttpClient.log.d(LOG_TAG, "Request got cancelled");
309309
}
310310

311311
public void onUserException(Throwable error) {
312-
Log.e(LOG_TAG, "User-space exception detected!", error);
312+
AsyncHttpClient.log.e(LOG_TAG, "User-space exception detected!", error);
313313
throw new RuntimeException(error);
314314
}
315315

@@ -359,15 +359,15 @@ protected void handleMessage(Message message) {
359359
if (response != null && response.length >= 3) {
360360
onSuccess((Integer) response[0], (Header[]) response[1], (byte[]) response[2]);
361361
} else {
362-
Log.e(LOG_TAG, "SUCCESS_MESSAGE didn't got enough params");
362+
AsyncHttpClient.log.e(LOG_TAG, "SUCCESS_MESSAGE didn't got enough params");
363363
}
364364
break;
365365
case FAILURE_MESSAGE:
366366
response = (Object[]) message.obj;
367367
if (response != null && response.length >= 4) {
368368
onFailure((Integer) response[0], (Header[]) response[1], (byte[]) response[2], (Throwable) response[3]);
369369
} else {
370-
Log.e(LOG_TAG, "FAILURE_MESSAGE didn't got enough params");
370+
AsyncHttpClient.log.e(LOG_TAG, "FAILURE_MESSAGE didn't got enough params");
371371
}
372372
break;
373373
case START_MESSAGE:
@@ -382,18 +382,18 @@ protected void handleMessage(Message message) {
382382
try {
383383
onProgress((Long) response[0], (Long) response[1]);
384384
} catch (Throwable t) {
385-
Log.e(LOG_TAG, "custom onProgress contains an error", t);
385+
AsyncHttpClient.log.e(LOG_TAG, "custom onProgress contains an error", t);
386386
}
387387
} else {
388-
Log.e(LOG_TAG, "PROGRESS_MESSAGE didn't got enough params");
388+
AsyncHttpClient.log.e(LOG_TAG, "PROGRESS_MESSAGE didn't got enough params");
389389
}
390390
break;
391391
case RETRY_MESSAGE:
392392
response = (Object[]) message.obj;
393393
if (response != null && response.length == 1) {
394394
onRetry((Integer) response[0]);
395395
} else {
396-
Log.e(LOG_TAG, "RETRY_MESSAGE didn't get enough params");
396+
AsyncHttpClient.log.e(LOG_TAG, "RETRY_MESSAGE didn't get enough params");
397397
}
398398
break;
399399
case CANCEL_MESSAGE:

library/src/main/java/com/loopj/android/http/BaseJsonHttpResponseHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void run() {
8787
}
8888
});
8989
} catch (final Throwable t) {
90-
Log.d(LOG_TAG, "parseResponse thrown an problem", t);
90+
AsyncHttpClient.log.d(LOG_TAG, "parseResponse thrown an problem", t);
9191
postRunnable(new Runnable() {
9292
@Override
9393
public void run() {
@@ -123,7 +123,7 @@ public void run() {
123123
}
124124
});
125125
} catch (Throwable t) {
126-
Log.d(LOG_TAG, "parseResponse thrown an problem", t);
126+
AsyncHttpClient.log.d(LOG_TAG, "parseResponse thrown an problem", t);
127127
postRunnable(new Runnable() {
128128
@Override
129129
public void run() {

library/src/main/java/com/loopj/android/http/BinaryHttpResponseHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public BinaryHttpResponseHandler(String[] allowedContentTypes) {
8989
if (allowedContentTypes != null) {
9090
mAllowedContentTypes = allowedContentTypes;
9191
} else {
92-
Log.e(LOG_TAG, "Constructor passed allowedContentTypes was null !");
92+
AsyncHttpClient.log.e(LOG_TAG, "Constructor passed allowedContentTypes was null !");
9393
}
9494
}
9595

@@ -105,7 +105,7 @@ public BinaryHttpResponseHandler(String[] allowedContentTypes, Looper looper) {
105105
if (allowedContentTypes != null) {
106106
mAllowedContentTypes = allowedContentTypes;
107107
} else {
108-
Log.e(LOG_TAG, "Constructor passed allowedContentTypes was null !");
108+
AsyncHttpClient.log.e(LOG_TAG, "Constructor passed allowedContentTypes was null !");
109109
}
110110
}
111111

@@ -140,7 +140,7 @@ public final void sendResponseMessage(HttpResponse response) throws IOException
140140
foundAllowedContentType = true;
141141
}
142142
} catch (PatternSyntaxException e) {
143-
Log.e(LOG_TAG, "Given pattern is not valid: " + anAllowedContentType, e);
143+
AsyncHttpClient.log.e(LOG_TAG, "Given pattern is not valid: " + anAllowedContentType, e);
144144
}
145145
}
146146
if (!foundAllowedContentType) {

library/src/main/java/com/loopj/android/http/DataAsyncHttpResponseHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public DataAsyncHttpResponseHandler() {
4646
* @param responseBody response body received so far
4747
*/
4848
public void onProgressData(byte[] responseBody) {
49-
Log.d(LOG_TAG, "onProgressData(byte[]) was not overriden, but callback was received");
49+
AsyncHttpClient.log.d(LOG_TAG, "onProgressData(byte[]) was not overriden, but callback was received");
5050
}
5151

5252

@@ -67,10 +67,10 @@ protected void handleMessage(Message message) {
6767
try {
6868
onProgressData((byte[]) response[0]);
6969
} catch (Throwable t) {
70-
Log.e(LOG_TAG, "custom onProgressData contains an error", t);
70+
AsyncHttpClient.log.e(LOG_TAG, "custom onProgressData contains an error", t);
7171
}
7272
} else {
73-
Log.e(LOG_TAG, "PROGRESS_DATA_MESSAGE didn't got enough params");
73+
AsyncHttpClient.log.e(LOG_TAG, "PROGRESS_DATA_MESSAGE didn't got enough params");
7474
}
7575
break;
7676
}

library/src/main/java/com/loopj/android/http/FileAsyncHttpResponseHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public FileAsyncHttpResponseHandler(File file, boolean append, boolean renameTar
7171
}
7272
if (file.isDirectory()) {
7373
if (!file.mkdirs()) {
74-
Log.d(LOG_TAG, "Cannot create directories for requested Directory location, might not be a problem");
74+
AsyncHttpClient.log.d(LOG_TAG, "Cannot create directories for requested Directory location, might not be a problem");
7575
}
7676
}
7777
this.file = file;
@@ -111,7 +111,7 @@ protected File getTemporaryFile(Context context) {
111111
try {
112112
return File.createTempFile("temp_", "_handled", context.getCacheDir());
113113
} catch (IOException e) {
114-
Log.e(LOG_TAG, "Cannot create temporary file", e);
114+
AsyncHttpClient.log.e(LOG_TAG, "Cannot create temporary file", e);
115115
}
116116
return null;
117117
}

0 commit comments

Comments
 (0)