diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/logging/KL.kt | 4 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt | 41 |
2 files changed, 29 insertions, 16 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/logging/KL.kt b/core/src/main/kotlin/ca/allanwang/kau/logging/KL.kt index 24146b0..f690571 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/logging/KL.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/logging/KL.kt @@ -1,6 +1,8 @@ package ca.allanwang.kau.logging +import ca.allanwang.kau.BuildConfig + /** * Created by Allan Wang on 2017-06-19. */ -object KL : KauLogger("KAU")
\ No newline at end of file +object KL : KauLogger("KAU", { BuildConfig.DEBUG })
\ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt index da1c05b..e639867 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt @@ -22,12 +22,16 @@ import android.util.Log * if (BuildConfig.DEBUG) d(message) * } */ -open class KauLogger(val tag: String) { +open class KauLogger( + /** + * Tag to be used for each log + */ + val tag: String, + /** + * Toggle to dictate whether a message should be logged + */ + var shouldLog: (priority: Int) -> Boolean = { true }) { - /** - * Global toggle to enable the whole logger - */ - open var enabled = true inline fun v(message: () -> Any?) = log(Log.VERBOSE, message) @@ -37,20 +41,22 @@ open class KauLogger(val tag: String) { inline fun e(t: Throwable? = null, message: () -> Any?) = log(Log.ERROR, message, t) - inline fun eThrow(message: Any) = with(message.toString()) { - log(Log.ERROR, { this }, Throwable(this)) + inline fun eThrow(message: Any?) { + val msg = message?.toString() ?: return + log(Log.ERROR, { msg }, Throwable(msg)) } inline fun log(priority: Int, message: () -> Any?, t: Throwable? = null) { - if (enabled) - logImpl(priority, message()?.toString() ?: "null", t) + if (shouldLog(priority)) + logImpl(priority, message()?.toString(), t) } - open fun logImpl(priority: Int, message: String, t: Throwable?) { + open fun logImpl(priority: Int, message: String?, t: Throwable?) { + val msg = message ?: "null" if (t != null) - Log.e(tag, message, t) + Log.e(tag, msg, t) else - Log.println(priority, tag, message) + Log.println(priority, tag, msg) } /** @@ -82,12 +88,17 @@ class KauLoggerExtension(val tag: String, val logger: KauLogger) { inline fun e(t: Throwable? = null, message: () -> Any?) = log(Log.ERROR, message, t) - inline fun eThrow(message: Any) = with(message.toString()) { - log(Log.ERROR, { this }, Throwable(this)) + inline fun eThrow(message: Any?) { + val msg = message?.toString() ?: return + log(Log.ERROR, { msg }, Throwable(msg)) } inline fun log(priority: Int, message: () -> Any?, t: Throwable? = null) = - logger.log(priority, { "$tag: ${message()?.toString() ?: "null"}" }, t) + logger.log(priority, { + val msg = message()?.toString() + if (msg == null) null + else "$tag: $msg" + }, t) inline fun checkThread(id: Int) { d { |