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

可紊 提交于 2019-11-27 15:00:22

问题


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?


回答1:


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



回答2:


The important thing is to insert "(X:Y)" in your log message, while X is your desired file name and Y is your desired line number in X. (I learned it from @breceivemail's answer). So try:

public static void log(final String tag, final String msg) {
    final StackTraceElement stackTrace = new Exception().getStackTrace()[1];

    String fileName = stackTrace.getFileName();
    if (fileName == null) fileName="";  // It is necessary if you want to use proguard obfuscation.

    final String info = stackTrace.getMethodName() + " (" + fileName + ":"
            + stackTrace.getLineNumber() + ")";

    Log.LEVEL(tag, info + ": " + msg);
}

Note: The LEVEL is the log level and can be v, d, i, w, e or wtf.

Now you can use log(tag, msg) instead of Log.LEVEL(tag, msg).


Example:

MainActivity.java:

...
public class MainActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        log("Test Tag", "Hello World!");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
    ...

The output:

12-30 14:24:45.343 ? I/Test Tag: onCreate (MainActivity.java:10): Hello World!

And MainActivity.java:10 automatically would be a link and you can click on it!


You can also assign following value to info variable if you want more verbose log:

final String info = stackTrace.getClassName() + "." + stackTrace.getMethodName() + " ("
            + fileName + ":" + stackTrace.getLineNumber() + ")\n";

So the output of above example would be:

12-30 14:33:07.360 ? I/Test Tag: com.example.myapp.MainActivity.onCreate (MainActivity.java:11)
                                 Hello World!    



回答3:


Yes you can do it .. Follow the example as answered on SO - logging




回答4:


To answer the question in a simple way:

respecter cette règle :

{FileName}.{ext}:{LigneNumber}

e.g. MainActivity.java:10

which gives a sample as below

Log.d(TAG, "onResume: MainActivity.java:10");

I hope this will help you




回答5:


use http://developer.android.com/reference/android/util/Log.html



来源:https://stackoverflow.com/questions/10597623/how-to-insert-a-log-in-logcat-that-when-i-click-on-it-jumps-to-its-line-in-code

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