@@ -11,6 +11,7 @@ import eu.kanade.tachiyomi.BuildConfig
1111import eu.kanade.tachiyomi.util.system.launchIO
1212import eu.kanade.tachiyomi.util.system.withIOContext
1313import java.io.IOException
14+ import java.io.OutputStream
1415import java.text.DateFormat
1516import java.text.SimpleDateFormat
1617import java.util.Date
@@ -27,7 +28,6 @@ import kotlinx.coroutines.currentCoroutineContext
2728import kotlinx.coroutines.isActive
2829import kotlinx.coroutines.newSingleThreadContext
2930
30- // FIXME: Only keep 5 logs "globally"
3131/* *
3232 * Copyright (c) 2024 Touchlab
3333 * SPDX-License-Identifier: Apache-2.0
@@ -40,6 +40,7 @@ import kotlinx.coroutines.newSingleThreadContext
4040class RollingUniFileLogWriter (
4141 private val logPath : UniFile ,
4242 private val rollOnSize : Long = 10 * 1024 * 1024 , // 10MB
43+ private val maxRolledLogFiles : Int = 5 ,
4344 private val maxLogFiles : Int = 5 ,
4445 private val messageStringFormatter : MessageStringFormatter = DefaultFormatter ,
4546 private val messageDateFormat : DateFormat = SimpleDateFormat ("yyyy-MM -dd HH :mm:ss.SSS ", Locale .getDefault())
@@ -96,11 +97,11 @@ class RollingUniFileLogWriter(
9697 }
9798
9899 private fun rollLogs () {
99- if (pathForLogIndex(maxLogFiles - 1 )?.exists() == true ) {
100- pathForLogIndex(maxLogFiles - 1 )?.delete()
100+ if (pathForLogIndex(maxRolledLogFiles - 1 )?.exists() == true ) {
101+ pathForLogIndex(maxRolledLogFiles - 1 )?.delete()
101102 }
102103
103- (0 .. < (maxLogFiles - 1 )).reversed().forEach {
104+ (0 .. < (maxRolledLogFiles - 1 )).reversed().forEach {
104105 val sourcePath = pathForLogIndex(it)
105106 val targetFileName = fileNameForLogIndex(it + 1 )
106107 if (sourcePath?.exists() == true ) {
@@ -116,7 +117,8 @@ class RollingUniFileLogWriter(
116117
117118 private fun fileNameForLogIndex (index : Int ): String {
118119 val date = SimpleDateFormat (" yyyy-MM-dd" , Locale .getDefault()).format(Date ())
119- return if (index == 0 ) " ${date} -${BuildConfig .BUILD_TYPE } .log" else " ${date} -${BuildConfig .BUILD_TYPE } (${index} ).log"
120+ val name = " ${date} -${BuildConfig .BUILD_TYPE } "
121+ return if (index == 0 ) " ${name} .log" else " $name (${index} ).log"
120122 }
121123
122124 private fun pathForLogIndex (index : Int , create : Boolean = false): UniFile ? {
@@ -130,7 +132,27 @@ class RollingUniFileLogWriter(
130132 maybeRollLogs(fileSize(logFilePath))
131133 }
132134
133- fun openNewOutput () = pathForLogIndex(0 , true )?.openOutputStream(true )
135+ fun openNewOutput (): OutputStream ? {
136+ val newLog = pathForLogIndex(0 , true )
137+ val dupes = mutableMapOf<String , List <UniFile >>()
138+ logPath
139+ .listFiles { file, filename ->
140+ val match = LOG_FILE_REGEX .find(filename)
141+ match?.groupValues?.get(1 )?.let { key ->
142+ dupes[" ${key} .log" ] = dupes[" ${key} .log" ].orEmpty() + listOf (file)
143+ }
144+
145+ match == null
146+ }
147+ .orEmpty()
148+ .sortedByDescending { it.name }
149+ .drop(maxLogFiles - 1 )
150+ .forEach {
151+ it.delete()
152+ dupes[it.name]?.forEach { f -> f.delete() }
153+ }
154+ return newLog?.openOutputStream(true )
155+ }
134156
135157 var currentLogSink = openNewOutput()
136158
@@ -158,4 +180,8 @@ class RollingUniFileLogWriter(
158180 }
159181
160182 private fun fileSize (path : UniFile ? ) = path?.length() ? : - 1L
183+
184+ companion object {
185+ private val LOG_FILE_REGEX = """ (\d+-\d+-\d+-${BuildConfig .BUILD_TYPE } ) \(\d+\)\.log""" .toRegex()
186+ }
161187}
0 commit comments