From 05bd6e0a5b79b78ca77cd5459e7bf8af7485ec33 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 2 Mar 2017 20:15:00 +0100 Subject: [PATCH] plant DebugTree if K9.DEBUG || BuildConfig.DEBUG --- k9mail/src/main/java/com/fsck/k9/K9.java | 24 ++++++++++++++----- .../fsck/k9/activity/ActivityListener.java | 2 +- .../com/fsck/k9/activity/setup/Prefs.java | 6 ++--- .../k9/controller/MessagingController.java | 4 ++-- .../com/fsck/k9/mailstore/LocalFolder.java | 2 +- .../com/fsck/k9/mailstore/LocalStore.java | 10 ++++---- .../fsck/k9/mailstore/LockableDatabase.java | 2 +- .../com/fsck/k9/message/TextBodyBuilder.java | 2 +- .../com/fsck/k9/preferences/Settings.java | 2 +- .../fsck/k9/preferences/SettingsImporter.java | 2 +- .../provider/AttachmentTempFileProvider.java | 2 +- .../k9/provider/DecryptedFileProvider.java | 2 +- .../k9/activity/ActivityListenerTest.java | 6 ++--- .../controller/MessagingControllerTest.java | 2 +- .../com/fsck/k9/mailstore/MigrationTest.java | 2 +- 15 files changed, 41 insertions(+), 29 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/K9.java b/k9mail/src/main/java/com/fsck/k9/K9.java index 90fec6f08..d2288cfe7 100644 --- a/k9mail/src/main/java/com/fsck/k9/K9.java +++ b/k9mail/src/main/java/com/fsck/k9/K9.java @@ -137,7 +137,7 @@ public class K9 extends Application { * Log.d, including protocol dumps. * Controlled by Preferences at run-time */ - public static boolean DEBUG = false; + private static boolean DEBUG = false; /** * If this is enabled than logging that normally hides sensitive information @@ -523,10 +523,6 @@ public class K9 extends Application { app = this; Globals.setContext(this); - if (BuildConfig.DEBUG) { - Timber.plant(new DebugTree()); - } - K9MailLib.setDebugStatus(new K9MailLib.DebugStatus() { @Override public boolean enabled() { return DEBUG; @@ -675,7 +671,7 @@ public class K9 extends Application { */ public static void loadPrefs(Preferences prefs) { Storage storage = prefs.getStorage(); - DEBUG = storage.getBoolean("enableDebugLogging", BuildConfig.DEVELOPER_MODE); + setDebug(storage.getBoolean("enableDebugLogging", BuildConfig.DEVELOPER_MODE)); DEBUG_SENSITIVE = storage.getBoolean("enableSensitiveLogging", false); mAnimations = storage.getBoolean("animations", true); mGesturesEnabled = storage.getBoolean("gesturesEnabled", false); @@ -1020,7 +1016,14 @@ public class K9 extends Application { return false; } + public static void setDebug(boolean debug) { + K9.DEBUG = debug; + updateLoggingStatus(); + } + public static boolean isDebug() { + return DEBUG; + } public static boolean startIntegratedInbox() { return mStartIntegratedInbox; @@ -1422,4 +1425,13 @@ public class K9 extends Application { editor.commit(); } } + + private static void updateLoggingStatus() { + Timber.uprootAll(); + boolean enableDebugLogging = BuildConfig.DEBUG || DEBUG; + if (enableDebugLogging) { + Timber.plant(new DebugTree()); + } + } + } diff --git a/k9mail/src/main/java/com/fsck/k9/activity/ActivityListener.java b/k9mail/src/main/java/com/fsck/k9/activity/ActivityListener.java index 1dfc9b0ca..1d494bc8d 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/ActivityListener.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/ActivityListener.java @@ -45,7 +45,7 @@ public class ActivityListener extends SimpleMessagingListener { return context.getString(R.string.status_next_poll, DateUtils.getRelativeTimeSpanString(nextPollTime, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS, 0)); - } else if (K9.DEBUG && MailService.isSyncDisabled()) { + } else if (K9.isDebug() && MailService.isSyncDisabled()) { if (MailService.hasNoConnectivity()) { return context.getString(R.string.status_no_network); } else if (MailService.isSyncNoBackground()) { diff --git a/k9mail/src/main/java/com/fsck/k9/activity/setup/Prefs.java b/k9mail/src/main/java/com/fsck/k9/activity/setup/Prefs.java index 0f082e0c4..9fed129bc 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/setup/Prefs.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/setup/Prefs.java @@ -357,7 +357,7 @@ public class Prefs extends K9PreferenceActivity { mHideUserAgent = (CheckBoxPreference)findPreference(PREFERENCE_HIDE_USERAGENT); mHideTimeZone = (CheckBoxPreference)findPreference(PREFERENCE_HIDE_TIMEZONE); - mDebugLogging.setChecked(K9.DEBUG); + mDebugLogging.setChecked(K9.isDebug()); mSensitiveLogging.setChecked(K9.DEBUG_SENSITIVE); mHideUserAgent.setChecked(K9.hideUserAgent()); mHideTimeZone.setChecked(K9.hideTimeZone()); @@ -529,10 +529,10 @@ public class Prefs extends K9PreferenceActivity { K9.setAttachmentDefaultPath(mAttachmentPathPreference.getSummary().toString()); boolean needsRefresh = K9.setBackgroundOps(mBackgroundOps.getValue()); - if (!K9.DEBUG && mDebugLogging.isChecked()) { + if (!K9.isDebug() && mDebugLogging.isChecked()) { Toast.makeText(this, R.string.debug_logging_enabled, Toast.LENGTH_LONG).show(); } - K9.DEBUG = mDebugLogging.isChecked(); + K9.setDebug(mDebugLogging.isChecked()); K9.DEBUG_SENSITIVE = mSensitiveLogging.isChecked(); K9.setHideUserAgent(mHideUserAgent.isChecked()); K9.setHideTimeZone(mHideTimeZone.isChecked()); diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java index 7df0801b1..01facaee7 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -1287,7 +1287,7 @@ public class MessagingController { public void messageFinished(T message, int number, int ofTotal) { try { if (message.isSet(Flag.DELETED) || message.olderThan(earliestDate)) { - if (K9.DEBUG) { + if (K9.isDebug()) { if (message.isSet(Flag.DELETED)) { Timber.v("Newly downloaded message %s:%s:%s was marked deleted on server, " + "skipping", account, folder, message.getUid()); @@ -2146,7 +2146,7 @@ public class MessagingController { private static AtomicBoolean loopCatch = new AtomicBoolean(); private void addErrorMessage(Account account, String subject, String body) { - if (!K9.DEBUG) { + if (!K9.isDebug()) { return; } if (!loopCatch.compareAndSet(false, true)) { diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java index 600ca9ea2..ea2559d84 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java @@ -1873,7 +1873,7 @@ public class LocalFolder extends Folder implements Serializable { String messagePartId = cursor.getString(0); File file = localStore.getAttachmentFile(messagePartId); if (file.exists()) { - if (!file.delete() && K9.DEBUG) { + if (!file.delete() && K9.isDebug()) { Timber.d("Couldn't delete message part file: %s", file.getAbsolutePath()); } } diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java index c93294b6b..c1aa977e9 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java @@ -301,7 +301,7 @@ public class LocalStore extends Store implements Serializable { } public void compact() throws MessagingException { - if (K9.DEBUG) { + if (K9.isDebug()) { Timber.i("Before compaction size = %d", getSize()); } @@ -313,20 +313,20 @@ public class LocalStore extends Store implements Serializable { } }); - if (K9.DEBUG) { + if (K9.isDebug()) { Timber.i("After compaction size = %d", getSize()); } } public void clear() throws MessagingException { - if (K9.DEBUG) { + if (K9.isDebug()) { Timber.i("Before prune size = %d", getSize()); } deleteAllMessageDataFromDisk(); - if (K9.DEBUG) { + if (K9.isDebug()) { Timber.i("After prune / before compaction size = %d", getSize()); Timber.i("Before clear folder count = %d", getFolderCount()); Timber.i("Before clear message count = %d", getMessageCount()); @@ -352,7 +352,7 @@ public class LocalStore extends Store implements Serializable { compact(); - if (K9.DEBUG) { + if (K9.isDebug()) { Timber.i("After clear message count = %d", getMessageCount()); Timber.i("After clear size = %d", getSize()); } diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LockableDatabase.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LockableDatabase.java index 2e26fbc8c..2c7d3b727 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LockableDatabase.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LockableDatabase.java @@ -265,7 +265,7 @@ public class LockableDatabase { lockRead(); final boolean doTransaction = transactional && inTransaction.get() == null; try { - final boolean debug = K9.DEBUG; + final boolean debug = K9.isDebug(); if (doTransaction) { inTransaction.set(Boolean.TRUE); mDb.beginTransaction(); diff --git a/k9mail/src/main/java/com/fsck/k9/message/TextBodyBuilder.java b/k9mail/src/main/java/com/fsck/k9/message/TextBodyBuilder.java index 291e382a3..869333857 100644 --- a/k9mail/src/main/java/com/fsck/k9/message/TextBodyBuilder.java +++ b/k9mail/src/main/java/com/fsck/k9/message/TextBodyBuilder.java @@ -47,7 +47,7 @@ class TextBodyBuilder { if (mIncludeQuotedText) { InsertableHtmlContent quotedHtmlContent = getQuotedTextHtml(); - if (K9.DEBUG) { + if (K9.isDebug()) { Timber.d("insertable: %s", quotedHtmlContent.toDebugString()); } diff --git a/k9mail/src/main/java/com/fsck/k9/preferences/Settings.java b/k9mail/src/main/java/com/fsck/k9/preferences/Settings.java index ffff49090..383edaa22 100644 --- a/k9mail/src/main/java/com/fsck/k9/preferences/Settings.java +++ b/k9mail/src/main/java/com/fsck/k9/preferences/Settings.java @@ -166,7 +166,7 @@ public class Settings { T defaultValue = setting.getDefaultValue(); validatedSettingsMutable.put(settingName, defaultValue); - if (K9.DEBUG) { + if (K9.isDebug()) { String prettyValue = setting.toPrettyString(defaultValue); Timber.v("Added new setting \"%s\" with default value \"%s\"", settingName, prettyValue); } diff --git a/k9mail/src/main/java/com/fsck/k9/preferences/SettingsImporter.java b/k9mail/src/main/java/com/fsck/k9/preferences/SettingsImporter.java index 8464eb54e..001d3d5f8 100644 --- a/k9mail/src/main/java/com/fsck/k9/preferences/SettingsImporter.java +++ b/k9mail/src/main/java/com/fsck/k9/preferences/SettingsImporter.java @@ -609,7 +609,7 @@ public class SettingsImporter { * The new value for the preference. */ private static void putString(StorageEditor editor, String key, String value) { - if (K9.DEBUG) { + if (K9.isDebug()) { String outputValue = value; if (!K9.DEBUG_SENSITIVE && (key.endsWith(".transportUri") || key.endsWith(".storeUri"))) { outputValue = "*sensitive*"; diff --git a/k9mail/src/main/java/com/fsck/k9/provider/AttachmentTempFileProvider.java b/k9mail/src/main/java/com/fsck/k9/provider/AttachmentTempFileProvider.java index 3b7a6ea10..43158442b 100644 --- a/k9mail/src/main/java/com/fsck/k9/provider/AttachmentTempFileProvider.java +++ b/k9mail/src/main/java/com/fsck/k9/provider/AttachmentTempFileProvider.java @@ -106,7 +106,7 @@ public class AttachmentTempFileProvider extends FileProvider { allFilesDeleted = false; } } else { - if (K9.DEBUG) { + if (K9.isDebug()) { String timeLeftStr = String.format( Locale.ENGLISH, "%.2f", (lastModified - deletionThreshold) / 1000 / 60.0); Timber.e("Not deleting temp file (for another %s minutes)", timeLeftStr); diff --git a/k9mail/src/main/java/com/fsck/k9/provider/DecryptedFileProvider.java b/k9mail/src/main/java/com/fsck/k9/provider/DecryptedFileProvider.java index b3ae4b502..3e85c19a1 100644 --- a/k9mail/src/main/java/com/fsck/k9/provider/DecryptedFileProvider.java +++ b/k9mail/src/main/java/com/fsck/k9/provider/DecryptedFileProvider.java @@ -85,7 +85,7 @@ public class DecryptedFileProvider extends FileProvider { allFilesDeleted = false; } } else { - if (K9.DEBUG) { + if (K9.isDebug()) { String timeLeftStr = String.format( Locale.ENGLISH, "%.2f", (lastModified - deletionThreshold) / 1000 / 60.0); Timber.e("Not deleting temp file (for another %s minutes)", timeLeftStr); diff --git a/k9mail/src/test/java/com/fsck/k9/activity/ActivityListenerTest.java b/k9mail/src/test/java/com/fsck/k9/activity/ActivityListenerTest.java index 660fa1ea4..f05813fc5 100644 --- a/k9mail/src/test/java/com/fsck/k9/activity/ActivityListenerTest.java +++ b/k9mail/src/test/java/com/fsck/k9/activity/ActivityListenerTest.java @@ -75,7 +75,7 @@ public class ActivityListenerTest { String operation = activityListener.getOperation(context); - if (K9.DEBUG) { + if (K9.isDebug()) { assertEquals("Polling and pushing disabled", operation); } else { assertEquals("Syncing disabled", operation); @@ -90,7 +90,7 @@ public class ActivityListenerTest { String operation = activityListener.getOperation(context); - if (K9.DEBUG) { + if (K9.isDebug()) { assertEquals("Polling and pushing disabled", operation); } else { assertEquals("Syncing disabled", operation); @@ -104,7 +104,7 @@ public class ActivityListenerTest { String operation = activityListener.getOperation(context); - if (K9.DEBUG) { + if (K9.isDebug()) { assertEquals("Polling and pushing disabled", operation); } else { assertEquals("Syncing disabled", operation); diff --git a/k9mail/src/test/java/com/fsck/k9/controller/MessagingControllerTest.java b/k9mail/src/test/java/com/fsck/k9/controller/MessagingControllerTest.java index bfdcf8d26..9aad30463 100644 --- a/k9mail/src/test/java/com/fsck/k9/controller/MessagingControllerTest.java +++ b/k9mail/src/test/java/com/fsck/k9/controller/MessagingControllerTest.java @@ -183,7 +183,7 @@ public class MessagingControllerTest { @Test() public void clearFolderSynchronous_whenExceptionThrown_shouldAddErrorMessageInDebug() throws MessagingException { - if (K9.DEBUG) { + if (K9.isDebug()) { doThrow(new RuntimeException("Test")).when(localFolder).open(Folder.OPEN_MODE_RW); controller.clearFolderSynchronous(account, FOLDER_NAME, listener); diff --git a/k9mail/src/test/java/com/fsck/k9/mailstore/MigrationTest.java b/k9mail/src/test/java/com/fsck/k9/mailstore/MigrationTest.java index a5f74fc8f..21e589010 100644 --- a/k9mail/src/test/java/com/fsck/k9/mailstore/MigrationTest.java +++ b/k9mail/src/test/java/com/fsck/k9/mailstore/MigrationTest.java @@ -42,7 +42,7 @@ public class MigrationTest { @Before public void setUp() throws Exception { - K9.DEBUG = true; + K9.setDebug(true); ShadowLog.stream = System.out; ShadowSQLiteConnection.reset();