0

I would like to make a macro that prints out the current method's name during Log.d and Log.e output. Right now I simply type the method name within a hard-coded string, but this is obviously inefficient should the method name change in the future, as each string needs to be searched for and replaced.

I am aware of using getMethodName() as indicated in this post:

How to get method name in Java

This one also looks promising:

Debugging with helper extension method

There are countless numbers of these posts on SO, but I'd like to find the best way for debugging purposes that does not impact runtime performance too much. Since I am using Eclipse I would like to find a solution that works well with that IDE.

7
  • are you an eclipse fanatic? Commented Jun 12, 2013 at 20:15
  • @pskink No. Why do you ask? Commented Jun 12, 2013 at 21:23
  • because i wrote an eclipse plug-in that when you call: Log.d(TAG, "msg") you will see in the logcat as if you called: Log.d(TAG, "myMethod:142, msg") Commented Jun 15, 2013 at 16:57
  • I use Eclipse for all my Android work, so if you post that as an answer I can review your plug-in and possibly accept it. Thanks. Commented Jun 18, 2013 at 17:43
  • the point is SO doesnt let me attach any files or i am wrong? Commented Jun 18, 2013 at 18:12

2 Answers 2

4

This is what I know of that is available to you for the current execution.

    Thread current = Thread.currentThread();
    StackTraceElement[] stack = current.getStackTrace();
    for(StackTraceElement element : stack)
    {
        if (!element.isNativeMethod()) {
            String className = element.getClassName();
            String fileName = element.getFileName();
            int lineNumber = element.getLineNumber();
            String methodName = element.getMethodName();
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I'll try that out. Thanks. Do you think that will have minimal impact on execution? Is there a way to use conditional compilation (as in C/C++) whereby I can remove this stuff from production code via a simple flag?
I personally keep a class with those kind of runtime values (log levels, or turning logging off entirely, etc) and then run a unit test before compiling a production release to confirm all of my expected values. And for performance, you can log the execution time of the method to see if it's really an issue. I can't imagine it is unless you're running it on each iteration of a loop that is also updating the UI or something. Sounds like you'll want to run all of your logging through a custom logging class, so that it's easy to turn this off with a global variable.
0

this is a simple yet very useful eclipse plug-in i mentioned in the comment above: download plugin

its an incremental builder so typical build time is measured in fractions of seconds. add it to your .project file as a new builder <buildCommand> with name:

<name>org.pskink.logger.builder</name>

it should be the last builder in <buildSpec> list

installation: just copy it to plugins folder and restart eclipse

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.