-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCrashlyticsTree.kt
More file actions
86 lines (74 loc) · 2.97 KB
/
CrashlyticsTree.kt
File metadata and controls
86 lines (74 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package info.hannes.crashlytic
import android.util.Log
import com.google.firebase.crashlytics.FirebaseCrashlytics
import timber.log.Timber
import java.util.concurrent.atomic.AtomicBoolean
@Suppress("unused")
class CrashlyticsTree(private val identifier: String? = null) : Timber.Tree() {
override fun logMessage(priority: Int, tag: String?, message: String, t: Throwable?, vararg args: Any?) {
if (priority < Log.INFO) {
return
}
super.log(priority, tag, message, t, args)
FirebaseCrashlytics.getInstance().setCustomKey(
"PRIORITY", when (priority) {
// 2 -> "Verbose"
// 3 -> "Debug"
4 -> "Info"
5 -> "Warn"
6 -> "Error"
7 -> "Assert"
else -> priority.toString()
}
)
tag?.let { FirebaseCrashlytics.getInstance().setCustomKey(KEY_TAG, it) }
FirebaseCrashlytics.getInstance().setCustomKey(KEY_MESSAGE, message)
FirebaseCrashlytics.getInstance().setCustomKey(KEY_UNIT_TEST, isRunningUnitTests.toString())
FirebaseCrashlytics.getInstance().setCustomKey(KEY_ESPRESSO, isRunningEspresso().toString())
identifier?.let { FirebaseCrashlytics.getInstance().setUserId(it) }
if (priority > Log.WARN) {
if (t != null)
FirebaseCrashlytics.getInstance().recordException(t)
else
FirebaseCrashlytics.getInstance().recordException(Throwable(message))
} else if (priority > Log.INFO) {
FirebaseCrashlytics.getInstance().log(message)
}
}
companion object {
const val KEY_TAG = "TAG"
const val KEY_MESSAGE = "message"
const val KEY_ESPRESSO = "Espresso"
const val KEY_UNIT_TEST = "UnitTest"
private var runningTest: AtomicBoolean? = null
private var runUnitTest: Boolean? = null
val isRunningUnitTests: Boolean
get() {
if (runUnitTest == null) {
runUnitTest = "true" == System.getProperty("run-under-test", "false") || !System.getProperty("java.vendor")!!.contains("Android")
}
return runUnitTest!!
}
@Synchronized
fun isRunningEspresso(): Boolean {
if (runningTest == null) {
var isTest: Boolean = try {
Class.forName("android.support.test.espresso.Espresso")
true
} catch (e: ClassNotFoundException) {
false
}
if (!isTest) {
isTest = try {
Class.forName("androidx.test.espresso.Espresso")
true
} catch (e: ClassNotFoundException) {
false
}
}
runningTest = AtomicBoolean(isTest)
}
return runningTest!!.get()
}
}
}