LogUtils

2022-01-07  本文已影响0人  半称心x度

object LogUtils {

fun log(message: String?) {

log(null, message)

}

fun log(tag: String? =null, message: String?) {

var content = message

if (message.isNullOrEmpty()) {

content ="NULL  "

        }

val trace = Throwable().fillInStackTrace().stackTrace

        var caller: String? =""

        var callingClass =""

        var callFile: String? =""

        var lineNumber =0

        for (iin 2 until trace.size) {

val clazz: Class<*> = trace[i].javaClass

            if (clazz != LogUtils::class.java) {

callingClass = trace[i].className

                callingClass = callingClass.substring(

callingClass

.lastIndexOf('.') +1

                )

caller = trace[i].methodName

                callFile = trace[i].fileName

                lineNumber = trace[i].lineNumber

                break

            }

}

//        val method = java.lang.String.format(

//            Locale.US,

//            "[%03d] %s.%s(%s:%d)",

//            Thread.currentThread().id,

//            callingClass,

//            caller,

//            callFile,

//            lineNumber

//        )

        val method ="($callFile,$lineNumber)"

        Log.d(tag ?: callFile, content + method)

}

}

上一篇下一篇

猜你喜欢

热点阅读