본문 바로가기
Android

Android DEBUG 모드 체크하기 / 디버그 모드일때만 로그 보이기

by kkong93 2022. 10. 25.
반응형
class GlobalApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        DEBUG = isDebuggable(this)
    }

    /**
     * get Debug Mode
     *
     * @param context
     * @return
     */
    private fun isDebuggable(context: Context): Boolean {
        var debuggable = false
        val pm: PackageManager = context.getPackageManager()
        try {
            val appinfo: ApplicationInfo = pm.getApplicationInfo(context.getPackageName(), 0)
            debuggable = 0 != appinfo.flags and ApplicationInfo.FLAG_DEBUGGABLE
        } catch (e: PackageManager.NameNotFoundException) {
            /* debuggable variable will remain false */
        }
        return debuggable
    }

    companion object {
        var DEBUG = false
    }
}

 

object Dlog {
    val TAG = "KONG"

    /** Log Level Error  */
    fun e(message: String?) {
        if (GlobalApplication.DEBUG) buildLogMsg(message)?.let { Log.e(TAG, it) }
    }

    /** Log Level Warning  */
    fun w(message: String?) {
        if (GlobalApplication.DEBUG) buildLogMsg(message)?.let { Log.w(TAG, it) }
    }

    /** Log Level Information  */
    fun i(message: String?) {
        if (GlobalApplication.DEBUG) buildLogMsg(message)?.let { Log.i(TAG, it) }
    }

    /** Log Level Debug  */
    fun d(message: String?) {
        if (GlobalApplication.DEBUG) buildLogMsg(message)?.let { Log.d(TAG, it) }
    }

    /** Log Level Verbose  */
    fun v(message: String?) {
        if (GlobalApplication.DEBUG) buildLogMsg(message)?.let { Log.v(TAG, it) }
    }

    fun buildLogMsg(message: String?): String? {
        val ste = Thread.currentThread().stackTrace[4]
        val sb = StringBuilder()
        sb.append("[")
        sb.append("${ste.fileName} : ${ste.lineNumber}")
        sb.append("::")
        sb.append(ste.methodName)
        sb.append("]")
        sb.append(message)
        return sb.toString()
    }
}

디버그 모드일때만 로그 보기

반응형

댓글