优化
This commit is contained in:
@@ -45,19 +45,18 @@ class CrashHandler private constructor(private val context: Context) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getCrashReportFiles(context: Context): Array<File> {
|
//fun getCrashReportFiles(context: Context): Array<File> {
|
||||||
// val crashDir = File(context.getExternalFilesDir(null), CRASH_REPORTS_DIR)
|
// val crashDir = getCrashDir()
|
||||||
val crashDir = File(context.filesDir, CRASH_REPORTS_DIR)
|
// return if (crashDir.exists() && crashDir.isDirectory) {
|
||||||
return if (crashDir.exists() && crashDir.isDirectory) {
|
// crashDir.listFiles { _, name -> name.endsWith(".log") } ?: emptyArray()
|
||||||
crashDir.listFiles { _, name -> name.endsWith(".log") } ?: emptyArray()
|
// } else {
|
||||||
} else {
|
// emptyArray()
|
||||||
emptyArray()
|
// }
|
||||||
}
|
//}
|
||||||
}
|
//
|
||||||
|
//fun clearCrashReports(context: Context) {
|
||||||
fun clearCrashReports(context: Context) {
|
// getCrashReportFiles(context).forEach { it.delete() }
|
||||||
getCrashReportFiles(context).forEach { it.delete() }
|
//}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
|
private val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
|
||||||
@@ -69,6 +68,11 @@ class CrashHandler private constructor(private val context: Context) :
|
|||||||
override fun uncaughtException(thread: Thread, ex: Throwable) {
|
override fun uncaughtException(thread: Thread, ex: Throwable) {
|
||||||
handleException(thread, ex)
|
handleException(thread, ex)
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(3000)
|
||||||
|
} catch (e: InterruptedException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
// 如果系统提供了默认的异常处理器,则交给系统去结束程序
|
// 如果系统提供了默认的异常处理器,则交给系统去结束程序
|
||||||
// 否则自己结束程序
|
// 否则自己结束程序
|
||||||
defaultHandler?.uncaughtException(thread, ex) ?: run {
|
defaultHandler?.uncaughtException(thread, ex) ?: run {
|
||||||
@@ -204,11 +208,8 @@ class CrashHandler private constructor(private val context: Context) :
|
|||||||
val time = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.getDefault()).format(Date())
|
val time = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.getDefault()).format(Date())
|
||||||
val fileName = "crash_$time.log"
|
val fileName = "crash_$time.log"
|
||||||
|
|
||||||
// val crashDir = File(context.getExternalFilesDir(null), CRASH_REPORTS_DIR)
|
val crashDir = getCrashDir()
|
||||||
val crashDir = File(context.filesDir, CRASH_REPORTS_DIR)
|
|
||||||
if (!crashDir.exists()) {
|
|
||||||
crashDir.mkdirs()
|
|
||||||
}
|
|
||||||
val crashFile = File(crashDir, fileName)
|
val crashFile = File(crashDir, fileName)
|
||||||
FileOutputStream(crashFile).use { it.write(crashInfo.toByteArray()) }
|
FileOutputStream(crashFile).use { it.write(crashInfo.toByteArray()) }
|
||||||
|
|
||||||
@@ -222,7 +223,7 @@ class CrashHandler private constructor(private val context: Context) :
|
|||||||
* 清理旧的崩溃日志
|
* 清理旧的崩溃日志
|
||||||
*/
|
*/
|
||||||
fun cleanupOldCrashReports(maxAgeDays: Int = 7) {
|
fun cleanupOldCrashReports(maxAgeDays: Int = 7) {
|
||||||
val crashDir = File(context.filesDir, CRASH_REPORTS_DIR)
|
val crashDir = getCrashDir()
|
||||||
if (!crashDir.exists() || !crashDir.isDirectory) return
|
if (!crashDir.exists() || !crashDir.isDirectory) return
|
||||||
|
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
@@ -234,4 +235,18 @@ class CrashHandler private constructor(private val context: Context) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getCrashDir():File {
|
||||||
|
//val crashDir = File(context.getExternalFilesDir(null), CRASH_REPORTS_DIR)
|
||||||
|
var crashDir = File(context.filesDir, CRASH_REPORTS_DIR)
|
||||||
|
if (!crashDir.exists()) {
|
||||||
|
crashDir.mkdirs()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(crashDir.exists())) {
|
||||||
|
crashDir = File(context.cacheDir, CRASH_REPORTS_DIR)
|
||||||
|
}
|
||||||
|
return crashDir
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user