Skip to content

Better way to set CoroutineContext#DEBUG value #316

@dmytrodanylyk

Description

@dmytrodanylyk

Currently CoroutineContext#DEBUG rely on system property kotlinx.coroutines.debug.

private const val DEBUG_PROPERTY_NAME = "kotlinx.coroutines.debug"

private val DEBUG = run {
    val value = try { System.getProperty(DEBUG_PROPERTY_NAME) }
        catch (e: SecurityException) { null }
    when (value) {
        "auto", null -> CoroutineId::class.java.desiredAssertionStatus()
        "on", "" -> true
        "off" -> false
        else -> error("System property '$DEBUG_PROPERTY_NAME' has unrecognized value '$value'")
    }
}

In Android we usually rely on BuildConfig.DEBUG flag which indicate if app was build in debug or production mode.

Currently to set CoroutineContext#DEBUG value we need to use following code:

System.setProperty("kotlinx.coroutines.debug", if (BuildConfig.DEBUG) "on" else "off")

Which is not type safe, so my proposal is to:

  • make CoroutineContext#DEBUG_PROPERTY_NAME public, also move "on", "off", "auto" to public constants as well
  • or make alternative, simpler mechanism to change CoroutineContext#DEBUG value

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions