diff options
author | Allan Wang <me@allanwang.ca> | 2017-12-31 15:07:05 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-31 15:07:05 -0500 |
commit | e2009a3e33a58bab37f433845f6d105cb7821da1 (patch) | |
tree | b23f3587db872be7b7ac6a4885862c7d56e35be3 /core/src/main/kotlin/ca/allanwang/kau/logging | |
parent | 2718156fe63304ffb792cc3cb8592ad6e494a8b0 (diff) | |
download | kau-e2009a3e33a58bab37f433845f6d105cb7821da1.tar.gz kau-e2009a3e33a58bab37f433845f6d105cb7821da1.tar.bz2 kau-e2009a3e33a58bab37f433845f6d105cb7821da1.zip |
Enhancement/logger (#120)
* Rewrite logger to use lazy loading
* Clean up and add to changelog
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/logging')
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt | 127 |
1 files changed, 62 insertions, 65 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 61f0708..da1c05b 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt @@ -5,13 +5,22 @@ 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] - * Almost everything is opened to make everything customizable + * Only direct lazy logging is supported, as for best results, + * applications should extend this and use const/final flags to decide whether logging occurs + * That way, it will be stripped away by proguard + * + * Generally speaking, verbose log may contain private information, + * as it should be stripped away from production build + * + * Debug and info logs may contain sensitive info, and may be differentiated by creating a method such as + * inline fun _d(message: () -> Any?) { + * if (BuildConfig.DEBUG) d(message) + * } */ open class KauLogger(val tag: String) { @@ -20,83 +29,71 @@ open class KauLogger(val tag: String) { */ open var enabled = true - /** - * Global toggle to show private text - */ - open var showPrivateText = false + inline fun v(message: () -> Any?) = log(Log.VERBOSE, message) - /** - * If both msg and priv msg are accepted, output the combined output - */ - open var messageJoiner: (msg: String, privMsg: String) -> String = { msg, privMsg -> "$msg: $privMsg" } + inline fun i(message: () -> Any?) = log(Log.INFO, message) - /** - * Open hook to change the output of the logger (for instance, output to stdout rather than Android log files - * Does not use reference notation to avoid constructor leaks - */ - open var logFun: (priority: Int, message: String?, privateMessage: String?, t: Throwable?) -> Unit = { p, m, pm, t -> - logImpl(p, m, pm, t) - } + inline fun d(message: () -> Any?) = log(Log.DEBUG, message) - /** - * 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 } + inline fun e(t: Throwable? = null, message: () -> Any?) = log(Log.ERROR, message, t) - open fun disable(disable: Boolean = true): KauLogger { - enabled = !disable - return this + inline fun eThrow(message: Any) = with(message.toString()) { + log(Log.ERROR, { this }, Throwable(this)) } - open fun debug(enable: Boolean) { - filter = if (enable) { _ -> true } else { i -> i != Log.VERBOSE && i != Log.DEBUG } - showPrivateText = enable + inline fun log(priority: Int, message: () -> Any?, t: Throwable? = null) { + if (enabled) + logImpl(priority, message()?.toString() ?: "null", t) } - private fun log(priority: Int, message: String?, privateMessage: String?, t: Throwable? = null) { - if (!shouldLog(priority, message, privateMessage, t)) return - logImpl(priority, message, privateMessage, t) + open fun logImpl(priority: Int, message: String, t: Throwable?) { + if (t != null) + Log.e(tag, message, t) + else + Log.println(priority, tag, message) } /** - * Condition to pass to allow the input to be logged - */ - protected open fun shouldLog(priority: Int, message: String?, privateMessage: String?, t: Throwable?): Boolean - = enabled && filter(priority) - - /** - * Base implementation of the Android logger + * Log the looper */ - 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) + inline fun checkThread(id: Int) { + d { + val name = Thread.currentThread().name + val status = if (Looper.myLooper() == Looper.getMainLooper()) "is" else "is not" + "$id $status in the main thread - thread name: $name" + } } - 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) + fun extend(tag: String) = KauLoggerExtension(tag, this) +} + +/** + * Tag extender for [KauLogger] + * Will prepend [tag] to any expected log output by [logger] + * Note that if the parent logger is disabled, the extension logger will not output anything either + */ +class KauLoggerExtension(val tag: String, val logger: KauLogger) { + + inline fun v(message: () -> Any?) = log(Log.VERBOSE, message) + + inline fun i(message: () -> Any?) = log(Log.INFO, message) + + inline fun d(message: () -> Any?) = log(Log.DEBUG, message) + + 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)) } - /** - * 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") + inline fun log(priority: Int, message: () -> Any?, t: Throwable? = null) = + logger.log(priority, { "$tag: ${message()?.toString() ?: "null"}" }, t) + + inline fun checkThread(id: Int) { + d { + val name = Thread.currentThread().name + val status = if (Looper.myLooper() == Looper.getMainLooper()) "is" else "is not" + "$id $status in the main thread - thread name: $name" + } } -}
\ No newline at end of file +} |