From 5396b0408e58f4d1f5de49c4be79acd6a6f31948 Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani Date: Fri, 6 Dec 2024 07:51:51 +0700 Subject: [PATCH] chore: Only keep 5 log files and 5 rolled log files --- .../yokai/core/RollingUniFileLogWriter.kt | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/yokai/core/RollingUniFileLogWriter.kt b/app/src/main/java/yokai/core/RollingUniFileLogWriter.kt index 631e966cfd..540ba531cb 100644 --- a/app/src/main/java/yokai/core/RollingUniFileLogWriter.kt +++ b/app/src/main/java/yokai/core/RollingUniFileLogWriter.kt @@ -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>() + 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() + } }