Skip to content

Commit 5396b04

Browse files
committed
chore: Only keep 5 log files and 5 rolled log files
1 parent 332f3f7 commit 5396b04

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

app/src/main/java/yokai/core/RollingUniFileLogWriter.kt

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import eu.kanade.tachiyomi.BuildConfig
1111
import eu.kanade.tachiyomi.util.system.launchIO
1212
import eu.kanade.tachiyomi.util.system.withIOContext
1313
import java.io.IOException
14+
import java.io.OutputStream
1415
import java.text.DateFormat
1516
import java.text.SimpleDateFormat
1617
import java.util.Date
@@ -27,7 +28,6 @@ import kotlinx.coroutines.currentCoroutineContext
2728
import kotlinx.coroutines.isActive
2829
import 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
4040
class 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

Comments
 (0)