Skip to content

Commit

Permalink
When logging in debug mode, write a log file
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrueden committed Dec 10, 2024
1 parent 90d8bed commit 2b2e962
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/commonMain/kotlin/logging.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import platform.posix.exit
private const val EXIT_CODE_ON_FAIL = 20

var debugMode = getenv("DEBUG") !in listOf(null, "", "0", "false", "FALSE")
var logFilePath = getenv("JAUNCH_LOGFILE") ?: "jaunch.log"

private var logFile: File? = null

fun debug(vararg args: Any) { if (debugMode) report("DEBUG", *args) }

Expand Down Expand Up @@ -37,8 +40,18 @@ fun fail(message: String): Nothing {
}

private fun report(prefix: String, vararg args: Any) {
printlnErr(buildString {
val s = buildString {
append("[$prefix] ")
args.forEach { append(it) }
})
}
printlnErr(s)
if (debugMode) {
val log = logFile ?: File(logFilePath)
if (logFile == null) {
// Overwrite any log file from previous run.
if (log.exists) log.rm()
logFile = log
}
log.write("$s$NL")
}
}

0 comments on commit 2b2e962

Please sign in to comment.