25 lines
835 B
Kotlin
25 lines
835 B
Kotlin
package com.adlerosn.brasilfurfest.helper
|
|
|
|
import android.util.Log
|
|
import org.jetbrains.anko.AnkoAsyncContext
|
|
import org.jetbrains.anko.uiThread
|
|
import java.io.PrintWriter
|
|
import java.io.StringWriter
|
|
|
|
fun <T> logException(clazz: Class<T>, throwable: Throwable){
|
|
Log.e(clazz.name, StringWriter().apply { throwable.printStackTrace(PrintWriter(this)) }.toString())
|
|
}
|
|
|
|
fun <T, K> logIfException(clazz: Class<T>, runnable: ()->K): K? {
|
|
return try {
|
|
run(runnable)
|
|
} catch (throwable: Throwable) {
|
|
logException(clazz, throwable)
|
|
null
|
|
}
|
|
}
|
|
|
|
fun <K> logIfException(obj: Any, runnable: ()->K): K? = logIfException(obj::class.java, runnable)
|
|
|
|
fun <T: Any, K> AnkoAsyncContext<T>.uiThreadWithLogger(runnable: () -> K): Boolean =
|
|
uiThread { logIfException(this.weakRef.get()!!::class.java, runnable) } |