一、在Android Studo上 控制台上的日志,如何实现点击可跳转
在Android studio 中
- 在Android的运行环境中
- 打印(类文件名:行号),类文件时项目中可以找到的文件。
控制台上输出日志就会被标记蓝色,点击可以跳转到源文件指定的行数。
如
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
println("(MainActivity.kt:10)")
}
}
至此实现日志打印,点击可以跳转到源文件,只需要获取掉函数调用处的源文件名和行号即可。
二、获取函数调用的源文件和行号 (线程的栈结构)
我们知道每个线程中都有一个栈结构,每调用一个方法,就会将方法调用信息入栈,方法调用结束信息出栈。
称为函数调用栈。
Thread.currentThread().getStackTrace(); 可获取到当前线程的函数调用栈结构。
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
StackTraceElement 可以取到
- 类名 getClassName
- 方法名 getMethodName
- 类所在的文件名 getFileName
- 源文件的行号 getLineNumber
- 是否是native方法 isNativeMethod
因此栈结构可以获得 当前执行到了哪个类的哪个方法方法,位于哪个文件的第几行等信息。
public final class StackTraceElement implements Serializable {
public StackTraceElement(String declaringClass, String methodName, String fileName, int lineNumber) {
throw new RuntimeException("Stub!");
}
public String getFileName() {
throw new RuntimeException("Stub!");
}
public int getLineNumber() {
throw new RuntimeException("Stub!");
}
public String getClassName() {
throw new RuntimeException("Stub!");
}
public String getMethodName() {
throw new RuntimeException("Stub!");
}
public boolean isNativeMethod() {
throw new RuntimeException("Stub!");
}
public String toString() {
throw new RuntimeException("Stub!");
}
public boolean equals(Object obj) {
throw new RuntimeException("Stub!");
}
public int hashCode() {
throw new RuntimeException("Stub!");
}
}
三、自动定义Logu,计算出哪个类的第几行 调用了Logu
1、目标- 计算得出MainActivity.java 第5行调用了Logu
Logu.java
public class Logu {
public static void d(String tag,String msg){
Log.d(tag,msg);
}
}
MainActivity.java
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Logu.d("feifei","测试一下 获取调用当前方法的,类名,方法,文件名和行号")
}
}
2、我们将Logu 稍加改造,测试Logu的调用 位于栈帧的第几层
public class Logu {
public static void d(String tag,String msg){
//打印栈帧中的所有方法调用
try {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
for(int i = stackTrace.length-1;i>=0;i--){
System.out.println("feifei:"+stackTrace[i].toString());
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d(tag,msg);
}
}
打印结果如下,可以发现Logu的调用从底向上,位于第4层(index=3)
2019-06-30 21:43:12.466 22095-22095/com.example.testnavigation I/System.out: feifei index:16,className:com.android.internal.os.ZygoteInit,method:main,fileName:ZygoteInit.java,lineNumber:807
2019-06-30 21:43:12.466 22095-22095/com.example.testnavigation I/System.out: feifei index:15,className:com.android.internal.os.RuntimeInit$MethodAndArgsCaller,method:run,fileName:RuntimeInit.java,lineNumber:449
2019-06-30 21:43:12.467 22095-22095/com.example.testnavigation I/System.out: feifei index:14,className:java.lang.reflect.Method,method:invoke,fileName:Method.java,lineNumber:-2
2019-06-30 21:43:12.467 22095-22095/com.example.testnavigation I/System.out: feifei index:13,className:android.app.ActivityThread,method:main,fileName:ActivityThread.java,lineNumber:6747
2019-06-30 21:43:12.467 22095-22095/com.example.testnavigation I/System.out: feifei index:12,className:android.os.Looper,method:loop,fileName:Looper.java,lineNumber:164
2019-06-30 21:43:12.468 22095-22095/com.example.testnavigation I/System.out: feifei index:11,className:android.os.Handler,method:dispatchMessage,fileName:Handler.java,lineNumber:106
2019-06-30 21:43:12.468 22095-22095/com.example.testnavigation I/System.out: feifei index:10,className:android.app.ActivityThread$H,method:handleMessage,fileName:ActivityThread.java,lineNumber:1650
2019-06-30 21:43:12.469 22095-22095/com.example.testnavigation I/System.out: feifei index:9,className:android.app.ActivityThread,method:-wrap12,fileName:null,lineNumber:0
2019-06-30 21:43:12.469 22095-22095/com.example.testnavigation I/System.out: feifei index:8,className:android.app.ActivityThread,method:handleLaunchActivity,fileName:ActivityThread.java,lineNumber:2955
2019-06-30 21:43:12.470 22095-22095/com.example.testnavigation I/System.out: feifei index:7,className:android.app.ActivityThread,method:performLaunchActivity,fileName:ActivityThread.java,lineNumber:2812
2019-06-30 21:43:12.470 22095-22095/com.example.testnavigation I/System.out: feifei index:6,className:android.app.Instrumentation,method:callActivityOnCreate,fileName:Instrumentation.java,lineNumber:1217
2019-06-30 21:43:12.471 22095-22095/com.example.testnavigation I/System.out: feifei index:5,className:android.app.Activity,method:performCreate,fileName:Activity.java,lineNumber:7016
2019-06-30 21:43:12.471 22095-22095/com.example.testnavigation I/System.out: feifei index:4,className:android.app.Activity,method:performCreate,fileName:Activity.java,lineNumber:7025
2019-06-30 21:43:12.472 22095-22095/com.example.testnavigation I/System.out: feifei index:3,className:com.example.testnavigation.MainActivity,method:onCreate,fileName:MainActivity.kt,lineNumber:11
2019-06-30 21:43:12.472 22095-22095/com.example.testnavigation I/System.out: feifei index:2,className:com.example.testnavigation.Logu,method:d,fileName:Logu.java,lineNumber:12
2019-06-30 21:43:12.473 22095-22095/com.example.testnavigation I/System.out: feifei index:1,className:java.lang.Thread,method:getStackTrace,fileName:Thread.java,lineNumber:1538
2019-06-30 21:43:12.473 22095-22095/com.example.testnavigation I/System.out: feifei index:0,className:dalvik.system.VMStack,method:getThreadStackTrace,fileName:VMStack.java,lineNumber:-2
四、修改Logu,自动计算Logu被调用的上一级类名和行号,实现点击可跳转源文件。
public class Logu {
public static void d(String tag,String msg){
int index = 3;
StringBuffer stringBuffer = new StringBuffer();
//打印栈帧中的所有方法调用
try {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
// Log.d("feifei","reseal statckIndex:"+index+",stackTrace.length:"+stackTrace.length);
// for(int i = stackTrace.length-1;i>=0;i--){
// System.out.println("index:"+i+"feifei ----,className:"+stackTrace[i].getClassName()+",method:"+stackTrace[i].getMethodName()+",fileName:"+stackTrace[i].getFileName()+",lineNumber:"+stackTrace[i].getLineNumber());
// }
String className = stackTrace[index].getFileName();
String methodName = stackTrace[index].getMethodName();
int lineNumber = stackTrace[index].getLineNumber();
methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
stringBuffer.append("[(").append(className).append(":").append(lineNumber).append(")#").append(methodName).append("] ");
} catch (Exception e) {
e.printStackTrace();
}
String finalMsg = stringBuffer.toString()+": "+msg;
Log.d(tag,finalMsg);
}
}
输出结果如下:
2019-06-30 21:38:58.914 21764-21764/? D/feifei: [(MainActivity.kt:11)#OnCreate] -测试一下 获取调用当前方法的,类名,方法,文件名和行号