Skip to content

Commit

Permalink
feature: 日志
Browse files Browse the repository at this point in the history
  • Loading branch information
YuS1aN committed Mar 24, 2024
1 parent ca58cd0 commit c397b3c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/me/kbai/zhenxunui/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import com.google.gson.GsonBuilder
import com.google.gson.ToNumberPolicy
import me.kbai.zhenxunui.api.ErrorHandleAdapterFactory
import me.kbai.zhenxunui.model.BotBaseInfo
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale

/**
* @author Sean on 2023/5/31
Expand All @@ -35,6 +38,13 @@ object Constants {

var currentBot: BotBaseInfo? = null

val logFile by lazy {
val date = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
.format(System.currentTimeMillis())
mApplication.getExternalFilesDir(null)
?.let { File(it, "$date.log") }
}

@JvmStatic
fun init(app: Application) {
mApplication = app
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/me/kbai/zhenxunui/ZxApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Application;

import me.kbai.zhenxunui.tool.CrashHandler;
import me.kbai.zhenxunui.tool.GlobalToast;

/**
Expand All @@ -16,6 +17,7 @@ public void onCreate() {
mApp = this;
Constants.init(this);
GlobalToast.init(this);
CrashHandler.INSTANCE.init(this);
}

public static Application getApplication() {
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/me/kbai/zhenxunui/extends/LogExtends.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package me.kbai.zhenxunui.extends

import android.text.format.DateFormat
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.kbai.zhenxunui.BuildConfig
import me.kbai.zhenxunui.Constants
import java.util.Date

/**
* @author sean on 2022/5/12
*/

private val mCoroutineScope = CoroutineScope(Dispatchers.IO)

fun Any.logI(msg: String, tag: String? = null): Int {
val tagOrUnknown = tag ?: nameOrSuperclassName ?: "INFO"
outputLogFile(msg, tagOrUnknown)
Expand Down Expand Up @@ -37,8 +45,13 @@ private inline fun whenDebug(action: () -> Int): Int {
return 0
}

private fun outputLogFile(msg: String, tag: String) {
//
private fun outputLogFile(msg: String, tag: String) = mCoroutineScope.launch {
val file = Constants.logFile ?: return@launch
if (!file.exists()) {
if (!file.createNewFile()) return@launch
}
val dateTime = DateFormat.format("yyyy-MM-dd HH:mm:ss", Date()).toString()
file.appendText("$dateTime $tag $msg \n")
}

/**
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/me/kbai/zhenxunui/tool/CrashHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package me.kbai.zhenxunui.tool

import android.content.Context
import android.content.pm.PackageManager.NameNotFoundException
import android.os.Build
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import me.kbai.zhenxunui.extends.logE

object CrashHandler {

fun init(context: Context) {
Thread.setDefaultUncaughtExceptionHandler { _, exception ->
runBlocking(Dispatchers.IO) {
log(context, exception)
}
}
}

private fun log(context: Context, throwable: Throwable) = logE(
"MODEL: ${Build.MODEL} " +
"SDK: ${Build.VERSION.SDK_INT} " +
"CPU_ABI: ${Build.SUPPORTED_ABIS} " +
"VERSION: ${getPackageVersionName(context)} " +
"\n StackTrace: \n" +
Log.getStackTraceString(throwable),
"CRASH"
)


private fun getPackageVersionName(context: Context) = try {
context.packageManager.getPackageInfo(context.packageName, 0).versionName
} catch (e: NameNotFoundException) {
""
}
}

0 comments on commit c397b3c

Please sign in to comment.