From 7caa4a287fa7604166e5fc2cee814184e83a7d80 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 4 Oct 2022 11:34:17 +0200 Subject: [PATCH 1/5] work package: use Timber for loggin --- .../e/drive/work/AccountUserInfoWorker.java | 12 ++++++------ .../e/drive/work/CreateRemoteFolderWorker.java | 16 +++++++--------- .../e/drive/work/FirstStartWorker.java | 6 +++--- .../foundation/e/drive/work/FullScanWorker.java | 8 ++++---- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java index 957d5eeb..6fcc83c5 100644 --- a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java +++ b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java @@ -23,7 +23,6 @@ import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.util.Log; import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; @@ -48,13 +47,13 @@ import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.DavClientProvider; import foundation.e.drive.widgets.EDriveWidget; +import timber.log.Timber; /** * @author vincent Bourgmayer * @author TheScarastic */ public class AccountUserInfoWorker extends Worker { - private static final String TAG = AccountUserInfoWorker.class.getSimpleName(); public static final String UNIQUE_WORK_NAME = "AccountUserInfoWorker"; private final AccountManager accountManager; private final GetUserInfoRemoteOperation GetUserInfoRemoteOperation = new GetUserInfoRemoteOperation(); @@ -64,6 +63,7 @@ public class AccountUserInfoWorker extends Worker { public AccountUserInfoWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); + Timber.tag(AccountUserInfoWorker.class.getSimpleName()); mContext = context; accountManager = AccountManager.get(context); } @@ -100,7 +100,7 @@ public class AccountUserInfoWorker extends Worker { final String userId = userInfo.getId(); client.setUserId(userId); AccountManager.get(mContext).setUserData(account, ACCOUNT_USER_ID_KEY, userId); - Log.v(TAG, "UserId "+ userId +" saved for account"); + Timber.v("UserId %s saved for account", userId); } final Quota userQuota = userInfo.getQuota(); final double relativeQuota = userQuota.getRelative(); @@ -117,10 +117,10 @@ public class AccountUserInfoWorker extends Worker { accountManager.setUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY, "" + userQuota.getUsed()); addNotifAboutQuota(relativeQuota); - Log.d(TAG+"fetchUserInfo()", "Success"); + Timber.d("fetchUserInfo(): success"); return true; } - Log.d(TAG+"fetchUserInfo()", "Failure"); + Timber.d("fetchUserInfo(): failure"); return false; } @@ -196,7 +196,7 @@ public class AccountUserInfoWorker extends Worker { } } accountManager.setUserData(account, ACCOUNT_DATA_ALIAS_KEY, aliases); - Log.d(TAG+"fetchAliases()", "Success"); + Timber.d("fetchAliases(): success"); return true; } diff --git a/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java b/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java index 2d995c64..28005199 100644 --- a/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java +++ b/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java @@ -12,7 +12,6 @@ import android.accounts.Account; import android.accounts.AccountManager; import android.content.Context; import android.content.SharedPreferences; -import android.util.Log; import androidx.annotation.NonNull; import androidx.work.Data; @@ -31,6 +30,7 @@ import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.DavClientProvider; +import timber.log.Timber; /** * /!\ Doesn't require NextcloudClient yet @@ -38,7 +38,6 @@ import foundation.e.drive.utils.DavClientProvider; * @author Vincent Bourgmayer */ public class CreateRemoteFolderWorker extends Worker { - private static final String TAG = CreateRemoteFolderWorker.class.getSimpleName(); public static final String DATA_KEY_ID="id"; public static final String DATA_KEY_LIBELLE="libelle"; public static final String DATA_KEY_LOCAL_PATH="localPath"; @@ -52,6 +51,7 @@ public class CreateRemoteFolderWorker extends Worker { public CreateRemoteFolderWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); + Timber.tag(CreateRemoteFolderWorker.class.getSimpleName()); } @NonNull @@ -60,18 +60,18 @@ public class CreateRemoteFolderWorker extends Worker { final Context context = getApplicationContext(); final Account account = getAccount(); if (account == null) { - Log.e(TAG, "Can't get valid account: stop everything"); + Timber.e("doWork(): Can't get valid account"); return Result.failure(); } final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); if (!CommonUtils.haveNetworkConnection(context, meteredNetworkAllowed)) { - Log.e(TAG, "Can't create remote folder because there is no usable connection"); + Timber.d("doWork(): no usable connection"); return Result.retry(); } final SyncedFolder syncedFolder = getSyncedFolderFromData(); - Log.d(TAG, "doWork() for :"+syncedFolder.getLocalFolder()); + Timber.v("doWork() for : %s", syncedFolder.getLocalFolder()); final File folder = new File(syncedFolder.getLocalFolder() ); if (!folder.exists()) { folder.mkdirs(); @@ -81,7 +81,7 @@ public class CreateRemoteFolderWorker extends Worker { //final NextcloudClient client = DavClientProvider.getInstance().getNcClientInstance(account, context); final OwnCloudClient client = DavClientProvider.getInstance().getClientInstance(account, context); if (client == null) { - Log.e(TAG, "Can't get OwnCloudClient."); + Timber.d("doWork(): Can't get OwnCloudClient"); return Result.retry(); } @@ -90,9 +90,7 @@ public class CreateRemoteFolderWorker extends Worker { final RemoteOperationResult result = mkcolRequest.execute(client); if (result.isSuccess() || result.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS) { - if(DbHelper.insertSyncedFolder(syncedFolder, context) >= 0 ) { - Log.d(TAG, "Insertion in DB succeed"); - } + DbHelper.insertSyncedFolder(syncedFolder, context); return Result.success(); } diff --git a/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java index c112d36e..4898f79b 100644 --- a/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java +++ b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java @@ -12,7 +12,6 @@ import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER; import android.content.Context; import android.content.Intent; -import android.util.Log; import androidx.annotation.NonNull; import androidx.work.WorkManager; @@ -22,6 +21,7 @@ import androidx.work.WorkerParameters; import foundation.e.drive.EdriveApplication; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; +import timber.log.Timber; /** * This class start eDrive work after initialization. @@ -30,15 +30,15 @@ import foundation.e.drive.utils.CommonUtils; * @author Vincent Bourgmayer */ public class FirstStartWorker extends Worker { - private static final String TAG = FirstStartWorker.class.getSimpleName(); public FirstStartWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); + Timber.tag(FirstStartWorker.class.getSimpleName()); } @NonNull @Override public Result doWork() { - Log.d(TAG, "doWork()"); + Timber.v("doWork()"); final Context appContext = getApplicationContext(); appContext.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE) diff --git a/app/src/main/java/foundation/e/drive/work/FullScanWorker.java b/app/src/main/java/foundation/e/drive/work/FullScanWorker.java index bc895434..2dd503bf 100644 --- a/app/src/main/java/foundation/e/drive/work/FullScanWorker.java +++ b/app/src/main/java/foundation/e/drive/work/FullScanWorker.java @@ -13,7 +13,6 @@ import android.accounts.AccountManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.util.Log; import androidx.annotation.NonNull; import androidx.work.Worker; @@ -22,6 +21,7 @@ import androidx.work.WorkerParameters; import foundation.e.drive.services.ObserverService; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; +import timber.log.Timber; /** * As a first step, this class must replace foundation.e.drive.jobs.ScannerJob @@ -33,17 +33,17 @@ import foundation.e.drive.utils.CommonUtils; * @author Vincent Bourgmayer */ public class FullScanWorker extends Worker { - private final static String TAG = FullScanWorker.class.getSimpleName(); public final static String UNIQUE_WORK_NAME = "FullScan"; public FullScanWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); + Timber.tag(FullScanWorker.class.getSimpleName()); } @NonNull @Override public Result doWork() { - Log.d(TAG, "doWork(): going to send intent to ObserverService"); + Timber.v("doWork(): going to send intent to ObserverService"); final SharedPreferences prefs = getApplicationContext().getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); final String accountName = prefs.getString(AccountManager.KEY_ACCOUNT_NAME, ""); @@ -55,7 +55,7 @@ public class FullScanWorker extends Worker { final Intent observerServiceIntent = new Intent(this.getApplicationContext(), ObserverService.class); this.getApplicationContext().startService(observerServiceIntent); } else { - Log.w(TAG, "Intent for ObserverService not send : account is null or \"settings sync\" & \"media sync\" settings are disabled"); + Timber.d("Intent for ObserverService not send : account is null or \"settings sync\" & \"media sync\" settings are disabled"); } return Result.success(); -- GitLab From 265c147f9e5a932c9b12feae5b4e5d20aaedc924 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 4 Oct 2022 11:37:19 +0200 Subject: [PATCH 2/5] activity package: use Timber for loggin --- .../foundation/e/drive/activity/AccountsActivity.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java index 109936ae..7acb1ea8 100644 --- a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java +++ b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java @@ -24,7 +24,6 @@ import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.util.Log; import android.view.View; import androidx.appcompat.app.AppCompatActivity; @@ -38,9 +37,9 @@ import foundation.e.drive.databinding.ActivityAccountsBinding; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.DavClientProvider; import foundation.e.drive.widgets.EDriveWidget; +import timber.log.Timber; public class AccountsActivity extends AppCompatActivity { - private static final String TAG = AccountsActivity.class.getSimpleName(); public static final String NON_OFFICIAL_AVATAR_PATH = "/index.php/avatar/"; private static final String ACCOUNT_SETTINGS = @@ -51,6 +50,7 @@ public class AccountsActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Timber.tag(AccountsActivity.class.getSimpleName()); binding = ActivityAccountsBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); @@ -98,7 +98,7 @@ public class AccountsActivity extends AppCompatActivity { totalShownQuota = CommonUtils.humanReadableByteCountBin(totalQuotaLong); } } catch (NumberFormatException ignored) { - Log.i(TAG, "Bad totalQuotaLong " + totalQuota); + Timber.i("Bad totalQuotaLong " + totalQuota); } try { @@ -107,7 +107,7 @@ public class AccountsActivity extends AppCompatActivity { usedShownQuota = CommonUtils.humanReadableByteCountBin(usedQuotaLong); } } catch (NumberFormatException ignore) { - Log.i(TAG, "Bad usedQuotaLong " + usedQuota); + Timber.i("Bad usedQuotaLong " + usedQuota); } binding.plan.setText(getString(R.string.free_plan, totalShownQuota)); -- GitLab From 9f7deafb6a65480c70da10639673e80505477599 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 4 Oct 2022 12:12:06 +0200 Subject: [PATCH 3/5] Timber: add a ReleaseTree implementation --- app/src/main/AndroidManifest.xml | 1 + .../foundation/e/drive/EdriveApplication.java | 3 +- .../e/drive/receivers/DebugCmdReceiver.java | 9 +++- .../foundation/e/drive/utils/ReleaseTree.java | 43 +++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/foundation/e/drive/utils/ReleaseTree.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 41f9436c..2e436255 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -109,6 +109,7 @@ + diff --git a/app/src/main/java/foundation/e/drive/EdriveApplication.java b/app/src/main/java/foundation/e/drive/EdriveApplication.java index 493b027c..069c0509 100644 --- a/app/src/main/java/foundation/e/drive/EdriveApplication.java +++ b/app/src/main/java/foundation/e/drive/EdriveApplication.java @@ -21,6 +21,7 @@ import foundation.e.drive.FileObservers.RecursiveFileObserver; import foundation.e.drive.services.SynchronizationService; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; +import foundation.e.drive.utils.ReleaseTree; import timber.log.Timber; import static timber.log.Timber.DebugTree; @@ -42,7 +43,7 @@ public class EdriveApplication extends Application { Timber.plant(new DebugTree()); } else { //Not handled yet - //Timber.plant(new ReleaseTree()); + Timber.plant(new ReleaseTree()); } Timber.tag("EdriveApplication"); diff --git a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java index afb752cc..90a088bd 100644 --- a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java @@ -14,6 +14,7 @@ import android.util.Log; import foundation.e.drive.database.DbHelper; import foundation.e.drive.services.ObserverService; +import foundation.e.drive.utils.ReleaseTree; import timber.log.Timber; /** @@ -24,7 +25,8 @@ public class DebugCmdReceiver extends BroadcastReceiver { public static final String ACTION_FORCE_SYNC = "foundation.e.drive.action.FORCE_SYNC"; public static final String ACTION_DUMP_DATABASE = "foundation.e.drive.action.DUMP_DATABASE"; - + public static final String ACTION_FULL_LOG_ON_PROD ="foundation.e.drive.action.FULL_LOG_ON_PROD"; + private static final String FULL_LOG_ENABLE_KEY = "full_log_enable"; @Override public void onReceive(Context context, Intent intent) { Timber.tag(DebugCmdReceiver.class.getSimpleName()).v("onReceive"); @@ -37,6 +39,11 @@ public class DebugCmdReceiver extends BroadcastReceiver { Timber.d("Dump database intent received"); DbHelper.dumpDatabase(context); break; + case ACTION_FULL_LOG_ON_PROD: + final boolean allow_full_log = intent.getBooleanExtra(FULL_LOG_ENABLE_KEY, false); + ReleaseTree.allowDebugLogOnProd(allow_full_log); + Timber.d("Allow full log on prod: %s", allow_full_log); + break; default: break; } diff --git a/app/src/main/java/foundation/e/drive/utils/ReleaseTree.java b/app/src/main/java/foundation/e/drive/utils/ReleaseTree.java new file mode 100644 index 00000000..b1b2fbb6 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/utils/ReleaseTree.java @@ -0,0 +1,43 @@ +package foundation.e.drive.utils; + +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import timber.log.Timber; + +public class ReleaseTree extends Timber.Tree{ + private static boolean debugEnable = false; + + public static void allowDebugLogOnProd(boolean allow) { + debugEnable = allow; + } + + @Override + protected void log(int priority, @Nullable String tag, @NonNull String message, @Nullable Throwable throwable) { + if (!debugEnable && priority < Log.INFO ) { + return; + } + switch (priority) { + case Log.VERBOSE: + Log.v(tag, message, throwable); + break; + case Log.DEBUG: + Log.d(tag, message, throwable); + break; + case Log.INFO: + Log.i(tag, message, throwable); + break; + case Log.WARN: + Log.w(tag, message, throwable); + break; + case Log.ERROR: + Log.w(tag, message, throwable); + break; + case Log.ASSERT: + Log.wtf(tag, message, throwable); + break; + } + } +} -- GitLab From d7fd62a78c8d22af9863f29ce84371dd5e077a16 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Tue, 4 Oct 2022 13:36:33 +0000 Subject: [PATCH 4/5] Apply 1 suggestion(s) to 1 file(s) --- app/src/main/java/foundation/e/drive/utils/ReleaseTree.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/utils/ReleaseTree.java b/app/src/main/java/foundation/e/drive/utils/ReleaseTree.java index b1b2fbb6..26b62d75 100644 --- a/app/src/main/java/foundation/e/drive/utils/ReleaseTree.java +++ b/app/src/main/java/foundation/e/drive/utils/ReleaseTree.java @@ -7,7 +7,7 @@ import androidx.annotation.Nullable; import timber.log.Timber; -public class ReleaseTree extends Timber.Tree{ +public class ReleaseTree extends Timber.Tree { private static boolean debugEnable = false; public static void allowDebugLogOnProd(boolean allow) { -- GitLab From e37fba809edc98cb7f7396532be2e5f76d3173b3 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Tue, 4 Oct 2022 13:36:45 +0000 Subject: [PATCH 5/5] Apply 1 suggestion(s) to 1 file(s) --- .../java/foundation/e/drive/receivers/DebugCmdReceiver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java index 90a088bd..9e0e10b4 100644 --- a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java @@ -25,7 +25,7 @@ public class DebugCmdReceiver extends BroadcastReceiver { public static final String ACTION_FORCE_SYNC = "foundation.e.drive.action.FORCE_SYNC"; public static final String ACTION_DUMP_DATABASE = "foundation.e.drive.action.DUMP_DATABASE"; - public static final String ACTION_FULL_LOG_ON_PROD ="foundation.e.drive.action.FULL_LOG_ON_PROD"; + public static final String ACTION_FULL_LOG_ON_PROD = "foundation.e.drive.action.FULL_LOG_ON_PROD"; private static final String FULL_LOG_ENABLE_KEY = "full_log_enable"; @Override public void onReceive(Context context, Intent intent) { -- GitLab