Skip to content

Commit

Permalink
Kotlin 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Salomon BRYS committed Aug 19, 2020
1 parent 2736e9e commit 60dae85
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {

allprojects {
group = "org.kodein.log"
version = "0.4.0"
version = "0.5.0"
}

kodeinPublications {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.kodein.log.frontend

import org.kodein.log.LogFrontend
import org.kodein.log.LogReceiver
import org.kodein.log.Logger
import org.kodein.log.darwin.*

public val iosFrontend: LogFrontend = { tag ->
public val iosFrontend: LogFrontend = LogFrontend { tag ->
val log = darwin_log_create(tag.pkg, tag.name)

({ e, m ->
LogReceiver { e, m ->
val type = when (e.level) {
Logger.Level.VERBOSE -> DARWIN_LOG_TYPE_DEBUG
Logger.Level.INFO -> DARWIN_LOG_TYPE_INFO
Expand All @@ -16,5 +17,5 @@ public val iosFrontend: LogFrontend = { tag ->
}

darwin_log_with_type(log, type, m)
})
}
}
37 changes: 24 additions & 13 deletions kodein-log/src/commonMain/kotlin/org/kodein/log/Logger.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package org.kodein.log

import org.kodein.log.Logger.Level.*
import org.kodein.log.frontend.defaultLogFrontend
import kotlin.reflect.KClass


public typealias LogFilter = (Logger.Tag, Logger.Entry) -> Logger.Entry?
public fun interface LogFilter {
public fun filter(tag: Logger.Tag, entry: Logger.Entry): Logger.Entry?
}

public fun interface LogReceiver {
public fun receive(entry: Logger.Entry, message: String?)
}

public typealias LogFrontend = (Logger.Tag) -> (Logger.Entry, String?) -> Unit
public fun interface LogFrontend {
public fun getReceiverFor(tag: Logger.Tag): LogReceiver
}

private val defaultFrontEnds by lazy {
defaultLogFrontend(Logger.Tag(Logger::class))(Logger.Entry(Logger.Level.VERBOSE), "Using platform default log front end since no front end was defined")
defaultLogFrontend
.getReceiverFor(Logger.Tag(Logger::class))
.receive(Logger.Entry(VERBOSE), "Using platform default log front end since no front end was defined")
listOf(defaultLogFrontend)
}

Expand All @@ -23,33 +34,33 @@ public class Logger(@PublishedApi internal val tag: Tag, frontEnds: Collection<L
}

@PublishedApi
internal val frontends = (if (frontEnds.isNotEmpty()) frontEnds else defaultFrontEnds) .map { it(tag) }
internal val frontends = (if (frontEnds.isNotEmpty()) frontEnds else defaultFrontEnds) .map { it.getReceiverFor(tag) }

public enum class Level(public val severity: Int) { VERBOSE(0), INFO(1), WARNING(2), ERROR(3) }

public data class Entry(val level: Level, val ex: Throwable? = null, val meta: Map<String, Any> = emptyMap())

@PublishedApi
internal fun createEntry(level: Level, error: Throwable? = null, meta: Map<String, Any>): Entry? =
filters.fold(Entry(level, error, meta)) { entry, filter -> filter(tag, entry) ?: return null }
filters.fold(Entry(level, error, meta)) { entry, filter -> filter.filter(tag, entry) ?: return null }

public inline fun log(level: Level, error: Throwable? = null, meta: Map<String, Any> = emptyMap(), msgCreator: () -> String? = { null }) {
val entry = createEntry(level, error, meta) ?: return
val msg = msgCreator()
frontends.forEach { it(entry, msg) }
frontends.forEach { it.receive(entry, msg) }
}

public inline fun verbose(msgCreator: () -> String) { log(Level.VERBOSE) { msgCreator() } }
public inline fun verbose(msgCreator: () -> String) { log(level = VERBOSE, msgCreator = msgCreator) }

public inline fun info(msgCreator: () -> String) { log(Level.INFO, msgCreator = msgCreator) }
public inline fun info(msgCreator: () -> String) { log(level = INFO, msgCreator = msgCreator) }

public inline fun warning(ex: Throwable? = null, msgCreator: () -> String) { log(Level.WARNING, ex) { msgCreator() } }
public inline fun warning(ex: Throwable) { log(Level.WARNING, ex) }
public inline fun warning(ex: Throwable? = null, msgCreator: () -> String) { log(level = WARNING, error = ex, msgCreator = msgCreator) }
public inline fun warning(ex: Throwable) { log(level = WARNING, error = ex) }

public inline fun error(ex: Throwable? = null, msgCreator: () -> String) { log(Level.ERROR, ex) { msgCreator() } }
public inline fun error(ex: Throwable) { log(Level.ERROR, ex) }
public inline fun error(ex: Throwable? = null, msgCreator: () -> String) { log(level = ERROR, error = ex, msgCreator = msgCreator) }
public inline fun error(ex: Throwable) { log(level = ERROR, error = ex) }

public companion object {
public inline fun <reified T: Any> from(frontends: Collection<LogFrontend>, filters: Collection<LogFilter> = emptyList()): Logger = Logger(Logger.Tag(T::class), frontends, filters)
public inline fun <reified T: Any> from(frontends: Collection<LogFrontend>, filters: Collection<LogFilter> = emptyList()): Logger = Logger(Tag(T::class), frontends, filters)
}
}
6 changes: 1 addition & 5 deletions kodein-log/src/commonMain/kotlin/org/kodein/log/factory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import org.kodein.log.frontend.defaultLogFrontend
import kotlin.reflect.KClass


public interface LoggerFactory {
public fun interface LoggerFactory {
public fun newLogger(tag: Logger.Tag): Logger

public companion object {
public operator fun invoke(factory: (Logger.Tag) -> Logger): LoggerFactory = object : LoggerFactory {
override fun newLogger(tag: Logger.Tag) = factory(tag)
}

public operator fun invoke(frontends: Collection<LogFrontend>, filters: Collection<LogFilter> = emptyList()): LoggerFactory = LoggerFactory {
Logger(it, frontends, filters)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package org.kodein.log.filter
import org.kodein.log.LogFilter
import org.kodein.log.frontend.getStackTraceStrings

public val logStackTrace: LogFilter = { _, e ->
public val logStackTrace: LogFilter = LogFilter { _, e ->
e.copy(meta = e.meta + ("logStackTrace" to Throwable().getStackTraceStrings().joinToString("\n")))
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class Condition {
}
}

public fun conditionList(default: Boolean, list: Iterable<Condition>): LogFilter = filter@ { tag, entry ->
public fun conditionList(default: Boolean, list: Iterable<Condition>): LogFilter = LogFilter filter@ { tag, entry ->
list.forEach {
if (it.verifies(tag)) {
return@filter if (it.display) entry else null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package org.kodein.log.frontend

import org.kodein.log.Logger
import org.kodein.log.LogFrontend
import org.kodein.log.LogReceiver
import org.kodein.log.currentTimeStr


public fun stringFrontend(getPrinter: (Logger.Entry) -> (String) -> Unit): LogFrontend = { tag ->
{ entry, msg ->
public fun stringFrontend(getPrinter: (Logger.Entry) -> (String) -> Unit): LogFrontend = LogFrontend { tag ->
LogReceiver { entry, msg ->
val printer = getPrinter(entry)

val prefix = " ".repeat(7 - entry.level.name.length) + "${entry.level.name}: ${currentTimeStr()} |"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package org.kodein.log.frontend

import org.kodein.log.Logger
import org.kodein.log.LogFrontend
import org.kodein.log.LogReceiver


public val consoleFrontend: LogFrontend = { tag ->
{ entry, msg ->
public val consoleFrontend: LogFrontend = LogFrontend { tag ->
LogReceiver { entry, msg ->
val logMsg =
if (entry.meta.isEmpty()) msg
else buildString {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.kodein.log

import kotlin.js.Date

@Suppress("LocalVariableName")
public actual fun currentTimeStr(): String = Date(Date.now()).let {
val Y = it.getFullYear().toString()
val M = it.getMonth().inc().toString().padStart(2, '0')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package org.kodein.log.frontend

import android.util.Log
import org.kodein.log.LogFrontend
import org.kodein.log.LogReceiver
import org.kodein.log.Logger

public val androidFrontend: LogFrontend = { tag ->
({ entry, message ->
public val androidFrontend: LogFrontend = LogFrontend { tag ->
LogReceiver { entry, message ->
when (entry.level) {
Logger.Level.ERROR -> {
Log.e(tag.toString(), message, entry.ex)
Expand All @@ -20,5 +21,5 @@ public val androidFrontend: LogFrontend = { tag ->
Log.v(tag.toString(), message, entry.ex)
}
}
})
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.kodein.log.frontend

import org.kodein.log.LogFrontend
import org.kodein.log.LogReceiver
import org.kodein.log.Logger
import org.slf4j.LoggerFactory

public val slf4jFrontend: LogFrontend = { tag ->
public val slf4jFrontend: LogFrontend = LogFrontend { tag ->
val logger = LoggerFactory.getLogger(tag.toString())
({ entry, message ->

LogReceiver { entry, message ->
val logMsg =
if (entry.meta.isEmpty()) message
else buildString {
Expand All @@ -21,5 +21,5 @@ public val slf4jFrontend: LogFrontend = { tag ->
Logger.Level.WARNING -> logger.warn(logMsg, entry.ex)
Logger.Level.ERROR -> logger.error(logMsg, entry.ex)
}
})
}
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
maven(url = "https://dl.bintray.com/kodein-framework/Kodein-Internal-Gradle")
}
dependencies {
classpath("org.kodein.internal.gradle:kodein-internal-gradle-settings:3.7.0-kotlin-1.4-rc-74")
classpath("org.kodein.internal.gradle:kodein-internal-gradle-settings:4.0.0")
}
}

Expand Down

0 comments on commit 60dae85

Please sign in to comment.