chore: Only keep 5 log files and 5 rolled log files

This commit is contained in:
Ahmad Ansori Palembani 2024-12-06 07:51:51 +07:00
parent 332f3f7ee6
commit 5396b0408e
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -11,6 +11,7 @@ import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.util.system.launchIO import eu.kanade.tachiyomi.util.system.launchIO
import eu.kanade.tachiyomi.util.system.withIOContext import eu.kanade.tachiyomi.util.system.withIOContext
import java.io.IOException import java.io.IOException
import java.io.OutputStream
import java.text.DateFormat import java.text.DateFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
@ -27,7 +28,6 @@ import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.newSingleThreadContext import kotlinx.coroutines.newSingleThreadContext
// FIXME: Only keep 5 logs "globally"
/** /**
* Copyright (c) 2024 Touchlab * Copyright (c) 2024 Touchlab
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
@ -40,6 +40,7 @@ import kotlinx.coroutines.newSingleThreadContext
class RollingUniFileLogWriter( class RollingUniFileLogWriter(
private val logPath: UniFile, private val logPath: UniFile,
private val rollOnSize: Long = 10 * 1024 * 1024, // 10MB private val rollOnSize: Long = 10 * 1024 * 1024, // 10MB
private val maxRolledLogFiles: Int = 5,
private val maxLogFiles: Int = 5, private val maxLogFiles: Int = 5,
private val messageStringFormatter: MessageStringFormatter = DefaultFormatter, private val messageStringFormatter: MessageStringFormatter = DefaultFormatter,
private val messageDateFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()) private val messageDateFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault())
@ -96,11 +97,11 @@ class RollingUniFileLogWriter(
} }
private fun rollLogs() { private fun rollLogs() {
if (pathForLogIndex(maxLogFiles - 1)?.exists() == true) { if (pathForLogIndex(maxRolledLogFiles - 1)?.exists() == true) {
pathForLogIndex(maxLogFiles - 1)?.delete() pathForLogIndex(maxRolledLogFiles - 1)?.delete()
} }
(0..<(maxLogFiles - 1)).reversed().forEach { (0..<(maxRolledLogFiles - 1)).reversed().forEach {
val sourcePath = pathForLogIndex(it) val sourcePath = pathForLogIndex(it)
val targetFileName = fileNameForLogIndex(it + 1) val targetFileName = fileNameForLogIndex(it + 1)
if (sourcePath?.exists() == true) { if (sourcePath?.exists() == true) {
@ -116,7 +117,8 @@ class RollingUniFileLogWriter(
private fun fileNameForLogIndex(index: Int): String { private fun fileNameForLogIndex(index: Int): String {
val date = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date()) val date = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date())
return if (index == 0) "${date}-${BuildConfig.BUILD_TYPE}.log" else "${date}-${BuildConfig.BUILD_TYPE} (${index}).log" val name = "${date}-${BuildConfig.BUILD_TYPE}"
return if (index == 0) "${name}.log" else "$name (${index}).log"
} }
private fun pathForLogIndex(index: Int, create: Boolean = false): UniFile? { private fun pathForLogIndex(index: Int, create: Boolean = false): UniFile? {
@ -130,7 +132,27 @@ class RollingUniFileLogWriter(
maybeRollLogs(fileSize(logFilePath)) maybeRollLogs(fileSize(logFilePath))
} }
fun openNewOutput() = pathForLogIndex(0, true)?.openOutputStream(true) fun openNewOutput(): OutputStream? {
val newLog = pathForLogIndex(0, true)
val dupes = mutableMapOf<String, List<UniFile>>()
logPath
.listFiles { file, filename ->
val match = LOG_FILE_REGEX.find(filename)
match?.groupValues?.get(1)?.let { key ->
dupes["${key}.log"] = dupes["${key}.log"].orEmpty() + listOf(file)
}
match == null
}
.orEmpty()
.sortedByDescending { it.name }
.drop(maxLogFiles - 1)
.forEach {
it.delete()
dupes[it.name]?.forEach { f -> f.delete() }
}
return newLog?.openOutputStream(true)
}
var currentLogSink = openNewOutput() var currentLogSink = openNewOutput()
@ -158,4 +180,8 @@ class RollingUniFileLogWriter(
} }
private fun fileSize(path: UniFile?) = path?.length() ?: -1L private fun fileSize(path: UniFile?) = path?.length() ?: -1L
companion object {
private val LOG_FILE_REGEX = """(\d+-\d+-\d+-${BuildConfig.BUILD_TYPE}) \(\d+\)\.log""".toRegex()
}
} }