Skip to content

Commit

Permalink
Version 1.0.0:
Browse files Browse the repository at this point in the history
- removed KotlinX.DateTime & moved from Instant to Timestamp.
- WASM support.
  • Loading branch information
SalomonBrys committed Aug 14, 2023
1 parent 8b826a9 commit ba5de82
Show file tree
Hide file tree
Showing 22 changed files with 141 additions and 168 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ plugins {

allprojects {
group = "org.kodein.log"
version = "0.19.0"
version = "1.0.0"
}
114 changes: 11 additions & 103 deletions canard/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,132 +3,40 @@ plugins {
}

kotlin.kodein {
jsEnv()
all()

// all() TODO kotlinx-datetime does not support WASM

val commonMain by common.main
// val datetimeMain by sourceSets.creating {
// dependsOn(common.main)
// dependencies {
// api(libs.kotlinx.datetime)
// }
// }
val datetimeMain by kotlin.sourceSets.creating {
dependsOn(commonMain)
dependencies {
api(libs.kotlinx.datetime)
}
}

// add(kodeinTargets.jvm.jvm) {
// main.dependsOn(datetimeMain)
// target.setCompileClasspath()
// main.dependencies {
// implementation(libs.slf4j.api)
// compileOnly(rootProject.files("libs/android-log.jar"))
// }
// }
// }
jvm {
target.setCompileClasspath()
sources.main {
dependsOn(datetimeMain)
dependencies {
implementation(libs.slf4j.api)
compileOnly(rootProject.files("libs/android-log.jar"))
}
}
}

// add(kodeinTargets.js.js) {
// main.dependsOn(datetimeMain)
// configure(listOf(mainCompilation, testCompilation)) {
// kotlinOptions {
// sourceMap = true
// sourceMapEmbedSources = "always"
// }
// }
// }
js {
sources.main { dependsOn(datetimeMain) }
compilations.configureEach {
kotlinOptions {
sourceMap = true
sourceMapEmbedSources = "always"
}
allApple {
compilations.main {
cinterops.create("darwin_log")
}
}

val allNativeMain by kotlin.sourceSets.creating {
dependsOn(commonMain)
}
// val defaultNativeMain by sourceSets.creating {
// dependsOn(common.main)
// }
val defaultNativeMain by kotlin.sourceSets.creating {
dependsOn(allNativeMain)
}

// add(kodeinTargets.native.allDesktop) {
// main {
// dependsOn(defaultNativeMain)
// dependsOn(datetimeMain)
// }
// }
allDesktop {
sources.main {
dependsOn(defaultNativeMain)
dependsOn(datetimeMain)
languageSettings {
optIn("kotlinx.cinterop.ExperimentalForeignApi")
}
}
}

// val embeddedNativeMain by sourceSets.creating {
// dependsOn(common.main)
// }
val embeddedNativeMain by kotlin.sourceSets.creating {
dependsOn(allNativeMain)
val nativeMain by kotlin.sourceSets.getting
val defaultNativeMain by kotlin.sourceSets.creating {
dependsOn(nativeMain)
}

// add(kodeinTargets.native.allEmbeddedLinux) {
// main {
// dependsOn(defaultNativeMain)
// dependsOn(embeddedNativeMain)
// }
// }
linuxArm64 {
addAll(targets.allNative - targets.allApple) {
sources.main {
dependsOn(defaultNativeMain)
dependsOn(embeddedNativeMain)
}
}

// add(kodeinTargets.native.allDarwin) {
// main.dependsOn(datetimeMain)
// mainCompilation.cinterops.create("darwin_log")
// }
val allAppleMobileMain by kotlin.sourceSets.creating {
dependsOn(allNativeMain)
}
// TODO watchosDeviceArm64 is not supported by KotlinX DateTime
addAll(targets.allAppleMobile - targets.watchosDeviceArm64) {
sources.main {
dependsOn(allAppleMobileMain)
dependsOn(datetimeMain)
languageSettings {
optIn("kotlinx.cinterop.ExperimentalForeignApi")
}
}
compilations.main {
cinterops.create("darwin_log")
}
}
}

kotlin.sourceSets.all {
languageSettings {
optIn("kotlin.RequiresOptIn")
}
}

kodeinUpload {
Expand Down
5 changes: 3 additions & 2 deletions canard/src/commonMain/kotlin/org/kodein/log/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private val defaultFrontEnds by lazy {
listOf(defaultLogFrontend)
}

@Suppress("NOTHING_TO_INLINE")
/**
* Logger
*/
Expand Down Expand Up @@ -87,7 +86,7 @@ public class Logger(
/**
* Metadata that helps describe log messages
*/
public data class Entry(val level: Level, val ex: Throwable? = null, val meta: Map<String, Any> = emptyMap(), val instant: Instant = now())
public data class Entry(val level: Level, val ex: Throwable? = null, val meta: Map<String, Any> = emptyMap(), val timestamp: Timestamp = now())

@PublishedApi
internal fun createEntry(level: Level, error: Throwable? = null, meta: Map<String, Any>): Entry? =
Expand Down Expand Up @@ -123,6 +122,7 @@ public class Logger(
/**
* Log an exception with [WARNING] severity
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun warning(ex: Throwable) { log(level = WARNING, error = ex) }

/**
Expand All @@ -132,6 +132,7 @@ public class Logger(
/**
* Log an exception with [ERROR] severity
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun error(ex: Throwable) { log(level = ERROR, error = ex) }

public companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.kodein.log.toLocalString


public fun printLogIn(tag: Logger.Tag, entry: Logger.Entry, msg: String?, printer: (String) -> Unit) {
val prefix = " ".repeat(7 - entry.level.name.length) + "${entry.level.name}: ${entry.instant.toLocalString()} |"
val prefix = " ".repeat(7 - entry.level.name.length) + "${entry.level.name}: ${entry.timestamp.toLocalString()} |"
val indent = " ".repeat(prefix.length)
if (msg != null) {
msg.lines().forEachIndexed { i, l ->
Expand Down
7 changes: 0 additions & 7 deletions canard/src/commonMain/kotlin/org/kodein/log/instant.kt

This file was deleted.

10 changes: 10 additions & 0 deletions canard/src/commonMain/kotlin/org/kodein/log/timestamp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.kodein.log

import kotlin.jvm.JvmInline

@JvmInline
public value class Timestamp(public val msecSinceEpoch: ULong)

public expect fun now(): Timestamp

public expect fun Timestamp.toLocalString(): String
6 changes: 3 additions & 3 deletions canard/src/commonTest/kotlin/org/kodein/log/TestFrontend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package org.kodein.log


class TestFrontend : LogFrontend {
private val instant = now()
private val timestamp = now()

val entries: MutableList<Triple<Logger.Tag, Logger.Entry, String?>> = ArrayList()

override fun getReceiverFor(tag: Logger.Tag): LogReceiver =
LogReceiver { entry, message ->
entries += Triple(tag, entry.copy(instant = instant), message)
entries += Triple(tag, entry.copy(timestamp = timestamp), message)
}

fun testEntry(level: Logger.Level, ex: Throwable? = null, meta: Map<String, Any> = emptyMap()): Logger.Entry =
Logger.Entry(level, ex, meta, instant)
Logger.Entry(level, ex, meta, timestamp)
}
11 changes: 0 additions & 11 deletions canard/src/datetimeMain/kotlin/org/kodein/log/instantDateTime.kt

This file was deleted.

This file was deleted.

19 changes: 19 additions & 0 deletions canard/src/jsMain/kotlin/org/kodein/log/timestampJs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.kodein.log

import kotlin.js.Date


public actual fun now(): Timestamp = Timestamp(Date.now().toULong())

@Suppress("LocalVariableName")
public actual fun Timestamp.toLocalString(): String = Date(msecSinceEpoch.toDouble()).let {
val Y = it.getFullYear().toString()
val M = it.getMonth().inc().toString().padStart(2, '0')
val D = it.getDate().toString().padStart(2, '0')
val h = it.getHours().toString().padStart(2, '0')
val m = it.getMinutes().toString().padStart(2, '0')
val s = it.getSeconds().toString().padStart(2, '0')
val ss = it.getMilliseconds().toString().padStart(3, '0')

"$Y/$M/$D $h:$m:$s:$ss"
}
11 changes: 11 additions & 0 deletions canard/src/jvmMain/kotlin/org/kodein/log/timestampJvm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.kodein.log

import java.text.SimpleDateFormat
import java.util.*


public actual fun now(): Timestamp = Timestamp(System.currentTimeMillis().toULong())

private val dtf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS")

public actual fun Timestamp.toLocalString(): String = dtf.format(Date(msecSinceEpoch.toLong()))
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalNativeApi::class, ExperimentalForeignApi::class)

package org.kodein.log.frontend

import kotlinx.cinterop.ExperimentalForeignApi
Expand All @@ -10,9 +8,11 @@ import kotlin.experimental.ExperimentalNativeApi
import kotlin.native.concurrent.SharedImmutable


@SharedImmutable
@OptIn(ExperimentalForeignApi::class)
private val stderr = fdopen(2, "w")

@OptIn(ExperimentalForeignApi::class)
internal actual fun errPrintln(msg: String) { fprintf(stderr, "%s\n", msg.cstr) }

@OptIn(ExperimentalNativeApi::class)
internal actual fun Throwable.getStackTraceStrings(): Array<String> = getStackTrace()
29 changes: 29 additions & 0 deletions canard/src/nativeMain/kotlin/org/kodein/log/timestampNative.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.kodein.log

import kotlinx.cinterop.*
import platform.posix.*


@OptIn(UnsafeNumber::class, ExperimentalForeignApi::class)
public actual fun now(): Timestamp {
memScoped {
val spec = alloc<timespec>()
clock_gettime(CLOCK_REALTIME.convert(), spec.ptr)
return Timestamp(spec.tv_sec.toULong() * 1_000u + spec.tv_nsec.toULong() / 1_000_000u)
}
}

@OptIn(UnsafeNumber::class, ExperimentalForeignApi::class)
public actual fun Timestamp.toLocalString(): String {
memScoped {
val ms = msecSinceEpoch % 1_000u
val sec = alloc<time_tVar>()
sec.value = (msecSinceEpoch / 1_000u).convert()
val info = localtime(sec.ptr)

val chars = allocArray<ByteVar>(32)
strftime(chars, 31.convert(), "%Y-%m-%dT%H:%M:%S", info)

return chars.toKString() + "." + ms.toString().padStart(3, '0')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.kodein.log.frontend

import org.kodein.log.LogFrontend

public actual val defaultLogFrontend: LogFrontend = printFrontend
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.kodein.log.frontend


internal actual fun errPrintln(msg: String) { println(msg) }

internal actual fun Throwable.getStackTraceStrings(): Array<String> =
arrayOf("Stack traces are currently not supported in WASM")
7 changes: 7 additions & 0 deletions canard/src/wasmMain/kotlin/org/kodein/log/kclassWasm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.kodein.log

import kotlin.reflect.KClass

public actual val KClass<*>.platformSimpleName: String get() = simpleName ?: "?"

public actual val KClass<*>.platformPackageName: String get() = "kotlin.wasm"
Loading

0 comments on commit ba5de82

Please sign in to comment.