<> about getStackTrace()

public StackTraceElement[] getStackTrace()

Returns an array of stack trace elements representing the stack dump of this thread . If the thread has not been started or terminated , Then the method will return a zero length array . If the returned array is not zero length , Then its first element represents the top of the stack , It is the latest method call in the sequence . The last element represents the bottom of the stack , Is the oldest method call in the sequence .

<>android/java Call stack

Add the following code to the called function
for (StackTraceElement i : Thread.currentThread().getStackTrace()) { System.out
.println("TAG " +i); }
<>Android Simple log encapsulation , Class name + Method name is Tag

Android The log class of is generally used as
Log.e(TAG,msg)

Tag The naming of values is generally a headache , A common practice is to Tag Set as class name , But every time log You have to set one for the class TAG variable , It's annoying . The purpose of this article is to solve this problem , take TAG The name of is extracted , We just need to care Log of msg that will do .
public class LogUtils { public static void d(String msg) { Log.e(getCallerInfo(
), msg); } private static String getCallerInfo() { StackTraceElement[]
stackTrace= Thread.currentThread().getStackTrace(); //0
VMStack.getThreadStackTrace //1 Thread.getStackTrace //2 LogUtil.getCallerInfo
//3 LogUtil.e //4 Caller StackTraceElement caller = stackTrace[4]; return caller
.getClassName() + " " + caller.getMethodName() + " " + caller.getLineNumber(); }
}
Other level logs can be supplemented similarly , usage method :
LogUtils.d("hello world!")

Technology