移动开发---kotlin 打印日志类

object LogUtil {
    private var isDebug = BuildConfig.DEBUG
    private const val TOP_BORDER =
        "╔══════════════════════════════════════════════════════════════════════════════════════════════════════════"
    private const val LEFT_BORDER = "║ "
    private const val BOTTOM_BORDER =
        "╚══════════════════════════════════════════════════════════════════════════════════════════════════════════"
    private const val CHUNK_SIZE = 106 //设置字节数
    fun d(message: String, tag: String = "SpeedTubeLog") {
        if (isDebug) {
            val newMsg = msgFormat(message)
            Log.d(tag, newMsg)
        }
    }

    fun i(message: String, tag: String = "SpeedTubeLog") {
        if (isDebug) {
            val newMsg = msgFormat(message)
            Log.i(tag, newMsg)
        }
    }

    fun w(message: String, tag: String = "SpeedTubeLog") {
        if (isDebug) {
            val newMsg = msgFormat(message)
            Log.w(tag, newMsg)
        }
    }

    fun e(message: String, tag: String = "SpeedTubeLog") {
        if (isDebug) {
            val newMsg = msgFormat(message)
            Log.e(tag, newMsg)
        }
    }


    private fun msgFormat(msg: String): String {
        val bytes: ByteArray = msg.toByteArray()
        val length = bytes.size
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US)
        var newMsg =
            "$TOP_BORDER\n$LEFT_BORDER\t${sdf.format(Date())}\n$LEFT_BORDER\t${targetStackTraceMSg()}"
        if (length > CHUNK_SIZE) {
            var i = 0
            while (i < length) {
                val count = (length - i).coerceAtMost(CHUNK_SIZE)
                val tempStr = String(bytes, i, count)
                newMsg += "\n$LEFT_BORDER\t$tempStr"
                i += CHUNK_SIZE
            }
        } else {
            newMsg += "\n$LEFT_BORDER\t$msg"
        }
        newMsg += "\n$BOTTOM_BORDER"
        return newMsg

    }

    private fun targetStackTraceMSg(): String {
        val targetStackTraceElement = getTargetStackTraceElement()
        return if (targetStackTraceElement != null) {
            "at ${targetStackTraceElement.className}.${targetStackTraceElement.methodName}(${targetStackTraceElement.fileName}:${targetStackTraceElement.lineNumber})"
        } else {
            ""
        }
    }

    private fun getTargetStackTraceElement(): StackTraceElement? {
        var targetStackTrace: StackTraceElement? = null
        var shouldTrace = false
        val stackTrace = Thread.currentThread().stackTrace
        for (stackTraceElement in stackTrace) {
            val isLogMethod = stackTraceElement.className == LogUtil::class.java.name
            if (shouldTrace && !isLogMethod) {
                targetStackTrace = stackTraceElement
                break
            }
            shouldTrace = isLogMethod
        }
        return targetStackTrace

    }
}
 ╔══════════════════════════════════════════════════════════════════════════════════════════════════════════
                                                                                                    ║ 	2024-01-17 13:05:01
                                                                                                    ║ 	at 包名.类名.方法名$lambda$1(类名.kt:133)
                                                                                                    ║ 	打印内容
                                                                                                    ╚══════════════════════════════════════════════════════════════════════════════════════════════════════════

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值