Skip to content

Commit

Permalink
Add Job.mkTree extension method (#504)
Browse files Browse the repository at this point in the history
It can be useful to print a hierarchy of coroutines for debug purposes.
  • Loading branch information
pm47 authored Jul 6, 2023
1 parent 5d5e984 commit 523118f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/commonMain/kotlin/fr/acinq/lightning/utils/coroutines.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package fr.acinq.lightning.utils

import kotlinx.coroutines.Job
import kotlinx.coroutines.job

private fun Job.print(buffer: StringBuilder, prefix: String, childrenPrefix: String) {
buffer.append(prefix)
buffer.append(job.toString())
buffer.append('\n')
val it = job.children.iterator()
while (it.hasNext()) {
val child = it.next()
if (it.hasNext()) {
child.print(buffer, "$childrenPrefix├── ", "$childrenPrefix")
} else {
child.print(buffer, "$childrenPrefix└── ", "$childrenPrefix ")
}
}
}

/**
* Extension method to print a hierarchy of coroutines for debug purposes.
* Usage:
* ```
* println(scope.coroutineContext.job.mkTree())
* ```
*/
fun Job.mkTree(): String {
val sb = StringBuilder()
job.print(sb, "", "")
return sb.toString()
}
2 changes: 1 addition & 1 deletion src/jvmMain/kotlin/fr/acinq/lightning/io/JvmTcpSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class JvmTcpSocket(val socket: Socket, val loggerFactory: LoggerFactory) : TcpSo

override suspend fun send(bytes: ByteArray?, offset: Int, length: Int, flush: Boolean) =
withContext(Dispatchers.IO) {
ensureActive()
try {
ensureActive()
if (bytes != null) connection.output.writeFully(bytes, offset, length)
if (flush) connection.output.flush()
} catch (ex: ClosedSendChannelException) {
Expand Down

0 comments on commit 523118f

Please sign in to comment.