Skip to content

Commit 5720d73

Browse files
committed
Use new logcat
With Android Studio Giraffe the new logcat is mandatory, but there you loose the click on code feature on given tag, so it moved to logged text
1 parent 45e8358 commit 5720d73

1 file changed

Lines changed: 28 additions & 10 deletions

File tree

LogcatCoreLib/src/main/java/info/hannes/timber/DebugFormatTree.kt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,33 @@ import org.json.JSONException
44
import org.json.JSONObject
55
import timber.log.Timber
66

7-
open class DebugFormatTree : Timber.DebugTree() {
7+
/* If you use old logcat, e.g Android Studio Electric Eel, you should use for newLogcat a false */
8+
open class DebugFormatTree(private val newLogcat: Boolean = true) : Timber.DebugTree() {
9+
10+
private var codeIdentifier = ""
811

912
override fun createStackElementTag(element: StackTraceElement): String? {
10-
return String.format(
11-
"(%s:%d) %s.%s()",
12-
element.fileName,
13-
element.lineNumber, // format ensures line numbers have at least 3 places to align consecutive output from the same file
14-
// method is fully qualified only when class differs on filename otherwise it can be cropped on long lambda expressions
15-
super.createStackElementTag(element)?.replaceFirst(element.fileName.takeWhile { it != '.' }, ""),
16-
element.methodName
17-
)
13+
if (newLogcat) {
14+
codeIdentifier = String.format(
15+
"(%s:%d)",
16+
element.fileName,
17+
element.lineNumber // format ensures line numbers have at least 3 places to align consecutive output from the same file
18+
)
19+
return String.format(
20+
"%s.%s()",
21+
// method is fully qualified only when class differs on filename otherwise it can be cropped on long lambda expressions
22+
super.createStackElementTag(element)?.replaceFirst(element.fileName.takeWhile { it != '.' }, ""),
23+
element.methodName
24+
)
25+
} else
26+
return String.format(
27+
"(%s:%d) %s.%s()",
28+
element.fileName,
29+
element.lineNumber, // format ensures line numbers have at least 3 places to align consecutive output from the same file
30+
// method is fully qualified only when class differs on filename otherwise it can be cropped on long lambda expressions
31+
super.createStackElementTag(element)?.replaceFirst(element.fileName.takeWhile { it != '.' }, ""),
32+
element.methodName
33+
)
1834
}
1935

2036
// if there is an JSON string, try to print out pretty
@@ -24,9 +40,11 @@ open class DebugFormatTree : Timber.DebugTree() {
2440
try {
2541
val json = JSONObject(message)
2642
localMessage = json.toString(3)
27-
} catch (e: JSONException) {
43+
} catch (_: JSONException) {
2844
}
2945
}
46+
if (newLogcat)
47+
localMessage = codeIdentifier + localMessage
3048
super.log(priority, tag, localMessage, t)
3149
}
3250
}

0 commit comments

Comments
 (0)