Added debug event logs to the event dispatcher

This commit is contained in:
Robinson 2023-05-28 16:56:05 +02:00
parent c119981859
commit 23572ea9fd
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C

View File

@ -18,12 +18,14 @@ package dorkbox.network.connection
import dorkbox.network.Configuration import dorkbox.network.Configuration
import dorkbox.util.NamedThreadFactory import dorkbox.util.NamedThreadFactory
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import mu.KotlinLogging
import java.util.concurrent.* import java.util.concurrent.*
/** /**
@ -31,6 +33,11 @@ import java.util.concurrent.*
*/ */
class EventDispatcher { class EventDispatcher {
companion object { companion object {
private val DEBUG_EVENTS = false
private val traceId = atomic(0)
private val logger = KotlinLogging.logger(EventDispatcher::class.java.simpleName)
enum class EVENT { enum class EVENT {
INIT, CONNECT, DISCONNECT, CLOSE, RMI INIT, CONNECT, DISCONNECT, CLOSE, RMI
} }
@ -55,7 +62,16 @@ class EventDispatcher {
* This is because there are blocking dependencies: DISCONNECT -> CONNECT. * This is because there are blocking dependencies: DISCONNECT -> CONNECT.
*/ */
fun launch(event: EVENT, function: suspend CoroutineScope.() -> Unit): Job { fun launch(event: EVENT, function: suspend CoroutineScope.() -> Unit): Job {
return eventData[event.ordinal].launch(block = function) return if (DEBUG_EVENTS) {
val id = traceId.getAndIncrement()
eventData[event.ordinal].launch(block = {
logger.debug { "Starting $event : $id" }
function()
logger.debug { "Finished $event : $id" }
})
} else {
eventData[event.ordinal].launch(block = function)
}
} }
} }
} }