package com.xty.common

import android.text.TextUtils
import com.tencent.mm.opensdk.utils.Log


/**
 * 日志封装类
 */
object LogUtils {

    private const val TAG = "ZJ365"

    /**
     * 得到tag
     * @return 所在类->方法:行
     */
    private fun generateTag(): String {
        val stackTraceElement = Thread.currentThread().stackTrace[6]
//        val callerClazzName =
//            stackTraceElement.className.substring(stackTraceElement.className.lastIndexOf(".") + 1)
        val fileName = stackTraceElement.fileName
//        val methodName = stackTraceElement.methodName
        val lineNumber = stackTraceElement.lineNumber
        return "($fileName:$lineNumber)"
    }

    private fun isDebug(): Boolean {
        return BuildConfig.isDebug// || MMkvHelper.getBoolean("openLog", false)
//        return true
    }


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

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

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

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

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

    fun d(tag: String?, msg: String) {//测试环境输出日志
        if (!isDebug()) return
        Log.d(TAG, getMsg(tag, msg))
    }

    fun i(tag: String?, msg: String) {//测试环境输出日志
        if (!isDebug()) return
        Log.i(TAG, getMsg(tag, msg))
    }

    fun e(tag: String?, msg: String) {
        if (!isDebug()) return
        Log.e(TAG, getMsg(tag, msg))
    }

    fun w(tag: String?, msg: String) {//测试环境输出日志
        if (!isDebug()) return
        Log.w(TAG, getMsg(tag, msg))
    }

    fun v(tag: String?, msg: String) {//测试环境输出日志
        if (!isDebug()) return
        Log.v(TAG, getMsg(tag, msg))
    }

    private fun getMsg(tag: String?, msg: String): String{
        val tagStr = if (TextUtils.isEmpty(tag)) generateTag() else generateTag() + ":$tag"
        return "$msg:$tagStr"
    }
}