aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-12-31 17:27:10 -0500
committerGitHub <noreply@github.com>2017-12-31 17:27:10 -0500
commit332d633ce32eb2d686a6342e39c0f7d4cb31edc0 (patch)
tree75690f1c3eb3df8f25fab066ce5783e8f3604d39 /core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt
parent7a9ca234c3245047ccda9597345a26884e9d3290 (diff)
downloadkau-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/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt41
1 files changed, 26 insertions, 15 deletions
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 {