Skip to content

Commit

Permalink
Log exceptions in FileWatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinRNDR committed Jan 3, 2021
1 parent 5c7c32a commit 3e23d77
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions orx-file-watcher/src/main/kotlin/FileWatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import mu.KotlinLogging
import org.openrndr.Program
import org.openrndr.launch
import java.io.File
Expand All @@ -13,6 +14,7 @@ import java.nio.file.Path
import java.nio.file.StandardWatchEventKinds
import java.nio.file.WatchKey
import kotlin.concurrent.thread
private val logger = KotlinLogging.logger {}

class FileWatcher(private val program: Program, val file: File, private val onChange: (File) -> Unit) {
val path = file.absoluteFile.toPath()
Expand Down Expand Up @@ -53,7 +55,9 @@ fun <T> watchFile(program: Program, file: File, transducer: (File) -> T): () ->
try {
result = transducer(file)
} catch (e: Throwable) {
e.printStackTrace()
logger.error(e) {
"""exception while transducing file"""
}
}
}

Expand Down Expand Up @@ -87,7 +91,7 @@ fun <T> (() -> T).triggerChange() {
/**
* add watcher to file watcher
*/
fun <T, R> (() -> T).watch(transducer: (T) -> R):()->R {
fun <T, R> (() -> T).watch(transducer: (T) -> R): () -> R {

var result = transducer(this())

Expand Down Expand Up @@ -141,11 +145,8 @@ fun main() {
val a = watchFile(Program(), File("README.md")) {
it.readText()
}


a.stop()
a.triggerChange()

while (true) {
println(a())
Thread.sleep(2000)
Expand Down

0 comments on commit 3e23d77

Please sign in to comment.