From 053300919c01f8dcc4c6fe9d6e9e88cac29d8111 Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Tue, 25 Feb 2020 20:38:55 +0800 Subject: [PATCH] add crash reporter --- .../com/github/kr328/clash/MainApplication.kt | 5 +++ .../github/kr328/clash/dump/LogcatDumper.kt | 4 +- .../github/kr328/clash/remote/ClashClient.kt | 45 ++++++++++++------- .../kr328/clash/remote/ProfileClient.kt | 24 +++++++--- .../com/github/kr328/clash/remote/Utils.kt | 24 ++++++++++ core/src/main/golang/clash | 2 +- 6 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/com/github/kr328/clash/remote/Utils.kt diff --git a/app/src/main/java/com/github/kr328/clash/MainApplication.kt b/app/src/main/java/com/github/kr328/clash/MainApplication.kt index dd869e0..ad403b2 100644 --- a/app/src/main/java/com/github/kr328/clash/MainApplication.kt +++ b/app/src/main/java/com/github/kr328/clash/MainApplication.kt @@ -7,7 +7,12 @@ import com.github.kr328.clash.remote.Broadcasts import com.github.kr328.clash.remote.Remote import com.microsoft.appcenter.AppCenter import com.microsoft.appcenter.analytics.Analytics +import com.microsoft.appcenter.crashes.AbstractCrashesListener import com.microsoft.appcenter.crashes.Crashes +import com.microsoft.appcenter.crashes.CrashesListener +import com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog +import com.microsoft.appcenter.crashes.model.ErrorReport +import java.lang.Exception @Suppress("unused") class MainApplication : Application() { diff --git a/app/src/main/java/com/github/kr328/clash/dump/LogcatDumper.kt b/app/src/main/java/com/github/kr328/clash/dump/LogcatDumper.kt index c03ef7c..0358a78 100644 --- a/app/src/main/java/com/github/kr328/clash/dump/LogcatDumper.kt +++ b/app/src/main/java/com/github/kr328/clash/dump/LogcatDumper.kt @@ -1,7 +1,5 @@ package com.github.kr328.clash.dump -import com.github.kr328.clash.core.utils.Log - object LogcatDumper { fun dump(): List { return try { @@ -32,4 +30,4 @@ object LogcatDumper { emptyList() } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/github/kr328/clash/remote/ClashClient.kt b/app/src/main/java/com/github/kr328/clash/remote/ClashClient.kt index edbfae8..7974f03 100644 --- a/app/src/main/java/com/github/kr328/clash/remote/ClashClient.kt +++ b/app/src/main/java/com/github/kr328/clash/remote/ClashClient.kt @@ -12,39 +12,50 @@ import kotlinx.coroutines.withContext class ClashClient(val service: IClashManager) { suspend fun setSelectProxy(name: String, proxy: String): Boolean = withContext(Dispatchers.IO) { - return@withContext service.setSelectProxy(name, proxy) + Utils.callRemote { + service.setSelectProxy(name, proxy) + } } suspend fun startHealthCheck(group: String) = withContext(Dispatchers.IO) { - CompletableDeferred().apply { - service.startHealthCheck(group, object : IStreamCallback.Stub() { - override fun complete() { - this@apply.complete(Unit) - } + Utils.callRemote { + CompletableDeferred().apply { + service.startHealthCheck(group, object : IStreamCallback.Stub() { + override fun complete() { + this@apply.complete(Unit) + } - override fun completeExceptionally(reason: String?) { - this@apply.completeExceptionally(RemoteException(reason)) - } + override fun completeExceptionally(reason: String?) { + this@apply.completeExceptionally(RemoteException(reason)) + } - override fun send(data: ParcelableContainer?) {} - }) + override fun send(data: ParcelableContainer?) {} + }) + } } }.await() suspend fun queryAllProxyGroups(): List = withContext(Dispatchers.IO) { - service.queryAllProxies().list + Utils.callRemote { + service.queryAllProxies().list + } } suspend fun queryGeneral(): General = withContext(Dispatchers.IO) { - service.queryGeneral() + Utils.callRemote { + service.queryGeneral() + } } - suspend fun queryBandwidth(): Long = - withContext(Dispatchers.IO) { - service.queryBandwidth() + suspend fun queryBandwidth(): Long = withContext(Dispatchers.IO) { + Utils.callRemote { + service.queryBandwidth() + } } suspend fun setProxyMode(mode: General.Mode) = withContext(Dispatchers.IO) { - service.setProxyMode(mode.toString()) + Utils.callRemote { + service.setProxyMode(mode.toString()) + } } } \ No newline at end of file diff --git a/app/src/main/java/com/github/kr328/clash/remote/ProfileClient.kt b/app/src/main/java/com/github/kr328/clash/remote/ProfileClient.kt index bf70528..b3d3a08 100644 --- a/app/src/main/java/com/github/kr328/clash/remote/ProfileClient.kt +++ b/app/src/main/java/com/github/kr328/clash/remote/ProfileClient.kt @@ -8,26 +8,38 @@ import kotlinx.coroutines.withContext class ProfileClient(private val service: IProfileService) { suspend fun queryProfiles(): Array = withContext(Dispatchers.IO) { - service.queryProfiles() + Utils.callRemote { + service.queryProfiles() + } } suspend fun queryActiveProfile(): ClashProfileEntity? = withContext(Dispatchers.IO) { - service.queryActiveProfile() + Utils.callRemote { + service.queryActiveProfile() + } } suspend fun enqueueRequest(request: ProfileRequest) = withContext(Dispatchers.IO) { - service.enqueueRequest(request) + Utils.callRemote { + service.enqueueRequest(request) + } } suspend fun setActiveProfile(id: Long) = withContext(Dispatchers.IO) { - service.setActiveProfile(id) + Utils.callRemote { + service.setActiveProfile(id) + } } suspend fun requestProfileEditUri(id: Long): String? = withContext(Dispatchers.IO) { - service.requestProfileEditUri(id) + Utils.callRemote { + service.requestProfileEditUri(id) + } } suspend fun commitProfileEditUri(uri: String) = withContext(Dispatchers.IO) { - service.commitProfileEditUri(uri) + Utils.callRemote { + service.commitProfileEditUri(uri) + } } } \ No newline at end of file diff --git a/app/src/main/java/com/github/kr328/clash/remote/Utils.kt b/app/src/main/java/com/github/kr328/clash/remote/Utils.kt new file mode 100644 index 0000000..bf0bf98 --- /dev/null +++ b/app/src/main/java/com/github/kr328/clash/remote/Utils.kt @@ -0,0 +1,24 @@ +package com.github.kr328.clash.remote + +import android.os.DeadObjectException +import com.github.kr328.clash.dump.LogcatDumper +import com.microsoft.appcenter.crashes.Crashes +import com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog + +internal object Utils { + fun callRemote(block: () -> T): T { + return try { + block() + } + catch (e: DeadObjectException) { + val log = LogcatDumper.dump().joinToString(separator = "\n") + + val attachmentLog = ErrorAttachmentLog + .attachmentWithText(log, "logcat.txt") + + Crashes.trackError(e, null, listOf(attachmentLog)) + + throw e + } + } +} \ No newline at end of file diff --git a/core/src/main/golang/clash b/core/src/main/golang/clash index c6eb441..d68467f 160000 --- a/core/src/main/golang/clash +++ b/core/src/main/golang/clash @@ -1 +1 @@ -Subproject commit c6eb441961536f9162dbd24b06ad249d5158d121 +Subproject commit d68467fe37dfa4ee5928a1c18e9eb8be1c36ae2e