6

Is there any way to access automatically any Log in Logcat by a double click ?

Actually, when there is an error crashing my Android Application, I can double click on the line saying for instance

at com.myapp.mypackage$Class.function(File.java:117)

And by Double-clicking on this line, I am automatically redirected to the related line of my code.

But, when I try to generate the same line in another Log, example :

Log.e("TAG", "at com.myapp.mypackage$Class.function(File.java:117)");

The Double-Click doesn't work anymore ...

Any ideas ?

3
  • You could probably throw an exception from the line where you get the issue.. But that would also stop your program
    – default
    Nov 9, 2011 at 15:58
  • 1
    That's because the editor handles Stack traces from exceptions. Depending on the code from the editor, this is only possible if the creator of the IDE matches a string in the log to an actual file in your project.
    – Patrick
    Nov 9, 2011 at 16:00
  • If using eclipse, you can use the "Stacktrace Console".
    – JimmyB
    Nov 9, 2011 at 16:03

2 Answers 2

9

If you want to create a log in logcat that can be clicked and go to your line use the following method to create it:

Enjoy!

public static void showLogCat(String tag, String msg) {

        StackTraceElement[] stackTraceElement = Thread.currentThread()
                .getStackTrace();
        int currentIndex = -1;
        for (int i = 0; i < stackTraceElement.length; i++) {
            if (stackTraceElement[i].getMethodName().compareTo("showLogCat") == 0)
            {
                currentIndex = i + 1;
                break;
            }
        }

        String fullClassName = stackTraceElement[currentIndex].getClassName();
        String className = fullClassName.substring(fullClassName
                .lastIndexOf(".") + 1);
        String methodName = stackTraceElement[currentIndex].getMethodName();
        String lineNumber = String
                .valueOf(stackTraceElement[currentIndex].getLineNumber());

        Log.i(tag, msg);
        Log.i(tag + " position", "at " + fullClassName + "." + methodName + "("
                + className + ".java:" + lineNumber + ")");

    }
0
3

If you don't mind the clutter in your log, you can easily just add a new Exception() to the log message

Log.e("TAG", "Looky here see", new Exception());
1
  • Thanks for the tip, that's kind of efficient, and I really don't mind about cluttering my log.
    – Thordax
    Nov 10, 2011 at 9:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.