mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
chore: Only keep 5 log files and 5 rolled log files
This commit is contained in:
parent
332f3f7ee6
commit
5396b0408e
1 changed files with 32 additions and 6 deletions
|
@ -11,6 +11,7 @@ import eu.kanade.tachiyomi.BuildConfig
|
|||
import eu.kanade.tachiyomi.util.system.launchIO
|
||||
import eu.kanade.tachiyomi.util.system.withIOContext
|
||||
import java.io.IOException
|
||||
import java.io.OutputStream
|
||||
import java.text.DateFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
|
@ -27,7 +28,6 @@ import kotlinx.coroutines.currentCoroutineContext
|
|||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.newSingleThreadContext
|
||||
|
||||
// FIXME: Only keep 5 logs "globally"
|
||||
/**
|
||||
* Copyright (c) 2024 Touchlab
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
|
@ -40,6 +40,7 @@ import kotlinx.coroutines.newSingleThreadContext
|
|||
class RollingUniFileLogWriter(
|
||||
private val logPath: UniFile,
|
||||
private val rollOnSize: Long = 10 * 1024 * 1024, // 10MB
|
||||
private val maxRolledLogFiles: Int = 5,
|
||||
private val maxLogFiles: Int = 5,
|
||||
private val messageStringFormatter: MessageStringFormatter = DefaultFormatter,
|
||||
private val messageDateFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault())
|
||||
|
@ -96,11 +97,11 @@ class RollingUniFileLogWriter(
|
|||
}
|
||||
|
||||
private fun rollLogs() {
|
||||
if (pathForLogIndex(maxLogFiles - 1)?.exists() == true) {
|
||||
pathForLogIndex(maxLogFiles - 1)?.delete()
|
||||
if (pathForLogIndex(maxRolledLogFiles - 1)?.exists() == true) {
|
||||
pathForLogIndex(maxRolledLogFiles - 1)?.delete()
|
||||
}
|
||||
|
||||
(0..<(maxLogFiles - 1)).reversed().forEach {
|
||||
(0..<(maxRolledLogFiles - 1)).reversed().forEach {
|
||||
val sourcePath = pathForLogIndex(it)
|
||||
val targetFileName = fileNameForLogIndex(it + 1)
|
||||
if (sourcePath?.exists() == true) {
|
||||
|
@ -116,7 +117,8 @@ class RollingUniFileLogWriter(
|
|||
|
||||
private fun fileNameForLogIndex(index: Int): String {
|
||||
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? {
|
||||
|
@ -130,7 +132,27 @@ class RollingUniFileLogWriter(
|
|||
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()
|
||||
|
||||
|
@ -158,4 +180,8 @@ class RollingUniFileLogWriter(
|
|||
}
|
||||
|
||||
private fun fileSize(path: UniFile?) = path?.length() ?: -1L
|
||||
|
||||
companion object {
|
||||
private val LOG_FILE_REGEX = """(\d+-\d+-\d+-${BuildConfig.BUILD_TYPE}) \(\d+\)\.log""".toRegex()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue