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 final AtomicInteger mNextCommandTag = new AtomicInteger(0);
private String mTransportTag;
// Keep others from instantiating directly // Keep others from instantiating directly
ImapConnection(ImapStore store) { ImapConnection(ImapStore store) {
setStore(store); setStore(store);
@ -107,6 +109,16 @@ class ImapConnection {
mLoginPhrase = null; 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 * 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. * 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 // copy configuration into a clean transport, if necessary
if (mTransport == null) { if (mTransport == null) {
mTransport = mImapStore.cloneTransport(); mTransport = mImapStore.cloneTransport();
mTransport.setTag(mTransportTag);
} }
mTransport.open(); mTransport.open();
@ -277,6 +290,7 @@ class ImapConnection {
private void createParser() { private void createParser() {
destroyResponses(); destroyResponses();
mParser = new ImapResponseParser(mTransport.getInputStream(), mDiscourse); mParser = new ImapResponseParser(mTransport.getInputStream(), mDiscourse);
mParser.setTag(mTransportTag);
} }
void destroyResponses() { void destroyResponses() {

View File

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

View File

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

View File

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