Skip to content

Commit

Permalink
refactor: Make Loggable less convenient
Browse files Browse the repository at this point in the history
Removing kotlin context receivers makes the logging API less convenient
but has to be done because they're deprecated and will be removed.

Closes #432
  • Loading branch information
grodin committed Nov 23, 2024
1 parent 4be8d13 commit 97b6199
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.omricat.maplibrarian.chartlist

import co.touchlab.kermit.Severity.Warn
import co.touchlab.kermit.Tag
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.andThen
import com.github.michaelbull.result.combine
Expand All @@ -21,6 +22,7 @@ import com.google.firebase.firestore.QuerySnapshot
import com.omricat.firebase.interop.runCatchingFirebaseException
import com.omricat.logging.Loggable
import com.omricat.logging.Logger
import com.omricat.logging.classTag
import com.omricat.logging.log
import com.omricat.maplibrarian.chartlist.ChartsRepository.AddNewChartError
import com.omricat.maplibrarian.chartlist.ChartsRepository.AddNewChartError.Cancelled
Expand All @@ -35,7 +37,6 @@ import com.omricat.maplibrarian.model.DbChartModel
import com.omricat.maplibrarian.model.UnsavedChartModel
import com.omricat.maplibrarian.model.User
import com.omricat.maplibrarian.utils.DispatcherProvider
import com.omricat.maplibrarian.utils.logAndMapException
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext

Expand All @@ -44,6 +45,9 @@ class FirebaseChartsRepository(
private val dispatchers: DispatcherProvider = DispatcherProvider.Default,
override val logger: Logger,
) : ChartsRepository, Loggable {

override val loggingTag: Tag = classTag()

override suspend fun chartsListForUser(
user: User
): Result<List<DbChartModel>, ChartsRepository.Error> =
Expand All @@ -52,7 +56,8 @@ class FirebaseChartsRepository(
db.mapsCollection(user).get().await()
}
}
.logAndMapException(::ExceptionWrappingError)
.onFailure { log(Warn, throwable = it) }
.mapError(::ExceptionWrappingError)
.andThen { snapshot ->
snapshot
.map { m -> m.parseMapModel() }
Expand All @@ -71,7 +76,8 @@ class FirebaseChartsRepository(
.await()
}
}
.logAndMapException { exception ->
.onFailure { log(Warn, throwable = it) }
.mapError { exception: FirebaseFirestoreException ->
when (exception.code) {
UNAVAILABLE -> Unavailable
ALREADY_EXISTS -> ChartExists(newChart)
Expand All @@ -83,6 +89,7 @@ class FirebaseChartsRepository(
"FirebaseFirestoreException $exception had a status code of OK. " +
"Docs say this should never happen."
)

UNKNOWN -> OtherException(exception)
else -> OtherException(exception)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.omricat.maplibrarian.root

import co.touchlab.kermit.Tag
import com.omricat.logging.Loggable
import com.omricat.logging.Logger
import com.omricat.logging.classTag
import com.omricat.logging.log
import com.omricat.maplibrarian.auth.AuthResult
import com.omricat.maplibrarian.auth.AuthResult.Authenticated
Expand Down Expand Up @@ -46,6 +48,8 @@ private class RootWorkflowImpl(
override val logger: Logger,
) : StatefulWorkflow<Unit, State, Nothing, Screen>(), Loggable, RootWorkflow {

override val loggingTag: Tag = classTag()

override fun initialState(props: Unit, snapshot: Snapshot?): State = Unauthorized

override fun render(renderProps: Unit, renderState: State, context: RenderContext): Screen =
Expand Down
19 changes: 8 additions & 11 deletions util/logging/src/main/kotlin/com/omricat/logging/Loggable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@ import kotlin.reflect.KClass
/** Convenience "mixin" interface to make logging easy. */
public interface Loggable {
public val logger: Logger
public val loggingTag: Tag
}

context(Loggable)
public inline fun <reified T : Any> T.log(
priority: Severity = Debug,
tag: String? = null,
noinline message: () -> String,
) {
logger.log(priority, Tag(tag ?: T::class.outerClassSimpleName()), message)
public inline fun <reified T : Any> T.classTag(): Tag = Tag(T::class.outerClassSimpleName())

public fun Loggable.log(priority: Severity = Debug, tag: String? = null, message: () -> String) {
logger.log(priority, tag?.let { Tag(it) } ?: this.loggingTag, message)
}

context(Loggable)
public inline fun <reified T : Any> T.log(
public fun Loggable.log(
priority: Severity = Debug,
tag: String? = null,
throwable: Throwable,
noinline message: () -> String = { throwable.message ?: "$throwable" },
message: () -> String = { throwable.message ?: "$throwable" },
) {
logger.log(priority, Tag(tag ?: T::class.outerClassSimpleName()), throwable, message)
logger.log(priority, tag?.let { Tag(it) } ?: this.loggingTag, throwable, message)
}

public fun KClass<*>.outerClassSimpleName(): String {
Expand Down
4 changes: 4 additions & 0 deletions util/logging/src/main/kotlin/com/omricat/logging/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import co.touchlab.kermit.Severity
import co.touchlab.kermit.Severity.Debug
import co.touchlab.kermit.Tag

/**
* Facade for logging. Presents a simplified interface on top of more fully-featured logging
* implementations.
*/
public interface Logger {
public fun log(
priority: Severity = Debug,
Expand Down

0 comments on commit 97b6199

Please sign in to comment.