Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 436
Bobs Avatar asked May 15 '12 09:05

Bobs


People also ask

What is chatty in Logcat?

As soon as app considered 'chatty' by logcat (more than 5 lines per second), logs of your app will be collapsed. You can avoid this behaviour by whitelisting your app for logcat: adb logcat -P '<pid or uid of your app>'

What is PID in Logcat?

pid = process ID. uid = user ID of the application that owns that process. gid = group IDs of the application that owns that process.


2 Answers

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");
like image 103
Bobs Avatar answered Nov 15 '22 18:11

Bobs


Please use this Tree with Timber.

class MyLinkingTimberTree : Timber.DebugTree() {
    override fun createStackElementTag(element: StackTraceElement): String? {
        return makeClickableLineNumber(element)
    }

    private fun makeClickableLineNumber(
        element: StackTraceElement
    ): String {
        val className = element.fileName
        val methodName = element.methodName
        val lineNumber = element.lineNumber
        val fileName = element.fileName
        val stringBuilder = StringBuilder(className)
        .append(".")
        .append(methodName)
        .append(" (")
        .append(fileName)
        .append(":")
        .append(lineNumber)
        .append(")  ")
        return stringBuilder.toString()
    }
}

And then just instantiate it like this:

class MyApplication: Application() {

    override fun onCreate() {
        super.onCreate()
        if(BuildConfig.DEBUG) {
            Timber.plant(MyLinkingTimberTree())
        }
    }
}

Then just use Timber normally:

Timber.d("Currently Signed in:")

And this is the result. Nice, isn't it? I hope you enjoy using it as much as I enjoyed making it! ;)

Logcat with a link!

like image 22
Ignacio Garcia Avatar answered Nov 15 '22 18:11

Ignacio Garcia