diff --git a/app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt b/app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt index de43786..0216b3e 100644 --- a/app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt +++ b/app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt @@ -29,7 +29,7 @@ class MainActivity : setContentView(R.layout.activity_main) if (BuildConfig.DEBUG) { - WebViewJavascriptBridge.setLogLevel(Logger.LogLevel.DEBUG) + WebViewJavascriptBridge.setLogLevel(Logger.LogLevel.VERBOSE) } else { WebViewJavascriptBridge.setLogLevel(Logger.LogLevel.ERROR) } @@ -106,7 +106,7 @@ class MainActivity : ).apply { consolePipe = object : ConsolePipe { override fun post(message: String) { - Logger.i("[console.log]") { message } + Logger.v("[console.log]") { message } } } diff --git a/library/src/main/java/com/ding1ding/jsbridge/Logger.kt b/library/src/main/java/com/ding1ding/jsbridge/Logger.kt index 5e9a510..18941d5 100644 --- a/library/src/main/java/com/ding1ding/jsbridge/Logger.kt +++ b/library/src/main/java/com/ding1ding/jsbridge/Logger.kt @@ -1,16 +1,23 @@ +@file:Suppress("ktlint:standard:max-line-length") + package com.ding1ding.jsbridge import android.util.Log -import kotlin.math.min +import java.util.concurrent.atomic.AtomicInteger object Logger { - private const val MAX_LOG_LENGTH = 4000 const val DEFAULT_TAG = "WebViewJsBridge" - @Volatile - var logLevel = LogLevel.INFO + var logLevel = AtomicInteger(LogLevel.INFO.value) - enum class LogLevel { VERBOSE, DEBUG, INFO, WARN, ERROR, NONE } + enum class LogLevel(val value: Int) { + VERBOSE(Log.VERBOSE), + DEBUG(Log.DEBUG), + INFO(Log.INFO), + WARN(Log.WARN), + ERROR(Log.ERROR), + NONE(Log.ASSERT), + } @JvmStatic inline fun v(tag: String = DEFAULT_TAG, message: () -> String) = @@ -30,48 +37,22 @@ object Logger { @JvmStatic inline fun e(throwable: Throwable, tag: String = DEFAULT_TAG, message: () -> String = { "" }) { - if (logLevel <= LogLevel.ERROR) { + if (logLevel.get() <= LogLevel.ERROR.value) { val fullMessage = buildString { append(message()) if (isNotEmpty() && message().isNotEmpty()) append(": ") append(Log.getStackTraceString(throwable)) } - logInternal(Log.ERROR, tag, fullMessage) + logInternal(LogLevel.ERROR, tag, fullMessage) } } @JvmStatic inline fun log(level: LogLevel, tag: String = DEFAULT_TAG, message: () -> String) { - if (logLevel <= level) logInternal(level.toAndroidLogLevel(), tag, message()) - } - - fun logInternal(priority: Int, tag: String, message: String) { - if (message.length < MAX_LOG_LENGTH) { - Log.println(priority, tag, message) - return - } - - var i = 0 - val length = message.length - while (i < length) { - var newline = message.indexOf('\n', i) - newline = if (newline != -1) newline else length - do { - val end = min(newline, i + MAX_LOG_LENGTH) - val part = message.substring(i, end) - Log.println(priority, tag, part) - i = end - } while (i < newline) - i++ - } + if (logLevel.get() <= level.value) logInternal(level, tag, message()) } - fun LogLevel.toAndroidLogLevel() = when (this) { - LogLevel.VERBOSE -> Log.VERBOSE - LogLevel.DEBUG -> Log.DEBUG - LogLevel.INFO -> Log.INFO - LogLevel.WARN -> Log.WARN - LogLevel.ERROR -> Log.ERROR - LogLevel.NONE -> Log.ASSERT + fun logInternal(level: LogLevel, tag: String, message: String) { + Log.println(level.value, tag, message) } } diff --git a/library/src/main/java/com/ding1ding/jsbridge/WebViewJavascriptBridge.kt b/library/src/main/java/com/ding1ding/jsbridge/WebViewJavascriptBridge.kt index 5a21913..92ba5c8 100644 --- a/library/src/main/java/com/ding1ding/jsbridge/WebViewJavascriptBridge.kt +++ b/library/src/main/java/com/ding1ding/jsbridge/WebViewJavascriptBridge.kt @@ -247,7 +247,7 @@ class WebViewJavascriptBridge private constructor( @JvmStatic fun setLogLevel(level: Logger.LogLevel) { - Logger.logLevel = level + Logger.logLevel.set(level.value) } } }