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) {