LogUtils

作者: 半称心x度 | 来源:发表于2022-01-07 15:25 被阅读0次

    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)

    }

    }

    相关文章

      网友评论

          本文标题:LogUtils

          本文链接:https://www.haomeiwen.com/subject/clvjcrtx.html