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

Clickable log source added #469

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ The `DebugTree` implementation will automatically figure out from which class it
use that class name as its tag. Since the tags vary, it works really well when coupled with a log
reader like [Pidcat][1].

The `DebugTree` implementation also supports clickable logs in logcat. It will provide a clickable
location of the specific log statement in Android Studio Logcat (in format FILENAME:LINE_NUMBER).
You can disable it by passing `clickableSourceEnabled = false` in `DebugTree` constructor.

There are no `Tree` implementations installed by default because every time you log in production, a
puppy dies.

Expand Down
46 changes: 33 additions & 13 deletions timber/src/main/java/timber/log/Timber.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Timber private constructor() {
return tag
}

@get:JvmSynthetic // Hide from public API.
internal open val source: String = ""

/** Log a verbose message with optional format args. */
open fun v(message: String?, vararg args: Any?) {
prepareLog(Log.VERBOSE, null, message, *args)
Expand Down Expand Up @@ -193,27 +196,32 @@ class Timber private constructor() {
}

/** A [Tree] for debug builds. Automatically infers the tag from the calling class. */
open class DebugTree : Tree() {
open class DebugTree(private val clickableSourceEnabled: Boolean = true) : Tree() {
private val fqcnIgnore = listOf(
Timber::class.java.name,
Timber.Forest::class.java.name,
Tree::class.java.name,
DebugTree::class.java.name
Timber::class.java.name,
Timber.Forest::class.java.name,
Tree::class.java.name,
DebugTree::class.java.name
)

override val tag: String?
get() = super.tag ?: Throwable().stackTrace
.first { it.className !in fqcnIgnore }
.let(::createStackElementTag)
.first { it.className !in fqcnIgnore }
.let(::createStackElementTag)

override val source: String
get() = Throwable().stackTrace
.first { it.className !in fqcnIgnore }
.let(::createStackElementSource)

/**
* Extract the tag which should be used for the message from the `element`. By default
* this will use the class name without any anonymous class suffixes (e.g., `Foo$1`
* becomes `Foo`).
*
* Note: This will not be called if a [manual tag][.tag] was specified.
*/
protected open fun createStackElementTag(element: StackTraceElement): String? {
*/
protected open fun createStackElementTag(element: StackTraceElement): String {
var tag = element.className.substringAfterLast('.')
val m = ANONYMOUS_CLASS.matcher(tag)
if (m.find()) {
Expand All @@ -227,14 +235,26 @@ class Timber private constructor() {
}
}

/**
* Extract the source (File Name & Line Number)
*/
protected open fun createStackElementSource(element: StackTraceElement) = with(element){
"($fileName:$lineNumber)"
}

/**
* Break up `message` into maximum-length chunks (if needed) and send to either
* [Log.println()][Log.println] or
* [Log.wtf()][Log.wtf] for logging.
*
* {@inheritDoc}
*/
*/
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
val message = if (clickableSourceEnabled) {
"$source $message"
} else {
message
}
if (message.length < MAX_LOG_LENGTH) {
if (priority == Log.ASSERT) {
Log.wtf(tag, message)
Expand Down Expand Up @@ -385,10 +405,10 @@ class Timber private constructor() {
/**
* A view into Timber's planted trees as a tree itself. This can be used for injecting a logger
* instance rather than using static methods or to facilitate testing.
*/
*/
@Suppress(
"NOTHING_TO_INLINE", // Kotlin users should reference `Tree.Forest` directly.
"NON_FINAL_MEMBER_IN_OBJECT" // For japicmp check.
"NOTHING_TO_INLINE", // Kotlin users should reference `Tree.Forest` directly.
"NON_FINAL_MEMBER_IN_OBJECT" // For japicmp check.
)
@JvmStatic
open inline fun asTree(): Tree = this
Expand Down