-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ding1dingx/dev
feat(logger): Implement a custom logger with configurable log levels
- Loading branch information
Showing
8 changed files
with
136 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,2 @@ | ||
# This will strip `Log.v`, `Log.d`, and `Log.i` statements and will leave `Log.w` and `Log.e` statements intact. | ||
-assumenosideeffects class android.util.Log { | ||
public static boolean isLoggable(java.lang.String, int); | ||
public static int v(...); | ||
public static int d(...); | ||
public static int i(...); | ||
} | ||
|
||
-keep class java.lang.reflect.** { *; } | ||
-keep class kotlin.reflect.** { *; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.ding1ding.jsbridge | ||
|
||
import android.util.Log | ||
import kotlin.math.min | ||
|
||
object Logger { | ||
private const val MAX_LOG_LENGTH = 4000 | ||
const val DEFAULT_TAG = "WebViewJsBridge" | ||
|
||
@Volatile | ||
var logLevel = LogLevel.INFO | ||
|
||
enum class LogLevel { VERBOSE, DEBUG, INFO, WARN, ERROR, NONE } | ||
|
||
@JvmStatic | ||
inline fun v(tag: String = DEFAULT_TAG, message: () -> String) = | ||
log(LogLevel.VERBOSE, tag, message) | ||
|
||
@JvmStatic | ||
inline fun d(tag: String = DEFAULT_TAG, message: () -> String) = log(LogLevel.DEBUG, tag, message) | ||
|
||
@JvmStatic | ||
inline fun i(tag: String = DEFAULT_TAG, message: () -> String) = log(LogLevel.INFO, tag, message) | ||
|
||
@JvmStatic | ||
inline fun w(tag: String = DEFAULT_TAG, message: () -> String) = log(LogLevel.WARN, tag, message) | ||
|
||
@JvmStatic | ||
inline fun e(tag: String = DEFAULT_TAG, message: () -> String) = log(LogLevel.ERROR, tag, message) | ||
|
||
@JvmStatic | ||
inline fun e(throwable: Throwable, tag: String = DEFAULT_TAG, message: () -> String = { "" }) { | ||
if (logLevel <= LogLevel.ERROR) { | ||
val fullMessage = buildString { | ||
append(message()) | ||
if (isNotEmpty() && message().isNotEmpty()) append(": ") | ||
append(Log.getStackTraceString(throwable)) | ||
} | ||
logInternal(Log.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++ | ||
} | ||
} | ||
|
||
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 | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
library/src/main/java/com/ding1ding/jsbridge/MessageHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.ding1ding.jsbridge | ||
|
||
interface MessageHandler<in InputType, out OutputType> { | ||
fun handle(parameter: InputType): OutputType | ||
interface MessageHandler<in Input, out Output> { | ||
fun handle(parameter: Input): Output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.