kotlin file
1.kotlin文件
fun log(content: String) = run {
if (isDebug()) {
getAutoJumpLogInfo().apply {
Log.d(this[1], "${this[0]}=====$content")
}
}
}
private fun getAutoJumpLogInfo(): Array<String> {
val contents = arrayOf("", "")
val stackTrace = Thread.currentThread().stackTrace
return if (stackTrace.size < 6) {
Log.e("LogKtx", "Stack is too shallow!!!")
contents
} else {
fun splicing(index: Int = 4, methodIndex: Int = 5) {
stackTrace[methodIndex].methodName.toString().let {
stackTrace[index].toString().substringAfter(it).apply {
contents[0] = this
contents[1] = this.substringBefore(".").substringAfter("(")
}
}
}
stackTrace[5].methodName.toString().let {
when {
stackTrace[4].toString().contains(it) -> splicing()
stackTrace[4].toString().contains(stackTrace[4].methodName.toString()) -> splicing(
methodIndex = 4
)
else -> splicing(5)
}
}
contents
}
}
private fun isDebug(): Boolean {
return try {
val info = App.get().applicationInfo
(info.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0
} catch (e: Exception) {
false
}
}
2.调用
log("variable$variable")