All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.de.halfbit.logger.sink.memory.MemoryRingSink.kt Maven / Gradle / Ivy

The newest version!
/** Copyright 2024 Halfbit GmbH, Sergej Shafarenka */
package de.halfbit.logger.sink.memory

import de.halfbit.logger.LogLevel
import de.halfbit.logger.LoggerBuilder
import de.halfbit.logger.sink.LogEntry
import de.halfbit.logger.sink.LogPrinter
import de.halfbit.logger.sink.LogSink
import kotlinx.datetime.Instant

public interface MemoryRingSink {
    public fun getLogEntries(): List
}

public fun LoggerBuilder.registerMemoryRingSink(
    logPrinter: LogPrinter = LogPrinter.Default,
    maxEntriesCount: Int = 128,
): MemoryRingSink {
    val sink = DefaultMemoryRingSink(maxEntriesCount, logPrinter)
    replaceSink(sink)
    return sink
}

private class DefaultMemoryRingSink(
    maxEntriesCount: Int,
    private val logPrinter: LogPrinter,
) : MemoryRingSink, LogSink {
    private val entries: RingArray = RingArray(maxEntriesCount)

    override fun log(level: LogLevel, tag: String, timestamp: Instant, message: String?, err: Throwable?) {
        val entry = LogEntry(level, tag, timestamp, message, err)
        entries.add(entry)
    }

    override fun getLogEntries(): List =
        entries
            .map { logPrinter(it.level, it.tag, it.timestamp, it.message, it.err) }
            .toList()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy