Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.
This repository was archived by the owner on Nov 14, 2018. It is now read-only.

Log with lazy messages and default tag parameters #546

@RamV13

Description

@RamV13

I think it would be extremely useful to extend the android.util.Log API with the functionality of lazy String messages and default tag parameters like so

Log.i("tag") { "msg" }
// or ...
Log.i { "msg" } // where tag = the calling Class' simple name

This could look like

interface Log {
    fun i(tag: String? = null, msg: () -> String): Int
}

val Any.Log: Log
    get() = object : Log {
        override fun i(tag: String?, msg: () -> String) {
            val tag = tag ?: this@Log.javaClass.simpleName
            return if (Log.isLoggable(tag, Log.INFO)) android.util.Log.i(tag, msg()) else 0
        }
    }

This, of course, isn't ideal because we have to introduce an extension property. Even without the default tag parameter, however, due to https://youtrack.jetbrains.com/issue/KT-11968, it would have to be temporarily implemented in some other way besides a static extension function. For example,

interface KTXLog {
    fun i(tag: String, msg: () -> String): Int
}

val Log = object : KTXLog {
    override fun i(tag: String, msg: () -> String): Int {
        return if (Log.isLoggable(tag, Log.INFO)) android.util.Log.i(tag, msg()) else 0
    }
}

This is also clearly not ideal so I'm not sure if this is something that we should wait on or can just implement in one of the two ways as above for now. I'm interested to hear other thoughts on this.

Note: This is similar to #289 but doesn't simplify the API excessively and also leverages the lazy lambdas correctly. On the other hand, this version doesn't get the benefit of inlining.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions