From 035c5fdf657a1480d014f51afca4e2fced9908d2 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Wed, 30 Oct 2019 23:07:04 +0100 Subject: [PATCH 1/6] Better logging --- owncloudComLibrary/build.gradle | 1 + .../android/lib/common/utils/Log_OC.java | 190 ++---------------- .../android/lib/common/utils/LoggingHelper.kt | 24 +++ 3 files changed, 40 insertions(+), 175 deletions(-) create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/LoggingHelper.kt diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index 72db6d827..307438386 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -8,6 +8,7 @@ dependencies { api 'com.squareup.okhttp3:okhttp:3.12.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" api 'com.gitlab.ownclouders:dav4android:oc_support_1.0.1' + api 'com.github.hannesa2:Logcat:1.5.2' } allOpen { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/Log_OC.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/Log_OC.java index 47d49c07a..4724986cc 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/Log_OC.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/Log_OC.java @@ -1,212 +1,52 @@ package com.owncloud.android.lib.common.utils; -import android.util.Log; +import timber.log.Timber; -import com.owncloud.android.lib.BuildConfig; - -import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Locale; public class Log_OC { - private static final String SIMPLE_DATE_FORMAT = "yyyy/MM/dd HH:mm:ss"; - private static final String LOG_FOLDER_NAME = "log"; - private static final long MAX_FILE_SIZE = 2000000; // 2MB - - private static String mOwncloudDataFolderLog = "owncloud_log"; - - private static File mLogFile; - private static File mFolder; - private static BufferedWriter mBuf; - - private static String[] mLogFileNames = { - "currentLog" + BuildConfig.BUILD_TYPE + ".txt", - "olderLog" + BuildConfig.BUILD_TYPE + ".txt" - }; - private static boolean isMaxFileSizeReached = false; - private static boolean isEnabled = false; + private static String mOwncloudDataFolderLog; public static void setLogDataFolder(String logFolder) { mOwncloudDataFolderLog = logFolder; } - public static void i(String TAG, String message) { - Log.i(TAG, message); - appendLog("I: " + TAG + " : " + message); + public static void i(String tag, String message) { + Timber.i(message); } public static void d(String TAG, String message) { - Log.d(TAG, message); - appendLog("D: " + TAG + " : " + message); + Timber.d(message); } public static void d(String TAG, String message, Exception e) { - Log.d(TAG, message, e); - appendLog("D: " + TAG + " : " + message + " Exception : " + e.getStackTrace()); + Timber.d(e, message); } public static void e(String TAG, String message) { - Log.e(TAG, message); - appendLog("E: " + TAG + " : " + message); + Timber.d(message); } public static void e(String TAG, String message, Throwable e) { - Log.e(TAG, message, e); - appendLog("E: " + TAG + " : " + message + " Exception : " + e.getStackTrace()); + Timber.e(e, message); } public static void v(String TAG, String message) { - Log.v(TAG, message); - appendLog("V: " + TAG + " : " + message); + Timber.v(message); } public static void w(String TAG, String message) { - Log.w(TAG, message); - appendLog("W: " + TAG + " : " + message); - } - - /** - * Start doing logging - * - * @param storagePath : directory for keeping logs - */ - synchronized public static void startLogging(String storagePath) { - String logPath = storagePath + File.separator + mOwncloudDataFolderLog + File.separator + LOG_FOLDER_NAME; - mFolder = new File(logPath); - mLogFile = new File(mFolder + File.separator + mLogFileNames[0]); - - boolean isFileCreated = false; - - if (!mFolder.exists()) { - mFolder.mkdirs(); - isFileCreated = true; - Log.d("LOG_OC", "Log file created"); - } - - try { - - // Create the current log file if does not exist - mLogFile.createNewFile(); - mBuf = new BufferedWriter(new FileWriter(mLogFile, true)); - isEnabled = true; - - if (isFileCreated) { - appendPhoneInfo(); - } - - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (mBuf != null) { - try { - mBuf.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - - synchronized public static void stopLogging() { - try { - if (mBuf != null) { - mBuf.close(); - } - isEnabled = false; - - mLogFile = null; - mFolder = null; - mBuf = null; - isMaxFileSizeReached = false; - - } catch (IOException e) { - // Because we are stopping logging, we only log to Android console. - Log.e("OC_Log", "Closing log file failed: ", e); - } catch (Exception e) { - // This catch should never fire because we do null check on mBuf. - // But just for the sake of stability let's log this odd situation. - // Because we are stopping logging, we only log to Android console. - Log.e("OC_Log", "Stopping logging failed: ", e); - } + Timber.w(message); } - /** - * Delete history logging - */ - public static void deleteHistoryLogging() { - File folderLogs = new File(mFolder + File.separator); - if (folderLogs.isDirectory()) { - String[] myFiles = folderLogs.list(); - for (String fileName : myFiles) { - File fileInFolder = new File(folderLogs, fileName); - Log_OC.d("delete file", fileInFolder.getAbsoluteFile() + " " + fileInFolder.delete()); - } - } + public static void startLogging(String storagePath) { + LoggingHelper.INSTANCE.startLogging( + new File(storagePath+ File.separator + mOwncloudDataFolderLog), mOwncloudDataFolderLog); } - /** - * Append the info of the device - */ - private static void appendPhoneInfo() { - appendLog("Model : " + android.os.Build.MODEL); - appendLog("Brand : " + android.os.Build.BRAND); - appendLog("Product : " + android.os.Build.PRODUCT); - appendLog("Device : " + android.os.Build.DEVICE); - appendLog("Version-Codename : " + android.os.Build.VERSION.CODENAME); - appendLog("Version-Release : " + android.os.Build.VERSION.RELEASE); + public static void stopLogging() { + LoggingHelper.INSTANCE.stopLogging(); } - /** - * Append to the log file the info passed - * - * @param text : text for adding to the log file - */ - synchronized private static void appendLog(String text) { - - if (isEnabled) { - - if (isMaxFileSizeReached) { - - // Move current log file info to another file (old logs) - File olderFile = new File(mFolder + File.separator + mLogFileNames[1]); - if (mLogFile.exists()) { - mLogFile.renameTo(olderFile); - } - - // Construct a new file for current log info - mLogFile = new File(mFolder + File.separator + mLogFileNames[0]); - isMaxFileSizeReached = false; - } - - String timeStamp = new SimpleDateFormat(SIMPLE_DATE_FORMAT, Locale.ENGLISH).format(Calendar.getInstance().getTime()); - - try { - mBuf = new BufferedWriter(new FileWriter(mLogFile, true)); - mBuf.newLine(); - mBuf.write(timeStamp + " " + text); - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - mBuf.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - // Check if current log file size is bigger than the max file size defined - if (mLogFile.length() > MAX_FILE_SIZE) { - isMaxFileSizeReached = true; - } - } - } - - public static String[] getLogFileNames() { - return mLogFileNames; - } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/LoggingHelper.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/LoggingHelper.kt new file mode 100644 index 000000000..23d9125bc --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/LoggingHelper.kt @@ -0,0 +1,24 @@ +package com.owncloud.android.lib.common.utils + +import info.hannes.timber.FileLoggingTree +import info.hannes.timber.fileLoggingTree +import timber.log.Timber +import java.io.File + +object LoggingHelper { + + fun startLogging(directory: File, storagePath: String) { + Timber.forest().fileLoggingTree()?.let { + Timber.forest().drop(Timber.forest().indexOf(it)) + } + if (!directory.exists()) + directory.mkdirs() + Timber.plant(FileLoggingTree(directory, filename = storagePath, delegator = Log_OC::class.java)) + } + + fun stopLogging() { + Timber.forest().fileLoggingTree()?.let { + Timber.forest().drop(Timber.forest().indexOf(it)) + } + } +} From d32f35147f6efacce41099e41c7d1271fd20a10f Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Tue, 12 Nov 2019 08:26:25 +0100 Subject: [PATCH 2/6] remove some pointless warnings --- owncloudComLibrary/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index 307438386..8d63d2b9d 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -8,7 +8,7 @@ dependencies { api 'com.squareup.okhttp3:okhttp:3.12.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" api 'com.gitlab.ownclouders:dav4android:oc_support_1.0.1' - api 'com.github.hannesa2:Logcat:1.5.2' + api 'com.github.hannesa2:Logcat:1.5.3' } allOpen { From b084ca7c03d7989ad222b2443a35b9828f5304e6 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Fri, 15 Nov 2019 08:16:14 +0100 Subject: [PATCH 3/6] introduce deprecated old and new methods --- .../android/lib/common/utils/Log_OC.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/Log_OC.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/Log_OC.java index 4724986cc..831076b6c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/Log_OC.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/Log_OC.java @@ -12,30 +12,65 @@ public static void setLogDataFolder(String logFolder) { mOwncloudDataFolderLog = logFolder; } + public static void i(String message) { + Timber.i(message); + } + + public static void d(String message) { + Timber.d(message); + } + + public static void d(String message, Exception e) { + Timber.d(e, message); + } + + public static void e(String message) { + Timber.e(message); + } + + public static void e(String message, Throwable e) { + Timber.e(e, message); + } + + public static void v(String message) { + Timber.v(message); + } + + public static void w(String message) { + Timber.w(message); + } + + @Deprecated public static void i(String tag, String message) { Timber.i(message); } + @Deprecated public static void d(String TAG, String message) { Timber.d(message); } + @Deprecated public static void d(String TAG, String message, Exception e) { Timber.d(e, message); } + @Deprecated public static void e(String TAG, String message) { - Timber.d(message); + Timber.e(message); } + @Deprecated public static void e(String TAG, String message, Throwable e) { Timber.e(e, message); } + @Deprecated public static void v(String TAG, String message) { Timber.v(message); } + @Deprecated public static void w(String TAG, String message) { Timber.w(message); } From fa4db38a416020687b32e544bdb5f2da59c68bb0 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Wed, 20 Nov 2019 07:51:07 +0100 Subject: [PATCH 4/6] fix progress dialog and 'No adapter attached' --- owncloudComLibrary/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index 8d63d2b9d..5df7c4811 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -8,7 +8,7 @@ dependencies { api 'com.squareup.okhttp3:okhttp:3.12.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" api 'com.gitlab.ownclouders:dav4android:oc_support_1.0.1' - api 'com.github.hannesa2:Logcat:1.5.3' + api 'com.github.hannesa2:Logcat:1.5.4' } allOpen { From 10a7bdbc4d06f0ff4f6ac477095eb8515f561293 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Fri, 22 Nov 2019 23:33:47 +0100 Subject: [PATCH 5/6] delete all *.log in directory --- owncloudComLibrary/build.gradle | 2 +- .../com/owncloud/android/lib/common/utils/LoggingHelper.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index 5df7c4811..0e8238d27 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -8,7 +8,7 @@ dependencies { api 'com.squareup.okhttp3:okhttp:3.12.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" api 'com.gitlab.ownclouders:dav4android:oc_support_1.0.1' - api 'com.github.hannesa2:Logcat:1.5.4' + api 'com.github.hannesa2:Logcat:1.5.5' } allOpen { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/LoggingHelper.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/LoggingHelper.kt index 23d9125bc..edb93b66d 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/LoggingHelper.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/utils/LoggingHelper.kt @@ -8,7 +8,7 @@ import java.io.File object LoggingHelper { fun startLogging(directory: File, storagePath: String) { - Timber.forest().fileLoggingTree()?.let { + fileLoggingTree()?.let { Timber.forest().drop(Timber.forest().indexOf(it)) } if (!directory.exists()) @@ -17,7 +17,7 @@ object LoggingHelper { } fun stopLogging() { - Timber.forest().fileLoggingTree()?.let { + fileLoggingTree()?.let { Timber.forest().drop(Timber.forest().indexOf(it)) } } From 5bf99df58afb0e3803a6185631832bab8dceddda Mon Sep 17 00:00:00 2001 From: davigonz Date: Tue, 26 Nov 2019 16:47:15 +0100 Subject: [PATCH 6/6] Update Logcat library dependency --- owncloudComLibrary/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index 0e8238d27..28c9a5d1b 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -8,7 +8,7 @@ dependencies { api 'com.squareup.okhttp3:okhttp:3.12.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" api 'com.gitlab.ownclouders:dav4android:oc_support_1.0.1' - api 'com.github.hannesa2:Logcat:1.5.5' + api 'com.github.hannesa2:Logcat:1.5.6' } allOpen {