Skip to content

Commit

Permalink
save log
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Aug 14, 2024
1 parent 8b2679a commit 31ec3c2
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 更新日誌

### v1.3.7.6

* 增加錯誤上報

### v1.3.7

* 時間顯示秒
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/MyTVApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class MyTVApplication : Application() {
shouldHeight = height
shouldWidth = (height * 16.0 / 9.0).toInt()
}

Thread.setDefaultUncaughtExceptionHandler(MyTVExceptionHandler(this))
}

fun getDisplayMetrics(): DisplayMetrics {
Expand Down
75 changes: 75 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/MyTVExceptionHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.lizongying.mytv0

import android.content.Context
import android.os.Build
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import kotlin.system.exitProcess

class MyTVExceptionHandler(val context: Context) : Thread.UncaughtExceptionHandler {
override fun uncaughtException(t: Thread, e: Throwable) {
val crashInfo =
"APP: ${context.appVersionName}, PRODUCT: ${Build.PRODUCT}, DEVICE: ${Build.DEVICE}, SUPPORTED_ABIS: ${Build.SUPPORTED_ABIS.joinToString()}, BOARD: ${Build.BOARD}, MANUFACTURER: ${Build.MANUFACTURER}, MODEL: ${Build.MODEL}, VERSION: ${Build.VERSION.SDK_INT}\nThread: ${t.name}\nException: ${e.message}\nStackTrace: ${
Log.getStackTraceString(
e
)
}\n"

CoroutineScope(Dispatchers.IO).launch {
saveCrashInfoToFile(crashInfo)

withContext(Dispatchers.Main) {
android.os.Process.killProcess(android.os.Process.myPid())
exitProcess(1)
}
}
}

private suspend fun saveCrashInfoToFile(crashInfo: String) {
if (isLimit()) {
Log.e(TAG, crashInfo)
} else {
try {
saveLog(crashInfo)
} catch (e: Exception) {
e.printStackTrace()
}
}
}

private fun isLimit(): Boolean {
if (context.appVersionName != SP.version) {
SP.version = context.appVersionName
SP.logTimes = SP.DEFAULT_LOG_TIMES
return false
} else {
SP.logTimes--
return SP.logTimes < 0
}
}

private suspend fun saveLog(crashInfo: String) {
withContext(Dispatchers.IO) {
val client = okhttp3.OkHttpClient.Builder().build()
val request = okhttp3.Request.Builder()
.url("https://lyrics.run/my-tv-0/v1/log")
.method("POST", crashInfo.toRequestBody("text/plain".toMediaType()))
.build()
try {
client.newCall(request).execute()
Log.i(TAG, "log success")
} catch (e: Exception) {
e.printStackTrace()
}
}
}

companion object {
private const val TAG = "MyTVException"
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/lizongying/mytv0/SP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ object SP {

private const val KEY_EPG = "epg"

private const val KEY_VERSION = "version"

private const val KEY_LOG_TIMES = "log_times"

const val DEFAULT_CONFIG_URL = ""
const val DEFAULT_EPG = "https://live.fanmingming.com/e.xml"
const val DEFAULT_CHANNEL = 0
const val DEFAULT_SHOW_ALL_CHANNELS = false
const val DEFAULT_COMPACT_MENU = true
const val DEFAULT_DISPLAY_SECONDS = false
const val DEFAULT_LOG_TIMES = 10

private lateinit var sp: SharedPreferences

Expand Down Expand Up @@ -151,4 +156,12 @@ object SP {
var epg: String?
get() = sp.getString(KEY_EPG, DEFAULT_EPG)
set(value) = sp.edit().putString(KEY_EPG, value).apply()

var version: String?
get() = sp.getString(KEY_VERSION, "")
set(value) = sp.edit().putString(KEY_VERSION, value).apply()

var logTimes: Int
get() = sp.getInt(KEY_LOG_TIMES, DEFAULT_LOG_TIMES)
set(value) = sp.edit().putInt(KEY_LOG_TIMES, value).apply()
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/lizongying/mytv0/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ object Utils {
}

init {
CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) {
CoroutineScope(Dispatchers.IO).launch {
init()
}
}
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version_code": 16975621, "version_name": "v1.3.7.5"}
{"version_code": 16975622, "version_name": "v1.3.7.6"}

0 comments on commit 31ec3c2

Please sign in to comment.