How to insert a log in LogCat that when I click on it jumps to its line in code?

前端 未结 7 1877
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 10:16

I want to insert a log in LogCat that when I click on it jumps to its line like some error logs that are generated by system.

Is it possible?

7条回答
  •  猫巷女王i
    2020-12-15 10:52

    I found it:

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

    Its usage:

    showLogCat("tag", "message");
    

提交回复
热议问题