-
Notifications
You must be signed in to change notification settings - Fork 30
/
CrashHandler.kt
162 lines (139 loc) · 5.06 KB
/
CrashHandler.kt
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package pyxis.uzuki.live.richutilskt.module.crash
import android.annotation.SuppressLint
import android.os.Build
import pyxis.uzuki.live.richutilskt.module.crash.item.LogLevel
import pyxis.uzuki.live.richutilskt.module.crash.item.RichCrashModel
import pyxis.uzuki.live.richutilskt.utils.nowDateString
import pyxis.uzuki.live.richutilskt.utils.tryCatch
import java.io.File
import java.util.*
/**
* RichUtilsKt
* Class: CrashConfig
* Created by Pyxis on 2017-11-06.
*
* Description:
*/
internal class CrashHandler private constructor(private val crashConfig: CrashConfig) : Thread.UncaughtExceptionHandler {
private var now: Calendar
private lateinit var richCrashModel: RichCrashModel
private val fileName: String
get() = "crash_${nowDateString()}.md"
private val lineBreak: String
get() = System.getProperty("line.separator")
init {
now = Calendar.getInstance()
}
override fun uncaughtException(t: Thread, e: Throwable) {
now = Calendar.getInstance()
handleException(e)
}
private fun handleException(ex: Throwable?) {
if (ex == null) {
return
}
richCrashModel = generateRichCrashModel(ex)
object : Thread() {
override fun run() {
saveLocal(writeLogIntoMarkdown())
}
}.start()
}
private fun saveLocal(message: String) {
val fileName = fileName
val file = File(crashConfig.logLocation, fileName)
if (!file.exists()) {
tryCatch {
file.createNewFile()
file.setWritable(java.lang.Boolean.TRUE)
}
}
val messageBytes = message.toByteArray()
file.writeBytes(messageBytes)
android.os.Process.killProcess(android.os.Process.myPid())
System.exit(10)
}
private fun generateRichCrashModel(ex: Throwable): RichCrashModel {
val richCrashModel = RichCrashModel()
richCrashModel.setConfig(crashConfig)
richCrashModel.setDeviceInfo(Build())
richCrashModel.setThrowable(ex)
richCrashModel.setTime(now.time, crashConfig)
return richCrashModel
}
@SuppressLint("InlinedApi")
private fun writeLogIntoMarkdown(): String {
val builder = StringBuilder()
builder.append("## Crash Log in ")
.append(richCrashModel.packageName)
.append(lineBreak)
.append("### Application Info")
.append(lineBreak)
.append("* Package Name: **")
.append(richCrashModel.packageName)
.append("**")
.append(lineBreak)
.append("* Version: **")
.append(richCrashModel.versionStr)
.append("**")
.append(lineBreak)
.append(lineBreak)
if (crashConfig.isDisplayDeviceInfo) {
builder.append("### Device Info")
.append(lineBreak)
.append("* Device: **")
.append(richCrashModel.modelStr)
.append(" (a.k.a ")
.append(richCrashModel.productStr)
.append(" or ")
.append(richCrashModel.deviceStr)
.append(")**")
.append(lineBreak)
.append("* Version: **")
.append(richCrashModel.sdkStr)
.append(" (")
.append(richCrashModel.sdkNum)
.append(")**")
.append(lineBreak)
.append("* Manufacturer: **")
.append(richCrashModel.manufacturerStr)
.append("**")
.append(lineBreak)
.append(lineBreak)
}
builder.append("### Crash Info")
.append(lineBreak)
.append("* When: **")
.append(richCrashModel.timeStr)
.append("**")
.append(lineBreak)
.append("* Message: **")
.append(richCrashModel.message)
.append("**")
.append(lineBreak)
.append("* Localized Message: **")
.append(richCrashModel.localizedMessage)
.append("**")
.append(lineBreak)
.append(lineBreak)
if (crashConfig.logLevel === LogLevel.STACKTRACE) {
builder.append("#### Stack Trace")
.append(lineBreak)
.append("````")
.append(lineBreak)
.append(richCrashModel.stackTrace)
.append("````")
.append(lineBreak)
.append(lineBreak)
}
return builder.toString()
}
companion object {
private var instance: CrashHandler? = null
fun getInstance(config: CrashConfig): CrashHandler {
if (instance == null)
instance = CrashHandler(config)
return instance as CrashHandler
}
}
}