From 36f18be31c0d191e5feed550b7a576dda93b4395 Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Tue, 9 Jun 2015 14:57:37 +0200 Subject: [PATCH] Assign debug tags to IMAP communication output. Allows better tracking of the output if multiple connections are active. Change-Id: I900c9f9ecda06d3191c4cf82af7e774ae70b7dac --- .../email/mail/store/ImapConnection.java | 14 ++++++++++++ .../android/email/mail/store/ImapFolder.java | 1 + .../mail/store/imap/ImapResponseParser.java | 14 +++++++++++- .../email/mail/transport/MailTransport.java | 22 ++++++++++++++----- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/provider_src/com/android/email/mail/store/ImapConnection.java b/provider_src/com/android/email/mail/store/ImapConnection.java index bef5f346e..3e5eb2a8a 100644 --- a/provider_src/com/android/email/mail/store/ImapConnection.java +++ b/provider_src/com/android/email/mail/store/ImapConnection.java @@ -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() { diff --git a/provider_src/com/android/email/mail/store/ImapFolder.java b/provider_src/com/android/email/mail/store/ImapFolder.java index 493035a73..9dc9c5e2c 100644 --- a/provider_src/com/android/email/mail/store/ImapFolder.java +++ b/provider_src/com/android/email/mail/store/ImapFolder.java @@ -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); diff --git a/provider_src/com/android/email/mail/store/imap/ImapResponseParser.java b/provider_src/com/android/email/mail/store/imap/ImapResponseParser.java index 5efea3109..fcedea1fc 100644 --- a/provider_src/com/android/email/mail/store/imap/ImapResponseParser.java +++ b/provider_src/com/android/email/mail/store/imap/ImapResponseParser.java @@ -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) { diff --git a/provider_src/com/android/email/mail/transport/MailTransport.java b/provider_src/com/android/email/mail/transport/MailTransport.java index 1767f19a7..c9db45ba8 100644 --- a/provider_src/com/android/email/mail/transport/MailTransport.java +++ b/provider_src/com/android/email/mail/transport/MailTransport.java @@ -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; }