Reputation: 13
I am about to kick off an android project and currently looking at some sample codes from internet. However sometimes it is hard to tell which classes are handling which which functions.
So I start to populate:
Log.d(LogUtils.DEBUG_TAG + getClass().getName(), new Exception().getStackTrace()[0].getMethodName() + " starts.");
to every function.
I just wonder if there is a quick way to auto populate this statement to every function in project?
Thanks vm, Joe
Upvotes: 1
Views: 435
Reputation: 19798
I am using this code for logs. It automatically give you information about class and method name from which it was called.
public static void log(final String msg)
{
final Throwable t = new Throwable();
final StackTraceElement[] elements = t.getStackTrace();
final String callerClassName = elements[1].getFileName();
final String callerMethodName = elements[1].getMethodName();
String TAG = "[" + callerClassName + "]";
Log.d(TAG, "[" + callerMethodName + "] " + msg);
}
Here is example:
log("Hello World");
Gives you:
[MainActivity.java] [onCreate] Hello World
Now you see that method was called from MainActivity.java file with onCreate method.
I just wonder if there is a quick way to auto populate this statement to every function in project?
No you need to add it manually. However you can use boolean variable in my method above to turn logs on/off.
Full source code could be found here.
Upvotes: 2