diff --git a/kotlinlog/build.gradle b/kotlinlog/build.gradle index d21f219..c2054de 100644 --- a/kotlinlog/build.gradle +++ b/kotlinlog/build.gradle @@ -12,7 +12,7 @@ android { minSdk 21 targetSdk 34 versionCode 8 - versionName "2.2.14" + versionName "2.2.16" buildConfigField 'int', 'VERSION_CODE', "$versionCode" buildConfigField 'String', 'VERSION_NAME', "\"$versionName\"" consumerProguardFile('proguard-rules.pro') diff --git a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/LogInternal.kt b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/LogInternal.kt index 0009b79..3261e74 100644 --- a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/LogInternal.kt +++ b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/LogInternal.kt @@ -66,7 +66,6 @@ private fun emptyCheck(): Boolean { /** * @return Returns method name by reflexion */ -@Suppress("UNREACHABLE_CODE") private fun getMethodStackTraceElement(): StackTraceElement { val ste = Thread.currentThread().stackTrace var found = false diff --git a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/FileLogger.kt b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/FileLogger.kt index 87f9468..fb2b6b5 100644 --- a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/FileLogger.kt +++ b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/FileLogger.kt @@ -1,6 +1,7 @@ package quanti.com.kotlinlog.file import android.content.Context +import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -71,10 +72,11 @@ class FileLogger( override fun logSync(androidLogLevel: Int, tag: String, methodName: String, text: String) { val formattedString = getFormatedString(appName, androidLogLevel, tag, methodName, text) - logFile.write(formattedString) + blockingQueue.add(formattedString) + forceWrite() } - + @OptIn(DelicateCoroutinesApi::class) override fun logThrowable(androidLogLevel: Int, tag: String, methodName: String, text: String, t: Throwable) { val formattedString = getFormatedString(appName, androidLogLevel, tag, methodName, text) @@ -98,8 +100,9 @@ class FileLogger( } } - //we want errors to be sync - logFile.write(finalText) + blockingQueue.add(finalText) + // write errors and queued logs immediately + forceWrite() } override fun cleanResources() { @@ -137,22 +140,20 @@ class FileLogger( * Deletes all stored logs in app local memory */ fun deleteLogsForIdentifier(appCtx: Context, logIdentifier: String) { - appCtx - .logFilesDir - .listFiles() - .filter { it.name.contains(logIdentifier) } - .filter { it.name.endsWith(".log") } - .forEach { it.delete() } + appCtx.logFilesDir + .listFiles() + ?.filter { it.name.contains(logIdentifier) } + ?.filter { it.name.endsWith(".log") } + ?.forEach { it.delete() } } /** * Deletes all stored logs in app local memory */ fun deleteAllLogs(appCtx: Context) { - appCtx - .logFilesDir - .listFiles() - .filter { it.name.endsWith(".log") } - .forEach { it.delete() } + appCtx.logFilesDir + .listFiles() + ?.filter { it.name.endsWith(".log") } + ?.forEach { it.delete() } } } diff --git a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/AbstractLogFile.kt b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/AbstractLogFile.kt index bc0051a..3c59429 100644 --- a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/AbstractLogFile.kt +++ b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/AbstractLogFile.kt @@ -57,7 +57,7 @@ abstract class AbstractLogFile( createNewFile() } while (queue.isNotEmpty()) { - fos.write(queue.poll().toByteArray()) + fos.write(queue.poll()?.toByteArray()) } } } @@ -94,7 +94,7 @@ abstract class AbstractLogFile( /** * Returns all files that corresponds to this logger */ - protected fun listOfLoggerFiles() = ctx.logFilesDir.listFiles().filter { it.name.contains(logIdentifier) } + protected fun listOfLoggerFiles() = ctx.logFilesDir.listFiles()?.filter { it.name.contains(logIdentifier) } /** diff --git a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/CircleLogFile.kt b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/CircleLogFile.kt index 53bee6a..450d694 100644 --- a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/CircleLogFile.kt +++ b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/CircleLogFile.kt @@ -48,13 +48,13 @@ class CircleLogFile( } //remove all zips - listOfLoggerFiles().deleteAllZips() + listOfLoggerFiles()?.deleteAllZips() //remove all files that exceeds specified limit listOfLoggerFiles() - .sortByAge() - .drop(bundle.numOfFiles) - .deleteAll() + ?.sortByAge() + ?.drop(bundle.numOfFiles) + ?.deleteAll() } } \ No newline at end of file diff --git a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/DayLogFile.kt b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/DayLogFile.kt index b7cfe9f..f7c4677 100644 --- a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/DayLogFile.kt +++ b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/DayLogFile.kt @@ -30,16 +30,16 @@ class DayLogFile( //switch to new file if needed loga("fileAge: " + file.fileAge()) loga("fileName: " + file.name) - if (file.fileAge() > 0 || !file.name!!.contains(getFormattedFileNameForDayTemp())) { + if (file.fileAge() > 0 || !file.name.contains(getFormattedFileNameForDayTemp())) { createNewFile() } //remove all zips - listOfLoggerFiles().deleteAllZips() + listOfLoggerFiles()?.deleteAllZips() //remove all files older than x days loga("max days saved: $maxDays") - listOfLoggerFiles().deleteAllOldFiles(maxDays) + listOfLoggerFiles()?.deleteAllOldFiles(maxDays) } diff --git a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/StrictCircleLogFile.kt b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/StrictCircleLogFile.kt index ee6603f..d7b780d 100644 --- a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/StrictCircleLogFile.kt +++ b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/file/StrictCircleLogFile.kt @@ -44,7 +44,7 @@ class StrictCircleLogFile( override fun writeBatch(queue: LinkedBlockingQueue) { lock.withLock { while (queue.isNotEmpty()) { - writeWithCheck(queue.poll()) + queue.poll()?.let { writeWithCheck(it) } } } } @@ -64,13 +64,13 @@ class StrictCircleLogFile( override fun cleanFolder() { //remove all zips - listOfLoggerFiles().deleteAllZips() + listOfLoggerFiles()?.deleteAllZips() //remove all files that exceeds specified limit listOfLoggerFiles() - .sortByAge() - .drop(bundle.numOfFiles) - .deleteAll() + ?.sortByAge() + ?.drop(bundle.numOfFiles) + ?.deleteAll() } } \ No newline at end of file