scoop:scoop.log:0.1.0
src/fallback.scoop
Selected production source · src/fallback.scoop
1
package scoop.log3
import scoop.platform.io.standardError4
import scoop.platform.io.writeText6
internal fun logExceptionSummary(failure: Throwable): String {7
if (failure is Exception) return failure.message ?: "Exception (no message)"8
return "Throwable"9
}11
private fun fallback(body: () -> String) {12
try { standardError.writeText("[log fallback] " + body() + "\n") }13
catch (ignored: Throwable) {14
// The final recovery attempt deliberately drops its own failure.15
return16
}17
}19
private fun requestDetails(level: LogLevel, target: String, location: SourceLocation?): String {20
val text = f"level=${level} target=${target}"21
return when (location) {22
case Some(val source) -> text + f" source=${source.file}:${source.line}:${source.column}"23
case None -> text24
}25
}27
private fun exceptionDetails(cause: Throwable?, messageFailure: Throwable?): String {28
val original = when (cause) {29
case Some(val failure) -> " cause=" + logExceptionSummary(failure)30
case None -> ""31
}32
return original + when (messageFailure) {33
case Some(val failure) -> " messageFailure=" + logExceptionSummary(failure)34
case None -> ""35
}36
}38
internal fun reportLogQueryFailure(39
failure: Throwable, level: LogLevel, target: String, location: SourceLocation?40
) {41
fallback {42
"phase=isEnabled error=" + logExceptionSummary(failure) + " " +43
requestDetails(level, target, location)44
}45
}47
internal fun reportLogRecordFailure(failure: Throwable, record: LogRecord) {48
fallback {49
"phase=emit error=" + logExceptionSummary(failure) + " " +50
requestDetails(record.level, record.target, Some(record.location)) +51
" message=" + record.message + exceptionDetails(record.cause, record.messageFailure)52
}53
}55
internal fun reportLogMessageFailure(56
failure: Throwable, messageFailure: Throwable, level: LogLevel, target: String,57
location: SourceLocation, cause: Throwable?58
) {59
fallback {60
"phase=message error=" + logExceptionSummary(failure) + " " +61
requestDetails(level, target, Some(location)) + exceptionDetails(cause, Some(messageFailure))62
}63
}65
internal fun reportLogFlushFailure(failure: Throwable) {66
fallback { "phase=flush error=" + logExceptionSummary(failure) }67
}