@@ -4,17 +4,33 @@ import org.json.JSONException
44import org.json.JSONObject
55import 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