kotlin:LogKit

看到别人的一个代码,觉得有点意思,就复制过来。

package robat

import android.util.Log
import java.util.*

object LogKit {
    private val MIN_STACK_OFFSET = 3

    var defaultTag = "LogKit"
    private val lineSeparator = System.getProperty("line.separator", "/n")

    val V = Log.VERBOSE
    val D = Log.DEBUG
    val I = Log.INFO
    val W = Log.WARN
    val E = Log.ERROR
    val A = Log.ASSERT

    private val TOP_BORDER = "╔═══════════════════════════════════════════════════════════════════════════════════════════════════"
    private val LEFT_BORDER = "║ "
    private val BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════"
    private val MAX_LEN = 1000
    var open = true

    private fun processTagAndHead(): String {
        val elements = Thread.currentThread().stackTrace
        val offset = getStackOffset(elements)
        val targetElement = elements[offset]
        val head = Formatter()
            .format("%s [%s(%s:%d)]",
                "In Thread: " + Thread.currentThread().name,
                targetElement.methodName,
                targetElement.fileName,
                targetElement.lineNumber)

        return head.toString()
    }

    private fun processMsgBody(msg: String, flag: Int, tag: String = defaultTag) {
        printTop(flag, tag)
        // 首先打印调用信息
        printLog(flag, tag)

        val lineCount = msg.length / MAX_LEN
        if (lineCount == 0) {
            printLog(flag, tag, msg)
        } else {
            var index = 0
            var i = 0
            while (true) {
                printLog(flag, tag, msg.substring(index, index + MAX_LEN))
                index += MAX_LEN
                if ((++i) >= lineCount)
                    break
            }
        }
        printBottom(flag, tag)
    }

    fun getStackOffset(trace: Array<StackTraceElement>): Int {
        var i = MIN_STACK_OFFSET
        while (i < trace.size) {
            val e = trace[i]
            val name = e.className
            if (name != LogKit::class.java.name) {
                return i
            }
            i++
        }
        return 2
    }

    /* 虽然 kotlin 有默认值这种操作,但是 Log.i(tag,msg) 这种比较符合平时的操作,所以还是提供类似的重载,
     * 而非 LogUtil.i(msg: String,tag: String = defaultTAG) 这种带默认值参数的方法 */

    fun v(msg: String) {
        v(defaultTag, msg)
    }

    fun i(msg: String) {
        i(defaultTag, msg)
    }

    fun d(msg: String) {
        d(defaultTag, msg)
    }

    fun w(msg: String) {
        w(defaultTag, msg)
    }

    fun e(msg: String) {
        e(defaultTag, msg)
    }

    fun v(tag: String, msg: String) {
        if (!open) {
            return
        }
        processMsgBody(msg, V, tag)
    }

    fun i(tag: String, msg: String) {
        if (!open) {
            return
        }
        processMsgBody(msg, I, tag)
    }

    fun d(tag: String, msg: String) {
        if (!open) {
            return
        }
        processMsgBody(msg, D, tag)
    }

    fun w(tag: String, msg: String) {
        if (!open) {
            return
        }
        processMsgBody(msg, W, tag)
    }

    fun e(tag: String, msg: String) {
        if (!open) {
            return
        }
        processMsgBody(msg, E, tag)
    }

    fun printLog(flag: Int, tag: String, msg: String = processTagAndHead()) {
        Log.println(flag, tag, LEFT_BORDER + msg)
    }

    fun printBottom(flag: Int, tag: String) {
        Log.println(flag, tag, BOTTOM_BORDER)
    }

    fun printTop(flag: Int, tag: String) {
        Log.println(flag, tag, TOP_BORDER)
    }

    fun closeLog() {
        this.open = false
    }


}
相关推荐
卡卡_R-Python8 小时前
子集选择——基于R语言实现(最优子集选择法、逐步回归法、Lasso回归法、交叉验证法)
回归·r语言·kotlin
夏非夏3 天前
Kotlin jetpack MVP
android·kotlin
zhangphil3 天前
Kotlin约束泛型参数必须继承自某个父类
kotlin
ch_kexin3 天前
Android kotlin integer-array 存放图片资源ID
android·开发语言·kotlin
jiay24 天前
Kotlin-面向对象之构造函数、实例化和初始化
android·开发语言·kotlin
我怀里的猫4 天前
glide ModelLoader的Key错误使用 可能造成的内存泄漏
android·kotlin·glide
陟彼高冈yu4 天前
第10天:Fragments(碎片)使用-补充材料——‘MainActivity.kt‘解读
android·kotlin·android studio
姑苏风4 天前
《Kotlin实战》-第11章:DSL构建
android·开发语言·kotlin
大耳猫5 天前
Android 解决Java和Kotlin JDK编译版本不一致异常
android·java·kotlin
萌面小侠Plus6 天前
Android笔记(三十五):用责任链模式封装一个App首页Dialog管理工具
android·dialog·笔记·kotlin·责任链模式