diff options
author | Allan Wang <me@allanwang.ca> | 2017-12-31 17:27:10 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-31 17:27:10 -0500 |
commit | 332d633ce32eb2d686a6342e39c0f7d4cb31edc0 (patch) | |
tree | 75690f1c3eb3df8f25fab066ce5783e8f3604d39 /core | |
parent | 7a9ca234c3245047ccda9597345a26884e9d3290 (diff) | |
download | kau-332d633ce32eb2d686a6342e39c0f7d4cb31edc0.tar.gz kau-332d633ce32eb2d686a6342e39c0f7d4cb31edc0.tar.bz2 kau-332d633ce32eb2d686a6342e39c0f7d4cb31edc0.zip |
Misc (#122)3.6.1
* Update dependencies and use dexcount
* Add should log extender
* Allow null message
* Add nullable eThro
* Update changelog
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 { |