@@ -5,7 +5,6 @@ import android.util.Log
55import org.json.JSONArray
66import org.json.JSONException
77import org.json.JSONObject
8- import se.reactant.reactantutils.BuildConfig
98
109/* *
1110 * Extensions for the Android util Log class
@@ -14,43 +13,36 @@ import se.reactant.reactantutils.BuildConfig
1413
1514const val UNKNOWN_CLASS_TAG = " UNKNOWN CLASS"
1615const val JSON_INDENTATION = 2
17- private var cachedCallingClassIndex = - 1
1816
19- fun i (message : String? = "", throwable : Throwable ? = null, tag : String? = ""): Unit {
20- if (BuildConfig .DEBUG ) Log .i(getCaller() + tag, message, throwable)
21- }
17+ fun i (message : String? = "", throwable : Throwable ? = null, tag : String? = "") =
18+ Log .i(getCaller() + tag, message, throwable)
2219
23- fun d (message : String? = "", throwable : Throwable ? = null, tag : String? = ""): Unit {
24- if (BuildConfig .DEBUG ) Log .d(getCaller() + tag, message, throwable)
25- }
20+ fun d (message : String? = "", throwable : Throwable ? = null, tag : String? = "") =
21+ Log .d(getCaller() + tag, message, throwable)
2622
27- fun e (message : String? = "", throwable : Throwable ? = null, tag : String? = ""): Unit {
28- if (BuildConfig .DEBUG ) Log .e(getCaller() + tag, message, throwable)
29- }
23+ fun e (message : String? = "", throwable : Throwable ? = null, tag : String? = "") =
24+ Log .e(getCaller() + tag, message, throwable)
3025
31- fun v (message : String? = "", throwable : Throwable ? = null, tag : String? = ""): Unit {
32- if (BuildConfig .DEBUG ) Log .v(getCaller() + tag, message, throwable)
33- }
26+ fun v (message : String? = "", throwable : Throwable ? = null, tag : String? = "") =
27+ Log .v(getCaller() + tag, message, throwable)
3428
35- fun w (message : String? = "", throwable : Throwable ? = null, tag : String? = ""): Unit {
36- if (BuildConfig .DEBUG ) Log .w(getCaller() + tag, message, throwable)
37- }
29+ fun w (message : String? = "", throwable : Throwable ? = null, tag : String? = "") =
30+ Log .w(getCaller() + tag, message, throwable)
3831
39- fun wtf (message : String? = "", throwable : Throwable ? = null, tag : String? = ""): Unit {
40- if (BuildConfig .DEBUG ) Log .wtf(getCaller() + tag, message, throwable)
41- }
32+ fun wtf (message : String? = "", throwable : Throwable ? = null, tag : String? = "") =
33+ Log .wtf(getCaller() + tag, message, throwable)
4234
43- fun <T >json (obj : T , throwable : Throwable ? = null, tag : String? = ""): Unit {
35+ fun <T > json (obj : T , throwable : Throwable ? = null, tag : String? = ""): Int {
4436 val json = if (obj is String ) obj.trim() else gson.toJson(obj)
4537
4638 try {
4739 when (json?.first()) {
48- ' {' -> Log .d(getCaller() + tag, JSONObject (json).toString(JSON_INDENTATION ), throwable)
49- ' [' -> Log .d(getCaller() + tag, JSONArray (json).toString(JSON_INDENTATION ), throwable)
50- else -> Log .e(getCaller() + tag, " Invalid Json" , throwable)
40+ ' {' -> return Log .d(getCaller() + tag, JSONObject (json).toString(JSON_INDENTATION ), throwable)
41+ ' [' -> return Log .d(getCaller() + tag, JSONArray (json).toString(JSON_INDENTATION ), throwable)
42+ else -> return Log .e(getCaller() + tag, " Invalid Json" , throwable)
5143 }
5244 } catch (e: JSONException ) {
53- Log .e(getCaller() + tag, " Invalid Json" , e)
45+ return Log .e(getCaller() + tag, " Invalid Json" , e)
5446 }
5547}
5648
@@ -63,12 +55,11 @@ private fun getCaller(): String {
6355
6456 // Find the last instance of the Log class in the current stack trace
6557 // and look for the calling Class in the next position
66- if (cachedCallingClassIndex == - 1 )
67- cachedCallingClassIndex = stackTrace.indexOfLast { it.className == " Log.LogExtensionsKt" } + 1
58+ val callingClassIndex = stackTrace.indexOfLast { it.className == " Log.LogExtensionsKt" } + 1
6859
6960 // Pull the calling class from the stack trace
70- if (cachedCallingClassIndex != - 1 && stackTrace.size >= cachedCallingClassIndex ) {
71- val callingClass = stackTrace[cachedCallingClassIndex ]
61+ if (callingClassIndex > 0 && stackTrace.size >= callingClassIndex ) {
62+ val callingClass = stackTrace[callingClassIndex ]
7263
7364 if (callingClass.className.split(" " ).isNotEmpty()) {
7465 return " (" + callingClass.fileName + " :" + callingClass.lineNumber + " )"
0 commit comments