aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/logging
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-08-07 22:20:57 -0700
committerGitHub <noreply@github.com>2017-08-07 22:20:57 -0700
commit02e1dbc84425b0ac7f771c82f70444f742397452 (patch)
tree05e978e7588e30ce653428671f3d9f5df5397385 /core/src/main/kotlin/ca/allanwang/kau/logging
parent187d8e64dc7189f63707d154166867084662dbe3 (diff)
downloadkau-02e1dbc84425b0ac7f771c82f70444f742397452.tar.gz
kau-02e1dbc84425b0ac7f771c82f70444f742397452.tar.bz2
kau-02e1dbc84425b0ac7f771c82f70444f742397452.zip
Release 3.3.0 (#32)3.3.0
* Rewrite Logger (#29) * Remove dependency on timber * Update logger * Reorder throwabl * Fix lint * Update readme * Blank target * Create Zip (#30) * Finish zips with tests * Finalize * Update changelog * Add log hooks * Open most logging functions * Remap kpref items (#31) * Update readme * Generate files and prepare release * Kpref -
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/logging')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/logging/KL.kt2
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt74
2 files changed, 62 insertions, 14 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 4fa3360..24146b0 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/logging/KL.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/logging/KL.kt
@@ -3,4 +3,4 @@ package ca.allanwang.kau.logging
/**
* Created by Allan Wang on 2017-06-19.
*/
-object KL : TimberLogger("KAU") \ No newline at end of file
+object KL : KauLogger("KAU") \ No newline at end of file
diff --git a/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt b/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt
index 4cf566a..2fbecf5 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt
@@ -2,23 +2,71 @@
package ca.allanwang.kau.logging
-import timber.log.Timber
+import android.os.Looper
+import android.util.Log
/**
* Created by Allan Wang on 2017-05-28.
*
- * Timber extension that will embed the tag as part of the message for each log item
+ * Base logger class with a predefined tag
+ * This may be extended by an object to effectively replace [Log]
*/
-open class TimberLogger(tag: String) {
- val TAG = "$tag: %s"
- inline fun e(s: String) = Timber.e(TAG, s)
- inline fun e(t: Throwable?, s: String = "error") = if (t == null) e(s) else Timber.e(t, TAG, s)
- inline fun d(s: String) = Timber.d(TAG, s)
- inline fun i(s: String) = Timber.i(TAG, s)
- inline fun v(s: String) = Timber.v(TAG, s)
- inline fun eThrow(s: String) = e(Throwable(s))
-// fun plant() {
-// Timber.plant(Timber.Tree())
-// }
+open class KauLogger(val tag: String) {
+
+ open var enabled = true
+ open var showPrivateText = false
+
+ /**
+ * 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?) {
+ var text = message ?: ""
+ if (showPrivateText && privateMessage != null)
+ text += "\n-\t$privateMessage"
+ if (t != null) Log.e(tag, text, t)
+ else if (text.isNotBlank()) 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