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

commonMain.kr.jadekim.logger.context.LogContext.kt Maven / Gradle / Ivy

There is a newer version: 2.0.10
Show newest version
package kr.jadekim.logger.context

import co.touchlab.stately.collections.SharedHashMap
import co.touchlab.stately.concurrency.ThreadLocalRef

interface LogContext : Map {

    operator fun plus(other: LogContext?): LogContext

    fun clone(): LogContext = LogContext(toMap())

    fun toImmutable(): LogContext = LogContext(toMap())

    fun toMutable(): MutableLogContext = MutableLogContext(toMutableMap())
}

internal class LogContextImpl(data: Map) : LogContext, Map by data {

    override fun plus(other: LogContext?): LogContext = if (other == null) this else {
        LogContext((this as Map).plus(other))
    }
}

fun LogContext(data: Map): LogContext = LogContextImpl(data)

object EmptyLogContext : LogContext by LogContext(emptyMap())

interface MutableLogContext : LogContext, MutableMap {

    override operator fun plus(other: LogContext?): MutableLogContext = if (other == null) clone() else {
        val data = mutableMapOf()

        data.putAll(this)
        data.putAll(other)

        MutableLogContext(data)
    }

    operator fun plusAssign(other: LogContext?) {
        if (other != null) {
            putAll(other)
        }
    }

    fun snap() = LogContext(toMap())

    override fun clone(): MutableLogContext = MutableLogContext(toMutableMap())
}

internal class MutableLogContextImpl(
    data: MutableMap,
) : MutableLogContext,
    MutableMap by SharedHashMap() {

    init {
        putAll(data)
    }
}

fun MutableLogContext(data: MutableMap = mutableMapOf()): MutableLogContext = MutableLogContextImpl(data)

object GlobalLogContext : MutableLogContext by MutableLogContext()

object ThreadLogContext : MutableLogContext {

    private val threadLocal = ThreadLocalRef()

    private val data: MutableLogContext
        get() {
            var data: MutableLogContext? = threadLocal.get()

            if (data == null) {
                data = MutableLogContext()
                threadLocal.set(data)
            }

            return data
        }

    override val entries: MutableSet>
        get() = data.entries

    override val keys: MutableSet
        get() = data.keys

    override val size: Int
        get() = data.size

    override val values: MutableCollection
        get() = data.values

    override fun containsKey(key: String): Boolean = data.containsKey(key)

    override fun containsValue(value: Any?): Boolean = data.containsValue(value)

    override fun get(key: String): Any? = data[key]

    override fun isEmpty(): Boolean = data.isEmpty()

    override fun clear() = data.clear()

    override fun put(key: String, value: Any?): Any? = data.put(key, value)

    override fun putAll(from: Map) = data.putAll(from)

    override fun remove(key: String): Any? = data.remove(key)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy