From 268f35ff3ab832e78f9202dab5099032294c58b7 Mon Sep 17 00:00:00 2001 From: Abrar Wiryawan Date: Fri, 21 Oct 2022 09:26:10 +0700 Subject: [PATCH] migrate `TimberDebugTree` to Kotlin --- .../pocketcasts/utils/TimberDebugTree.java | 82 ------------------- .../pocketcasts/utils/TimberDebugTree.kt | 74 +++++++++++++++++ 2 files changed, 74 insertions(+), 82 deletions(-) delete mode 100644 modules/services/utils/src/main/java/au/com/shiftyjelly/pocketcasts/utils/TimberDebugTree.java create mode 100644 modules/services/utils/src/main/java/au/com/shiftyjelly/pocketcasts/utils/TimberDebugTree.kt diff --git a/modules/services/utils/src/main/java/au/com/shiftyjelly/pocketcasts/utils/TimberDebugTree.java b/modules/services/utils/src/main/java/au/com/shiftyjelly/pocketcasts/utils/TimberDebugTree.java deleted file mode 100644 index ffa7ce6c9c..0000000000 --- a/modules/services/utils/src/main/java/au/com/shiftyjelly/pocketcasts/utils/TimberDebugTree.java +++ /dev/null @@ -1,82 +0,0 @@ -package au.com.shiftyjelly.pocketcasts.utils; - -import android.os.Build; -import android.util.Log; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import timber.log.Timber; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Timber only supports a stack level of greater than 5, so when in threads Timber throws an exception. - */ -public class TimberDebugTree extends Timber.Tree { - private static final int MAX_LOG_LENGTH = 4000; - private static final int MAX_TAG_LENGTH = 23; - private static final int CALL_STACK_INDEX = 4; - private static final Pattern ANONYMOUS_CLASS = Pattern.compile("(\\$\\d+)+$"); - - @Nullable - protected String createStackElementTag(@NotNull StackTraceElement element) { - String tag = element.getClassName(); - Matcher m = ANONYMOUS_CLASS.matcher(tag); - if (m.find()) { - tag = m.replaceAll(""); - } - tag = tag.substring(tag.lastIndexOf('.') + 1); - // Tag length limit was removed in API 24. - if (tag.length() <= MAX_TAG_LENGTH || Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - return tag; - } - return tag.substring(0, MAX_TAG_LENGTH); - } - - String getRealTag(String tag) { - if (tag != null) { - return tag; - } - - StackTraceElement[] stackTrace = new Throwable().getStackTrace(); - if (stackTrace.length == 0) { - throw new IllegalStateException("Synthetic stacktrace didn't have enough elements: are you using proguard?"); - } - return createStackElementTag(stackTrace[CALL_STACK_INDEX]); - } - - /** - * 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 protected void log(int priority, String tag, @NotNull String message, Throwable t) { - String 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. - for (int i = 0, length = message.length(); i < length; i++) { - int newline = message.indexOf('\n', i); - newline = newline != -1 ? newline : length; - do { - int end = Math.min(newline, i + MAX_LOG_LENGTH); - String part = message.substring(i, end); - if (priority == Log.ASSERT) { - Log.wtf(realTag, part); - } else { - Log.println(priority, realTag, part); - } - i = end; - } while (i < newline); - } - } -} \ No newline at end of file diff --git a/modules/services/utils/src/main/java/au/com/shiftyjelly/pocketcasts/utils/TimberDebugTree.kt b/modules/services/utils/src/main/java/au/com/shiftyjelly/pocketcasts/utils/TimberDebugTree.kt new file mode 100644 index 0000000000..885962287e --- /dev/null +++ b/modules/services/utils/src/main/java/au/com/shiftyjelly/pocketcasts/utils/TimberDebugTree.kt @@ -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+)+$") + } +} \ No newline at end of file