使用Thread.currentThread().stackTrace获取调用者信息
本质是打印代码逻辑堆栈的方式,也是为了通过加日志定位一些调用关系的方法
val stack = Thread.currentThread().stackTrace
for (info in stack) {
Log.d("stackTrace_debug", info.toString())
}
一个例子
示例代码
获取指定堆栈的行数
2021-11-27 14:06:55.523 12533-12533/com.fadi.demo:ui D/UiModuleController: scan: called from: 4, caller: (MainActivity.java:201)<--(Instrumentation.java:1486), state: 0
完整如下
@AnyThread
fun scan(from: Int, callback: Callback? = null) {
Log.d(TAG, "scan: called from: $from, caller: ${LogUtils.getCaller()}, state: $state")
}
@JvmStatic
fun getCaller(): String {
val stack = Thread.currentThread().stackTrace
for (info in stack) {
Log.d("stackTrace_debug", info.toString())
}
return stack[4].simpleString() + "<--" + stack[5].simpleString()
}
打印结果
2021-11-27 14:06:55.521 12533-12533/com.fadi.demo:ui D/stackTrace_debug: dalvik.system.VMStack.getThreadStackTrace(Native Method)
2021-11-27 14:06:55.521 12533-12533/com.fadi.demo:ui D/stackTrace_debug: java.lang.Thread.getStackTrace(Thread.java:1724)
2021-11-27 14:06:55.521 12533-12533/com.fadi.demo:ui D/stackTrace_debug: com.tct.smartmanager.common.LogUtils.getCaller(LogUtils.kt:38)
2021-11-27 14:06:55.521 12533-12533/com.fadi.demo:ui D/stackTrace_debug: com.tct.smartmanager.module.UiModuleController.scan(UiModuleController.kt:129)
2021-11-27 14:06:55.521 12533-12533/com.fadi.demo:ui D/stackTrace_debug: com.fadi.demo.ui.MainActivity.onResume(MainActivity.java:201)
2021-11-27 14:06:55.521 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1486)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.app.Activity.performResume(Activity.java:8244)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.app.ActivityThread.performResumeActivity(ActivityThread.java:4935)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4978)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.app.ActivityThread$H.handleMessage(ActivityThread.java:2355)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.os.Handler.dispatchMessage(Handler.java:106)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.os.Looper.loopOnce(Looper.java:217)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.os.Looper.loop(Looper.java:309)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: android.app.ActivityThread.main(ActivityThread.java:8192)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: java.lang.reflect.Method.invoke(Native Method)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:559)
2021-11-27 14:06:55.522 12533-12533/com.fadi.demo:ui D/stackTrace_debug: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1009)
2021-11-27 14:06:55.523 12533-12533/com.fadi.demo:ui D/UiModuleController: scan: called from: 4, caller: (MainActivity.java:201)<--(Instrumentation.java:1486), state: 0
|