-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Job.mkTree
extension method (#504)
It can be useful to print a hierarchy of coroutines for debug purposes.
- Loading branch information
Showing
2 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/commonMain/kotlin/fr/acinq/lightning/utils/coroutines.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters