Skip to content

Commit

Permalink
Merge pull request square#328 from square/jw/dont-mock-me
Browse files Browse the repository at this point in the history
Promote Android logging to a class which knows about chunking.
  • Loading branch information
dnkoutso committed Sep 18, 2013
2 parents 7780666 + 2e15cf9 commit 784a32b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
8 changes: 2 additions & 6 deletions retrofit/src/main/java/retrofit/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import android.os.Build;
import android.os.Process;
import android.util.Log;
import com.google.gson.Gson;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import retrofit.android.AndroidApacheClient;
import retrofit.android.AndroidLog;
import retrofit.android.MainThreadExecutor;
import retrofit.client.Client;
import retrofit.client.OkClient;
Expand Down Expand Up @@ -143,11 +143,7 @@ private static class Android extends Platform {
}

@Override RestAdapter.Log defaultLog() {
return new RestAdapter.Log() {
@Override public void log(String message) {
Log.d("Retrofit", message);
}
};
return new AndroidLog("Retrofit");
}
}

Expand Down
13 changes: 2 additions & 11 deletions retrofit/src/main/java/retrofit/RestAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
* @author Jake Wharton ([email protected])
*/
public class RestAdapter {
private static final int LOG_CHUNK_SIZE = 4000;
static final String THREAD_PREFIX = "Retrofit-";
static final String IDLE_THREAD_NAME = THREAD_PREFIX + "Idle";

Expand Down Expand Up @@ -396,11 +395,7 @@ private Request logAndReplaceRequest(Request request) throws IOException {
byte[] bodyBytes = ((TypedByteArray) body).getBytes();
bodySize = bodyBytes.length;
String bodyCharset = MimeUtil.parseCharset(bodyMime);
String bodyString = new String(bodyBytes, bodyCharset);
for (int i = 0, len = bodyString.length(); i < len; i += LOG_CHUNK_SIZE) {
int end = Math.min(len, i + LOG_CHUNK_SIZE);
log.log(bodyString.substring(i, end));
}
log.log(new String(bodyBytes, bodyCharset));
}
}

Expand Down Expand Up @@ -440,11 +435,7 @@ private Response logAndReplaceResponse(String url, Response response, long elaps
bodySize = bodyBytes.length;
String bodyMime = body.mimeType();
String bodyCharset = MimeUtil.parseCharset(bodyMime);
String bodyString = new String(bodyBytes, bodyCharset);
for (int i = 0, len = bodyString.length(); i < len; i += LOG_CHUNK_SIZE) {
int end = Math.min(len, i + LOG_CHUNK_SIZE);
log.log(bodyString.substring(i, end));
}
log.log(new String(bodyBytes, bodyCharset));
}
}

Expand Down
22 changes: 22 additions & 0 deletions retrofit/src/main/java/retrofit/android/AndroidLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package retrofit.android;

import android.util.Log;
import retrofit.RestAdapter;

/** A {@link RestAdapter.Log logger} for Android. */
public class AndroidLog implements RestAdapter.Log {
private static final int LOG_CHUNK_SIZE = 4000;

private final String tag;

public AndroidLog(String tag) {
this.tag = tag;
}

@Override public void log(String message) {
for (int i = 0, len = message.length(); i < len; i += LOG_CHUNK_SIZE) {
int end = Math.min(len, i + LOG_CHUNK_SIZE);
Log.d(tag, message.substring(i, end));
}
}
}

0 comments on commit 784a32b

Please sign in to comment.