Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the HyperlinkedDebugTree class that creates Logs that include hyperlinks to the calling line #377

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

Merthan
Copy link

@Merthan Merthan commented Jan 14, 2020

Added the HyperlinkedDebugTree class that creates Logs that include hyperlinks to the calling line

This way you can just click on the Tag and directly jump to the source. Very useful in big projects.

I think this should be included in some form as otherwise people who use the library won't know that this is even a possibility. I certainly didn't :D
The class also has a boolean variable that controls whether or not to show the method name too.

So planting it either looks like this:
Timber.plant(HyperlinkedDebugTree(false))
Or (because it's default value is true):
Timber.plant(HyperlinkedDebugTree())

This then looks similar to this (with clickable Hyperlinks)
D/(AddItemFragment.kt:222)getImageCaptureUri(): logMessage

D/(AddItemFragment.kt:222): logMessage
(there is no space before the ":", only here because I had to mark it as a link)

If you want to merge this PR but I forgot something like Testing or perhaps stuff like adding it in the Changelog, tell me.

At some point this might even be useful in the Readme file🗡

…yperlinks to the calling line and class.

This way you can just click on the Tag and directly jump to the source. Very useful in big projects.
@elfifo4
Copy link

elfifo4 commented Jan 14, 2023

@Merthan, in new versions of Android Studio, your idea should be implemented a bit differently:

/**
 * A [Tree] for debug builds.
 * Automatically shows a Hyperlink to the calling Class and LineNumber in the Logs.
 * Allows quick lookup of the caller source just by clicking on the Hyperlink in the Log.
 * @param showMethodName Whether or not to show the method name as well
 */
class HyperlinkedDebugTree(private val showMethodName: Boolean = true) : Timber.DebugTree() {

    override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
        super.log(priority, tag, getHyperlink() + message, t)
    }

    private fun getHyperlink(): String =
        Thread.currentThread().stackTrace.let { elements: Array<StackTraceElement> ->
            val index = 7 // most relevant StackTraceElement index that actually prints to logcat
            elements[index].let {
                "(${it.fileName}:${it.lineNumber}) "
            }
        }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants