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

前提是你 提交于 2019-12-17 20:36:16

问题


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 ?


回答1:


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());



回答2:


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

    }


来源:https://stackoverflow.com/questions/8067528/is-there-any-way-to-access-automatically-any-log-in-logcat-by-a-double-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!