From d74ae2f846aa392bd6690a8b8ddc1eccd7fcf3fd Mon Sep 17 00:00:00 2001 From: fengge <844143714@qq.com> Date: Thu, 23 Apr 2026 11:41:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91=E8=80=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/stand/standapp/utils/LogManager.kt | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/stand/standapp/utils/LogManager.kt b/app/src/main/java/com/stand/standapp/utils/LogManager.kt index 0dece91..0ce4483 100644 --- a/app/src/main/java/com/stand/standapp/utils/LogManager.kt +++ b/app/src/main/java/com/stand/standapp/utils/LogManager.kt @@ -127,33 +127,38 @@ object LogManager { } fun zipLogs(context: Context): String? { - val dir = logDir ?: return null - val zipFile = File(context.getExternalFilesDir(null), "logs_${System.currentTimeMillis()}.zip") - val tombstoneDir = File(context.getExternalFilesDir(null), "tombstones") + val zipFile = File(context.getExternalFilesDir(null), "full_logs_${System.currentTimeMillis()}.zip") + val logFolder = logDir // 这通常是 files/logs + val tombstoneFolder = File(context.getExternalFilesDir(null), "tombstones") return try { ZipOutputStream(FileOutputStream(zipFile)).use { zos -> - // 1. 打包普通日志 - dir.listFiles()?.forEach { file -> - if (file.isFile && (file.name.endsWith(".log") || file.name.endsWith(".txt"))) { - zos.putNextEntry(ZipEntry("logs/${file.name}")) - FileInputStream(file).use { it.copyTo(zos) } - zos.closeEntry() - } - } - // 2. 打包 Native 崩溃文件 (Tombstones) - if (tombstoneDir.exists()) { - tombstoneDir.listFiles()?.forEach { file -> - if (file.isFile) { - zos.putNextEntry(ZipEntry("native/${file.name}")) - FileInputStream(file).use { it.copyTo(zos) } - zos.closeEntry() - } - } + // 1. 打包 logs 文件夹 + logFolder?.let { addFolderToZip(zos, it, "logs") } + + // 2. 打包 tombstones 文件夹 + if (tombstoneFolder.exists()) { + addFolderToZip(zos, tombstoneFolder, "tombstones") } } zipFile.absolutePath - } catch (e: Exception) { null } + } catch (e: Exception) { + android.util.Log.e("LogManager", "Zip failed", e) + null + } + } + + private fun addFolderToZip(zos: ZipOutputStream, folder: File, parentName: String) { + folder.listFiles()?.forEach { file -> + if (file.isFile) { + try { + val entry = ZipEntry("$parentName/${file.name}") + zos.putNextEntry(entry) + FileInputStream(file).use { it.copyTo(zos) } + zos.closeEntry() + } catch (e: Exception) {} + } + } } fun uploadLogs(context: Context, callback: (Boolean, String) -> Unit) {