android log

package com.ffalconxr.mercury.common.utils

import android.text.TextUtils

import android.util.Log

import com.ffalconxr.mercury.common.BuildConfig

/**

* 支持Logcat日志里边点击到打印日志到源码,并可以在Release版动态开启。

*

* * 关闭开关 调用:adb shell setprop log.tag.Mercury I

* * 打开日志 调用:adb shell setprop log.tag.Mercury D

*

*

* 同步更新FLogger的isDebug日志开关。

*/

object FLogger {

private const val TAG ="Mercury"

    const val isDebug =true

    /** 日志开关*/

//    var isDebug = BuildConfig.DEBUG || Log.isLoggable(TAG, Log.DEBUG)

//        private se

//        t

    /** 是否写入文件*/

    private const val INPUT_FILE_ENABLE =false

    private const val VERBOSE =2

    private const val DEBUG =3

    private const val INFO =4

    private const val WARN =5

    private const val ERROR =6

    /**

    * 更新日志开关状态

    */

    private fun updateLogSwitch() {

//        isDebug = BuildConfig.DEBUG || Log.isLoggable(TAG, Log.DEBUG)

    }

@JvmStatic

    fun v(tag: String, msg: String) {

if (isDebug) {

format(VERBOSE, createLog(tag, msg))

}

}

@JvmStatic

    fun v(msg: String) = v("", msg)

@JvmStatic

    fun i(tag: String, msg: String) {

updateLogSwitch()

if (isDebug) {

format(INFO, createLog(tag, msg))

}

}

@JvmStatic

    fun i(msg: String) = i("", msg)

@JvmStatic

    fun w(tag: String ="", msg: String) {

if (isDebug) {

printLog(WARN, createLog(tag, msg))

}

}

@JvmStatic

    fun w(msg: String) = w("", msg)

@JvmStatic

    fun w(t: Throwable? =null) {

if (isDebug) {

val trace = Log.getStackTraceString(t)

printLog(WARN,"error: $trace")

}

}

@JvmStatic

    fun d(tag: String ="", msg: String) {

if (isDebug) {

format(DEBUG, createLog(tag, msg))

}

}

@JvmStatic

    fun d(msg: String) = d("", msg)

@JvmStatic

    fun e(tag: String ="", msg: String, t: Throwable? =null) {

if (isDebug) {

var trace = Log.getStackTraceString(t)

trace =if (trace.isEmpty())"" else "\n $trace"

            printLog(ERROR,"${createLog(tag, msg)} $trace")

}

}

@JvmStatic

    fun e(msg: String) = e("", msg)

@JvmOverloads

@JvmStatic

    fun e(t: Throwable? =null) {

if (isDebug) {

var trace = Log.getStackTraceString(t)

printLog(ERROR,"error: $trace")

}

}

private fun printLog(level: Int, msg: String) {

when (level) {

VERBOSE -> Log.v(TAG, msg)

DEBUG -> Log.d(TAG, msg)

INFO -> Log.i(TAG, msg)

WARN -> Log.w(TAG, msg)

ERROR -> Log.e(TAG, msg)

}

}

private fun createLog(tag: String, log: String): String {

//通过线程栈帧元素获取相应信息

        val logElement = Thread.currentThread().stackTrace.filter{

            !TextUtils.equals(it.className,"dalvik.system.VMStack")

&& !TextUtils.equals(it.className,"java.lang.Thread")

&& !TextUtils.equals(it.className, FLogger::class.java.canonicalName)

}.first()

//val logElement = Thread.currentThread().stackTrace[4]

        // 获取类名.即包名+类名

        val fullClassName = logElement.className

//val threadName = Thread.currentThread().name

//val threadID = Thread.currentThread().id

        val className = fullClassName.substring(fullClassName.lastIndexOf(".") +1)

val methodName = logElement.methodName

val fileName = logElement.fileName// 获取文件名.即xxx.java

        val lineNumber = logElement.lineNumber

val buffer = StringBuilder()

if (!TextUtils.isEmpty(tag) && !TextUtils.equals(tag, className)) {

buffer.append("${tag}=>>")

}

buffer.append("at[$className.$methodName($fileName:$lineNumber)]>> $log")

return buffer.toString()

}

private fun format(level: Int, msg: String) {

if (msg.length >4000) {

var i =0

            while (i < msg.length) {

if (i +4000 < msg.length)

printLog(level, msg.substring(i, i +4000))

else

                    printLog(level, msg.substring(i))

i +=4000

            }

}else {

printLog(level, msg)

}

}

/**

    * 打印当前位置调用堆栈

    */

    @JvmStatic

    fun printStack() {

if (isDebug) {

Log.e(TAG,"调用堆栈", Exception())

}

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容