Skip to content

Commit

Permalink
Added the Log.logValues property
Browse files Browse the repository at this point in the history
  • Loading branch information
chRyNaN committed Sep 12, 2022
1 parent eb7ff1f commit 0532cff
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions logger-core/src/commonMain/kotlin/com.chrynan.logger/Log.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.chrynan.logger

import kotlinx.coroutines.flow.*

/**
* An implementation of [Logger] and [LogInitializer] that can be used as a singleton base to log
* throughout the application. Before calling one of [log] functions, a [Logger] has to be
Expand All @@ -20,13 +22,37 @@ object Log : Logger,
@Suppress("VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL") // Use new Kotlin Native memory model.
var logger: Logger = DefaultLogger()

/**
* Retrieves a [Flow] of [LogValues] that are emitted after every successful invocation of the [log] on this [Log]
* object.
*
* **Note:** If the [Log.isEnabled] property is `false` and the [Log.log] function is invoked, no [LogValues] will
* be emitted.
*
* **Note:** [LogValues] are emitted after the internal [Log.log] function is successfully invoked.
*
* **Note:** There is no guarantee that a value emitted from this [Flow] is actually logged, as it is up to the
* [Logger] implementation set as the [Log.logger] to decide how and whether to log.
*/
val logValues: Flow<LogValues>
get() = values.asStateFlow().filterNotNull()

private val values = MutableStateFlow<LogValues?>(null)

override fun init() {
(logger as? LogInitializer)?.init()
}

override fun log(logType: LogType, tag: String, message: String?, throwable: Throwable?) {
if (logger.isEnabled) {
logger.log(logType = logType, tag = tag, message = message, throwable = throwable)

values.value = LogValues(
logType = logType,
tag = tag,
message = message,
throwable = throwable
)
}
}
}

0 comments on commit 0532cff

Please sign in to comment.