Skip to content

Commit

Permalink
Merge pull request #28 from Qase/FEAT/getLogFilesOption
Browse files Browse the repository at this point in the history
FEAT: add option to get list of log files
  • Loading branch information
AnezkaQuanti committed Apr 19, 2022
2 parents 70d266d + 22aa379 commit e7e1117
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions kotlinlog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 28
versionCode 3
versionName "2.2.8"
versionCode 4
versionName "2.2.9"
buildConfigField 'int', 'VERSION_CODE', "$versionCode"
buildConfigField 'String', 'VERSION_NAME', "\"$versionName\""
consumerProguardFile('proguard-rules.pro')
Expand Down
34 changes: 19 additions & 15 deletions kotlinlog/src/main/kotlin/quanti/com/kotlinlog/utils/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,23 @@ fun File.copyLogsTOSDCard(context: Context, sdCardFolderName: String = "KotlinLo
*/

fun getZipOfLogs(appCtx: Context, fileAge: Int = 4, extraFiles: List<File> = arrayListOf()): File {
val listOfFiles = getLogFiles(appCtx, fileAge).toMutableList()
listOfFiles.addAll(extraFiles)

val zipFileName = "Logs_${getFormattedFileNameForDayTemp()}_${appCtx.getApplicationName()}.zip"
val zipFile = File(appCtx.logFilesDir, zipFileName)
zipFile.createNewFile() //create file if not exists
listOfFiles.zip(zipFile)

return zipFile
}

fun getLogFiles(appCtx: Context, fileAge: Int = 4): List<File> {
//first perform clean of mess
appCtx.logFilesDir.listFiles().deleteAllZips()
appCtx.logFilesDir.listFiles().deleteAllOldFiles(fileAge)
appCtx.logFilesDir.listFiles()?.deleteAllZips()
appCtx.logFilesDir.listFiles()?.deleteAllOldFiles(fileAge)

if (appCtx.logFilesDir.listFiles().isEmpty()) {
if (appCtx.logFilesDir.listFiles().isNullOrEmpty()) {
throw FileNotFoundException("No files were found")
}

Expand All @@ -118,17 +130,9 @@ fun getZipOfLogs(appCtx: Context, fileAge: Int = 4, extraFiles: List<File> = arr
loga("Force write")
forceWrite()

val zipFileName = "Logs_${getFormattedFileNameForDayTemp()}_${appCtx.getApplicationName()}.zip"
val zipFile = File(appCtx.logFilesDir, zipFileName)
zipFile.createNewFile() //create file if not exists

val listOfFiles = appCtx.logFilesDir
return appCtx.logFilesDir
.listFiles()
.filter { it.isFile } //take only files
.filter { it.name.contains(".log", ignoreCase = true) }
.toMutableList()
listOfFiles.addAll(extraFiles)
listOfFiles.zip(zipFile)
return zipFile
?.filter { it.isFile } //take only files
?.filter { it.name.contains(".log", ignoreCase = true) }
?: listOf()
}

0 comments on commit e7e1117

Please sign in to comment.