Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create Android logcat entries that provide a link to source code in Eclipse? [duplicate]

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 ?

like image 864
Thordax Avatar asked Nov 24 '22 07:11

Thordax


2 Answers

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 + ")");

    }
like image 179
Bobs Avatar answered Dec 05 '22 18:12

Bobs


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());
like image 20
Patrick Avatar answered Dec 05 '22 18:12

Patrick