Forked from SUPERCILEX/coroutines-global-handler.kt
Created
December 21, 2018 06:21
-
-
Save Razhan/3deee9cdfcb690892874258107a9fd39 to your computer and use it in GitHub Desktop.
Revisions
-
SUPERCILEX revised this gist
Oct 20, 2018 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ internal class LoggingHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler), CoroutineExceptionHandler { override fun handleException(context: CoroutineContext, exception: Throwable) { Crashlytics.logException(exception) // Since we don't want to crash and Coroutines will call the current thread's handler, we // install a noop handler and then reinstall the existing one once coroutines calls the new @@ -28,3 +28,5 @@ internal class LoggingHandler : AbstractCoroutineContextElement(CoroutineExcepti } } } val Thread.isMain get() = this === Looper.getMainLooper().thread -
SUPERCILEX revised this gist
Oct 20, 2018 . 1 changed file with 26 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,30 @@ @Keep internal class LoggingHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler), CoroutineExceptionHandler { override fun handleException(context: CoroutineContext, exception: Throwable) { CrashLogger.invoke(exception) // Since we don't want to crash and Coroutines will call the current thread's handler, we // install a noop handler and then reinstall the existing one once coroutines calls the new // handler. Thread.currentThread().apply { // _Do_ crash the main thread to ensure we're not left in a bad state if (isMain) return@apply val removed = uncaughtExceptionHandler uncaughtExceptionHandler = if (removed == null) { ResettingHandler } else { Thread.UncaughtExceptionHandler { t, _ -> t.uncaughtExceptionHandler = removed } } } } private object ResettingHandler : Thread.UncaughtExceptionHandler { override fun uncaughtException(t: Thread, e: Throwable) { t.uncaughtExceptionHandler = null } } } -
SUPERCILEX revised this gist
Oct 20, 2018 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,2 @@ # Put in file src/main/resources/META-INF/services/kotlinx.coroutines.CoroutineExceptionHandler com.supercilex.robotscouter.core.LoggingHandler -
SUPERCILEX created this gist
Oct 20, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ @Keep internal class LoggingHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler), CoroutineExceptionHandler { override fun handleException(context: CoroutineContext, exception: Throwable) = CrashLogger.invoke(exception) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ # Put in file module/resources/META-INF/services/kotlinx.coroutines.CoroutineExceptionHandler # Tip: `src` in on the same level as `resources` com.supercilex.robotscouter.core.LoggingHandler