CrashReportStore.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/logging/CrashReportStore.kt
1package eu.friedcutlet.yokan.services.logging23import eu.friedcutlet.yokan.services.files.LocalFileService45private const val CrashReportPath = "crash/latest.txt"6private const val MaxCrashReportBytes = 32 * 102478object CrashReportStore {9 fun record(files: LocalFileService, throwable: Throwable) {10 val report = redact(throwable.stackTraceToString()).encodeToByteArray()11 files.writeBytes(CrashReportPath(files), report.copyOf(report.size.coerceAtMost(MaxCrashReportBytes)))12 }1314 fun read(files: LocalFileService): String? = runCatching {15 files.readBytes(CrashReportPath(files)).decodeToString()16 .takeIf { it.isNotBlank() }17 }.getOrNull()1819 fun delete(files: LocalFileService) {20 runCatching { files.deleteRecursively(CrashReportPath(files)) }21 }2223 internal fun redact(value: String): String = value24 .replace(Regex("(?im)(authorization\\s*[:=]\\s*(?:bearer\\s+)?)[^\\s]+"), "$1[REDACTED]")25 .replace(Regex("(?i)(password|token|access_token|refresh_token)(\\s*[:=]\\s*)[^\\s,;]+"), "$1$2[REDACTED]")26 .replace(Regex("(?i)([?&](?:token|password|access_token|refresh_token)=)[^&\\s]+"), "$1[REDACTED]")2728 private fun CrashReportPath(files: LocalFileService): String =29 "${files.cacheRoot.trimEnd('/', 92.toChar())}/$CrashReportPath"30}31