aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-08-12 10:09:01 -0700
committerGitHub <noreply@github.com>2017-08-12 10:09:01 -0700
commit10617f02a95b162695ea068c0be6acceda74cf35 (patch)
tree06943d13034cbcf0e7bdd01960162e62952bb51c /core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt
parent02e1dbc84425b0ac7f771c82f70444f742397452 (diff)
downloadkau-10617f02a95b162695ea068c0be6acceda74cf35.tar.gz
kau-10617f02a95b162695ea068c0be6acceda74cf35.tar.bz2
kau-10617f02a95b162695ea068c0be6acceda74cf35.zip
Release 3.3.1 (#36)3.3.1
* Add open message joiner function * Update text extraction * Fix background tint * Rename logger file * Test codecov * Remove coverage * Enhancement/swipe (#35) * Merge swipe onPostCreate with swipe onCreate * Update samples and docs * Add deprecated method to maintain compatibility * Replace exception with illegal argument * Check if parent exists before configurations in searchview
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.kt76
1 files changed, 76 insertions, 0 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
new file mode 100644
index 0000000..14655f0
--- /dev/null
+++ b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt
@@ -0,0 +1,76 @@
+@file:Suppress("NOTHING_TO_INLINE")
+
+package ca.allanwang.kau.logging
+
+import android.os.Looper
+import android.util.Log
+
+
+/**
+ * Created by Allan Wang on 2017-05-28.
+ *
+ * Base logger class with a predefined tag
+ * This may be extended by an object to effectively replace [Log]
+ */
+open class KauLogger(val tag: String) {
+
+ open var enabled = true
+ open var showPrivateText = false
+ open var messageJoiner: (msg: String, privMsg: String) -> String = { msg, privMsg -> "$msg: $privMsg" }
+
+ /**
+ * Filter pass-through to decide what we wish to log
+ * By default, we will ignore verbose and debug logs
+ * @returns {@code true} to log the message, {@code false} to ignore
+ */
+ open var filter: (Int) -> Boolean = { it != Log.VERBOSE && it != Log.DEBUG }
+
+ open fun disable(disable: Boolean = true): KauLogger {
+ enabled = !disable
+ return this
+ }
+
+ open fun debug(enable: Boolean) {
+ filter = if (enable) { _ -> true } else { i -> i != Log.VERBOSE && i != Log.DEBUG }
+ showPrivateText = enable
+ }
+
+ open fun log(priority: Int, message: String?, privateMessage: String?, t: Throwable? = null) {
+ if (!shouldLog(priority, message, privateMessage, t)) return
+ logImpl(priority, message, privateMessage, t)
+ }
+
+ protected open fun shouldLog(priority: Int, message: String?, privateMessage: String?, t: Throwable?): Boolean
+ = enabled && filter(priority)
+
+ protected open fun logImpl(priority: Int, message: String?, privateMessage: String?, t: Throwable?) {
+ val text = if (showPrivateText) {
+ if (message == null) privateMessage
+ else if (privateMessage == null) message
+ else messageJoiner(message, privateMessage)
+ } else message
+
+ if (t != null) Log.e(tag, text ?: "Error", t)
+ else if (!text.isNullOrBlank()) Log.println(priority, tag, text)
+ }
+
+ open fun v(text: String?, privateText: String? = null) = log(Log.VERBOSE, text, privateText)
+ open fun d(text: String?, privateText: String? = null) = log(Log.DEBUG, text, privateText)
+ open fun i(text: String?, privateText: String? = null) = log(Log.INFO, text, privateText)
+ open fun e(text: String?, privateText: String? = null) = log(Log.ERROR, text, privateText)
+ open fun a(text: String?, privateText: String? = null) = log(Log.ASSERT, text, privateText)
+ open fun e(t: Throwable?, text: String?, privateText: String? = null) = log(Log.ERROR, text, privateText, t)
+ open fun eThrow(text: String?) {
+ if (text != null)
+ e(Throwable(text), text)
+ }
+
+ /**
+ * Log the looper
+ */
+ open fun checkThread(id: Int) {
+ val name = Thread.currentThread().name
+ val status = if (Looper.myLooper() == Looper.getMainLooper()) "is" else "is not"
+ d("$id $status in the main thread - thread name: $name")
+ }
+} \ No newline at end of file