Skip to content

Commit

Permalink
feat(Logger): Improve logger implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
syxc committed Aug 27, 2024
1 parent 99baf5a commit e96e62e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 39 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 }
}
}

Expand Down
53 changes: 17 additions & 36 deletions library/src/main/java/com/ding1ding/jsbridge/Logger.kt
Original file line number Diff line number Diff line change
@@ -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) =
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class WebViewJavascriptBridge private constructor(

@JvmStatic
fun setLogLevel(level: Logger.LogLevel) {
Logger.logLevel = level
Logger.logLevel.set(level.value)
}
}
}

0 comments on commit e96e62e

Please sign in to comment.