Add ConcurrentComponentStorage.creationStacktrace

This commit is contained in:
Him188 2021-05-10 13:13:58 +08:00
parent f7f4ccf4f5
commit 1d2b2377cf
2 changed files with 13 additions and 3 deletions

View File

@ -17,7 +17,9 @@ import kotlin.LazyThreadSafetyMode.NONE
* A thread-safe implementation of [MutableComponentStorage] * A thread-safe implementation of [MutableComponentStorage]
*/ */
internal class ConcurrentComponentStorage( internal class ConcurrentComponentStorage(
private val showAllComponents: Boolean = SHOW_ALL_COMPONENTS private val showAllComponents: Boolean = SHOW_ALL_COMPONENTS,
private val creationStacktrace: Exception? =
if (SHOW_COMPONENTS_CREATION_STACKTRACE) Exception("Creation stacktrace") else null,
) : ComponentStorage, MutableComponentStorage { ) : ComponentStorage, MutableComponentStorage {
private val map = ConcurrentHashMap<ComponentKey<*>, Any?>() private val map = ConcurrentHashMap<ComponentKey<*>, Any?>()
@ -25,7 +27,8 @@ internal class ConcurrentComponentStorage(
override val size: Int get() = map.size override val size: Int get() = map.size
override operator fun <T : Any> get(key: ComponentKey<T>): T { override operator fun <T : Any> get(key: ComponentKey<T>): T {
return getOrNull(key) ?: throw NoSuchComponentException(key, this) return getOrNull(key)
?: throw NoSuchComponentException(key, this).apply { creationStacktrace?.let(::initCause) }
} }
override fun <T : Any> getOrNull(key: ComponentKey<T>): T? { override fun <T : Any> getOrNull(key: ComponentKey<T>): T? {
@ -56,4 +59,10 @@ internal class ConcurrentComponentStorage(
} }
} }
private val SHOW_ALL_COMPONENTS: Boolean by lazy(NONE) { systemProp("mirai.debug.network.show.all.components", false) } private val SHOW_ALL_COMPONENTS: Boolean by lazy(NONE) { systemProp("mirai.debug.network.show.all.components", false) }
private val SHOW_COMPONENTS_CREATION_STACKTRACE: Boolean by lazy(NONE) {
systemProp(
"mirai.debug.network.show.components.creation.stacktrace",
false
)
}

View File

@ -44,6 +44,7 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
init { init {
System.setProperty("mirai.debug.network.state.observer.logging", "full") System.setProperty("mirai.debug.network.state.observer.logging", "full")
System.setProperty("mirai.debug.network.show.all.components", "true") System.setProperty("mirai.debug.network.show.all.components", "true")
System.setProperty("mirai.debug.network.show.components.creation.stacktrace", "true")
} }
protected abstract val factory: NetworkHandlerFactory<H> protected abstract val factory: NetworkHandlerFactory<H>