Assign debug tags to IMAP communication output.

Allows better tracking of the output if multiple connections are active.

Change-Id: I900c9f9ecda06d3191c4cf82af7e774ae70b7dac
This commit is contained in:
Danny Baumann 2015-06-09 14:57:37 +02:00 committed by Steve Kondik
parent e680bf0ddc
commit 36f18be31c
4 changed files with 44 additions and 7 deletions

View File

@ -94,6 +94,8 @@ class ImapConnection {
*/
private final AtomicInteger mNextCommandTag = new AtomicInteger(0);
private String mTransportTag;
// Keep others from instantiating directly
ImapConnection(ImapStore store) {
setStore(store);
@ -107,6 +109,16 @@ class ImapConnection {
mLoginPhrase = null;
}
void setTransportTag(String tag) {
mTransportTag = tag;
if (mTransport != null) {
mTransport.setTag(tag);
}
if (mParser != null) {
mParser.setTag(tag);
}
}
/**
* Generates and returns the phrase to be used for authentication. This will be a LOGIN with
* username and password, or an OAUTH authentication string, with username and access token.
@ -152,6 +164,7 @@ class ImapConnection {
// copy configuration into a clean transport, if necessary
if (mTransport == null) {
mTransport = mImapStore.cloneTransport();
mTransport.setTag(mTransportTag);
}
mTransport.open();
@ -277,6 +290,7 @@ class ImapConnection {
private void createParser() {
destroyResponses();
mParser = new ImapResponseParser(mTransport.getInputStream(), mDiscourse);
mParser.setTag(mTransportTag);
}
void destroyResponses() {

View File

@ -177,6 +177,7 @@ public class ImapFolder extends Folder {
// * OK [UIDNEXT 57576] Predicted next UID
// 2 OK [READ-WRITE] Select completed.
try {
mConnection.setTransportTag(mName + "-" + hashCode());
doSelect();
} catch (IOException ioe) {
throw ioExceptionHandler(mConnection, ioe);

View File

@ -68,6 +68,7 @@ public class ImapResponseParser {
private boolean mIdling;
private boolean mExpectIdlingResponse;
private String mTag;
/**
* Exception thrown when we receive BYE. It derives from IOException, so it'll be treated
@ -100,6 +101,10 @@ public class ImapResponseParser {
mLiteralKeepInMemoryThreshold = literalKeepInMemoryThreshold;
}
public void setTag(String tag) {
mTag = tag;
}
private static IOException newEOSException() {
final String message = "End of stream reached";
if (DebugUtils.DEBUG) {
@ -149,6 +154,13 @@ public class ImapResponseParser {
mResponsesToDestroy.clear();
}
private String getFormattedTag() {
if (mTag != null) {
return "(" + mTag + ") ";
}
return "";
}
/**
* Reads the next response available on the stream and returns an
* {@link ImapResponse} object that represents it.
@ -165,7 +177,7 @@ public class ImapResponseParser {
try {
response = parseResponse();
if (DebugUtils.DEBUG) {
LogUtils.d(Logging.LOG_TAG, "<<< " + response.toString());
LogUtils.d(Logging.LOG_TAG, getFormattedTag() + "<<< " + response.toString());
}
} catch (RuntimeException e) {

View File

@ -55,6 +55,7 @@ public class MailTransport {
HttpsURLConnection.getDefaultHostnameVerifier();
private final String mDebugLabel;
private String mDebugTag;
private final Context mContext;
protected final HostAuth mHostAuth;
@ -69,6 +70,10 @@ public class MailTransport {
mHostAuth = hostAuth;
}
public void setTag(String tag) {
mDebugTag = tag;
}
/**
* Returns a new transport, using the current transport as a model. The new transport is
* configured identically (as if {@link #setSecurity(int, boolean)}, {@link #setPort(int)}
@ -292,16 +297,21 @@ public class MailTransport {
return mOut;
}
private String getFormattedDebugTag() {
if (mDebugTag != null) {
return "(" + mDebugTag + ") ";
}
return "";
}
/**
* Writes a single line to the server using \r\n termination.
*/
public void writeLine(String s, String sensitiveReplacement) throws IOException {
if (DebugUtils.DEBUG) {
if (sensitiveReplacement != null && !Logging.DEBUG_SENSITIVE) {
LogUtils.d(Logging.LOG_TAG, ">>> " + sensitiveReplacement);
} else {
LogUtils.d(Logging.LOG_TAG, ">>> " + s);
}
String output = sensitiveReplacement != null && !Logging.DEBUG_SENSITIVE
? sensitiveReplacement : s;
LogUtils.d(Logging.LOG_TAG, getFormattedDebugTag() + ">>> " + output);
}
OutputStream out = getOutputStream();
@ -333,7 +343,7 @@ public class MailTransport {
}
String ret = sb.toString();
if (loggable && DebugUtils.DEBUG) {
LogUtils.d(Logging.LOG_TAG, "<<< " + ret);
LogUtils.d(Logging.LOG_TAG, getFormattedDebugTag() + "<<< " + ret);
}
return ret;
}