Skip to content

Commit

Permalink
Merge pull request Automattic#440 from wiryadev/timber-kt
Browse files Browse the repository at this point in the history
migrate `TimberDebugTree` to Kotlin
  • Loading branch information
geekygecko committed Oct 23, 2022
2 parents 1b00311 + 268f35f commit f3654d6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 82 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package au.com.shiftyjelly.pocketcasts.utils

import android.os.Build
import android.util.Log
import java.util.regex.Pattern
import kotlin.math.min
import timber.log.Timber

class TimberDebugTree : Timber.Tree() {

/**
* Break up {@code message} into maximum-length chunks (if needed) and send to either
* {@link Log#println(int, String, String) Log.println()} or
* {@link Log#wtf(String, String) Log.wtf()} for logging.
*
* {@inheritDoc}
*/
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
val realTag = getRealTag(tag)
if (message.length < MAX_LOG_LENGTH) {
if (priority == Log.ASSERT) {
Log.wtf(realTag, message)
} else {
Log.println(priority, realTag, message)
}
return
}

// Split by line, then ensure each line can fit into Log's maximum length.
var i = 0
val length = message.length
while (i < length) {
var newline = message.indexOf('\n', i)
newline = if (newline != -1) newline else length
do {
val end = min(newline, i + MAX_LOG_LENGTH)
val part = message.substring(i, end)
if (priority == Log.ASSERT) {
Log.wtf(realTag, part)
} else {
Log.println(priority, realTag, part)
}
i = end
} while (i < newline)
i++
}
}

private fun createStackElementTag(element: StackTraceElement): String? {
var tag = element.className
val matcher = ANONYMOUS_CLASS.matcher(tag)
if (matcher.find()) tag = matcher.replaceAll("")
tag = tag.substring(tag.lastIndexOf('.') + 1)
// Tag length limit was removed in API 24.
return if (tag.length <= MAX_TAG_LENGTH || Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
tag
} else tag.substring(0, MAX_TAG_LENGTH)
}

private fun getRealTag(tag: String?): String? {
if (tag != null) return tag

val stackTrace = Throwable().stackTrace
check(stackTrace.isNotEmpty()) { "Synthetic stacktrace didn't have enough elements: are you using proguard?" }
return createStackElementTag(stackTrace[CALL_STACK_INDEX])
}

companion object {
private const val MAX_LOG_LENGTH = 400
private const val MAX_TAG_LENGTH = 23
private const val CALL_STACK_INDEX = 4
private val ANONYMOUS_CLASS = Pattern.compile("(\\$\\d+)+$")
}
}

0 comments on commit f3654d6

Please sign in to comment.