Scoop API

scoop:scoop.log:0.1.0

src/fallback.scoop

Selected production source · src/fallback.scoop

1package scoop.log
3import scoop.platform.io.standardError
4import scoop.platform.io.writeText
6internal fun logExceptionSummary(failure: Throwable): String {
7 if (failure is Exception) return failure.message ?: "Exception (no message)"
8 return "Throwable"
9}
11private 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 return
16 }
19private 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 -> text
24 }
27private 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 }
38internal 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 }
47internal 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 }
55internal 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 }
65internal fun reportLogFlushFailure(failure: Throwable) {
66 fallback { "phase=flush error=" + logExceptionSummary(failure) }