Scoop API

scoop:scoop.log:0.1.0

src/provider.scoop

Selected production source · src/provider.scoop

1package scoop.log
3/**
4 * Receives evaluated events. Methods can be called concurrently and must synchronize their own
5 * mutable state. Exceptions from these methods are recovered by Logger and Log.flush.
6 */
7public interface LogProvider {
8 /** Checks whether to evaluate and submit an event, without producing output. */
9 public fun isEnabled(level: LogLevel, target: String): Boolean
10 /** Accepts an event that has passed this provider's filter. */
11 public fun emit(record: LogRecord)
12 /** Submits events accepted before this call without closing the provider. */
13 public fun flush()
16/** The initial provider, also usable to explicitly disable all logging. */
17public object NoopLogProvider : LogProvider {
18 /** Always disables message evaluation and submission. */
19 public override fun isEnabled(level: LogLevel, target: String): Boolean = false
20 /** Deliberately discards a directly submitted event. */
21 public override fun emit(record: LogRecord): Unit = Unit
22 /** There is no buffered output to flush. */
23 public override fun flush(): Unit = Unit
26/** Application-owned selection of the process-wide default provider. */
27public object Log {
28 private val current: AtomicRef<LogProvider> = AtomicRef<LogProvider>(NoopLogProvider)
30 /**
31 * Atomically installs a provider and returns the previous one. Existing calls may still use
32 * the old provider; this operation neither flushes nor closes either provider.
33 */
34 public fun setProvider(provider: LogProvider): LogProvider = current.exchange(provider)
36 internal fun provider(): LogProvider = current.load()
38 /** Flushes the currently selected provider, reporting failure to stderr on a best-effort basis. */
39 public fun flush() {
40 try { provider().flush() }
41 catch (failure: Throwable) { reportLogFlushFailure(failure) }
42 }