From c7460f45d78e6b3bf51657ece21df81f388d705c Mon Sep 17 00:00:00 2001 From: Nick Apperley Date: Tue, 14 Jul 2020 17:56:24 +1200 Subject: [PATCH] Add Linux X64 Support (#119) This commit add support for linux x64 module. All the code for linux exists but it still lacks of packaging as we failed to deploy it to github packages properly. * Created linuxX64 module, and updated Kotlin to 1.3.72. * Implemented functionality for console logging. * Used AtomicReference for all properties in KotlinLoggingConfiguration.kt. * Adjusted encapsulation with KotlinLoggingConfiguration.kt. --- .gitignore | 2 + build.gradle.kts | 5 +- src/linuxX64Main/kotlin/mu/Appender.kt | 9 ++ .../kotlin/mu/ConsoleOutputAppender.kt | 15 ++ .../kotlin/mu/DefaultMessageFormatter.kt | 36 +++++ src/linuxX64Main/kotlin/mu/Formatter.kt | 14 ++ src/linuxX64Main/kotlin/mu/KLogger.kt | 129 ++++++++++++++++++ src/linuxX64Main/kotlin/mu/KMarkerFactory.kt | 8 ++ src/linuxX64Main/kotlin/mu/KotlinLogging.kt | 16 +++ .../kotlin/mu/KotlinLoggingConfiguration.kt | 25 ++++ .../kotlin/mu/KotlinLoggingLevel.kt | 13 ++ src/linuxX64Main/kotlin/mu/Marker.kt | 6 + .../kotlin/mu/internal/KLoggerLinux.kt | 114 ++++++++++++++++ .../kotlin/mu/internal/MarkerLinux.kt | 8 ++ 14 files changed, 398 insertions(+), 2 deletions(-) create mode 100644 src/linuxX64Main/kotlin/mu/Appender.kt create mode 100644 src/linuxX64Main/kotlin/mu/ConsoleOutputAppender.kt create mode 100644 src/linuxX64Main/kotlin/mu/DefaultMessageFormatter.kt create mode 100644 src/linuxX64Main/kotlin/mu/Formatter.kt create mode 100644 src/linuxX64Main/kotlin/mu/KLogger.kt create mode 100644 src/linuxX64Main/kotlin/mu/KMarkerFactory.kt create mode 100644 src/linuxX64Main/kotlin/mu/KotlinLogging.kt create mode 100644 src/linuxX64Main/kotlin/mu/KotlinLoggingConfiguration.kt create mode 100644 src/linuxX64Main/kotlin/mu/KotlinLoggingLevel.kt create mode 100644 src/linuxX64Main/kotlin/mu/Marker.kt create mode 100644 src/linuxX64Main/kotlin/mu/internal/KLoggerLinux.kt create mode 100644 src/linuxX64Main/kotlin/mu/internal/MarkerLinux.kt diff --git a/.gitignore b/.gitignore index eb56374d..2ae05eba 100644 --- a/.gitignore +++ b/.gitignore @@ -73,3 +73,5 @@ fabric.properties # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +github_packages.properties +github_packages.properties.old diff --git a/build.gradle.kts b/build.gradle.kts index a195817c..4845d742 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,8 +1,8 @@ import org.jetbrains.dokka.gradle.DokkaTask -import java.util.Date +import java.util.* plugins { - kotlin("multiplatform") version "1.3.70" + kotlin("multiplatform") version "1.3.72" id("com.jfrog.bintray") version "1.8.4" id("org.jetbrains.dokka") version "0.10.0" `maven-publish` @@ -67,6 +67,7 @@ kotlin { } } } + linuxX64("linuxX64") sourceSets { commonMain { dependencies { diff --git a/src/linuxX64Main/kotlin/mu/Appender.kt b/src/linuxX64Main/kotlin/mu/Appender.kt new file mode 100644 index 00000000..9a1947b0 --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/Appender.kt @@ -0,0 +1,9 @@ +package mu + +interface Appender { + fun trace(message: Any?) + fun debug(message: Any?) + fun info(message: Any?) + fun warn(message: Any?) + fun error(message: Any?) +} diff --git a/src/linuxX64Main/kotlin/mu/ConsoleOutputAppender.kt b/src/linuxX64Main/kotlin/mu/ConsoleOutputAppender.kt new file mode 100644 index 00000000..b3b004c0 --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/ConsoleOutputAppender.kt @@ -0,0 +1,15 @@ +package mu + +import platform.posix.fprintf +import platform.posix.stderr + +object ConsoleOutputAppender : Appender { + override fun trace(message: Any?) = println(message) + override fun debug(message: Any?) = println(message) + override fun info(message: Any?) = println(message) + override fun warn(message: Any?) = println(message) + + override fun error(message: Any?) { + fprintf(stderr, "$message\n") + } +} diff --git a/src/linuxX64Main/kotlin/mu/DefaultMessageFormatter.kt b/src/linuxX64Main/kotlin/mu/DefaultMessageFormatter.kt new file mode 100644 index 00000000..35de1cbe --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/DefaultMessageFormatter.kt @@ -0,0 +1,36 @@ +package mu + +import mu.internal.toStringSafe + +object DefaultMessageFormatter : Formatter { + override fun formatMessage(level: KotlinLoggingLevel, loggerName: String, msg: () -> Any?) = + "${level.name}: [$loggerName] ${msg.toStringSafe()}" + + override fun formatMessage(level: KotlinLoggingLevel, loggerName: String, t: Throwable?, msg: () -> Any?) = + "${level.name}: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}" + + override fun formatMessage(level: KotlinLoggingLevel, loggerName: String, marker: Marker?, msg: () -> Any?) = + "${level.name}: [$loggerName] ${marker?.getName()} ${msg.toStringSafe()}" + + override fun formatMessage( + level: KotlinLoggingLevel, + loggerName: String, + marker: Marker?, + t: Throwable?, + msg: () -> Any? + ) = + "${level.name}: [$loggerName] ${marker?.getName()} ${msg.toStringSafe()}${t.throwableToString()}" + + private fun Throwable?.throwableToString(): String { + if (this == null) { + return "" + } + var msg = "" + var current = this + while (current != null && current.cause != current) { + msg += ", Caused by: '${current.message}'" + current = current.cause + } + return msg + } +} diff --git a/src/linuxX64Main/kotlin/mu/Formatter.kt b/src/linuxX64Main/kotlin/mu/Formatter.kt new file mode 100644 index 00000000..55489f19 --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/Formatter.kt @@ -0,0 +1,14 @@ +package mu + +interface Formatter { + fun formatMessage(level: KotlinLoggingLevel, loggerName: String, msg: () -> Any?): Any? + fun formatMessage(level: KotlinLoggingLevel, loggerName: String, t: Throwable?, msg: () -> Any?): Any? + fun formatMessage(level: KotlinLoggingLevel, loggerName: String, marker: Marker?, msg: () -> Any?): Any? + fun formatMessage( + level: KotlinLoggingLevel, + loggerName: String, + marker: Marker?, + t: Throwable?, + msg: () -> Any? + ): Any? +} diff --git a/src/linuxX64Main/kotlin/mu/KLogger.kt b/src/linuxX64Main/kotlin/mu/KLogger.kt new file mode 100644 index 00000000..7ea915a6 --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/KLogger.kt @@ -0,0 +1,129 @@ +package mu + +actual interface KLogger { + + /** + * Lazy add a log message if isTraceEnabled is true + */ + actual fun trace(msg: () -> Any?) + + /** + * Lazy add a log message if isDebugEnabled is true + */ + actual fun debug(msg: () -> Any?) + + /** + * Lazy add a log message if isInfoEnabled is true + */ + actual fun info(msg: () -> Any?) + + /** + * Lazy add a log message if isWarnEnabled is true + */ + actual fun warn(msg: () -> Any?) + + /** + * Lazy add a log message if isErrorEnabled is true + */ + actual fun error(msg: () -> Any?) + + /** + * Lazy add a log message with throwable payload if isTraceEnabled is true + */ + actual fun trace(t: Throwable?, msg: () -> Any?) + + /** + * Lazy add a log message with throwable payload if isDebugEnabled is true + */ + actual fun debug(t: Throwable?, msg: () -> Any?) + + /** + * Lazy add a log message with throwable payload if isInfoEnabled is true + */ + actual fun info(t: Throwable?, msg: () -> Any?) + + /** + * Lazy add a log message with throwable payload if isWarnEnabled is true + */ + actual fun warn(t: Throwable?, msg: () -> Any?) + + /** + * Lazy add a log message with throwable payload if isErrorEnabled is true + */ + actual fun error(t: Throwable?, msg: () -> Any?) + + /** + * Lazy add a log message if isTraceEnabled is true + */ + actual fun trace(marker: Marker?, msg: () -> Any?) + + /** + * Lazy add a log message if isDebugEnabled is true + */ + actual fun debug(marker: Marker?, msg: () -> Any?) + + /** + * Lazy add a log message if isInfoEnabled is true + */ + actual fun info(marker: Marker?, msg: () -> Any?) + + /** + * Lazy add a log message if isWarnEnabled is true + */ + actual fun warn(marker: Marker?, msg: () -> Any?) + + /** + * Lazy add a log message if isErrorEnabled is true + */ + actual fun error(marker: Marker?, msg: () -> Any?) + + /** + * Lazy add a log message with throwable payload if isTraceEnabled is true + */ + actual fun trace(marker: Marker?, t: Throwable?, msg: () -> Any?) + + /** + * Lazy add a log message with throwable payload if isDebugEnabled is true + */ + actual fun debug(marker: Marker?, t: Throwable?, msg: () -> Any?) + + /** + * Lazy add a log message with throwable payload if isInfoEnabled is true + */ + actual fun info(marker: Marker?, t: Throwable?, msg: () -> Any?) + + /** + * Lazy add a log message with throwable payload if isWarnEnabled is true + */ + actual fun warn(marker: Marker?, t: Throwable?, msg: () -> Any?) + + /** + * Lazy add a log message with throwable payload if isErrorEnabled is true + */ + actual fun error(marker: Marker?, t: Throwable?, msg: () -> Any?) + + /** + * Add a log message with all the supplied parameters along with method name + */ + actual fun entry(vararg argArray: Any?) + + /** + * Add log message indicating exit of a method + */ + actual fun exit() + + /** + * Add a log message with the return value of a method + */ + actual fun exit(result: T): T where T : Any? + + /** + * Add a log message indicating an exception will be thrown along with the stack trace. + */ + actual fun throwing(throwable: T): T where T : Throwable + + /** + * Add a log message indicating an exception is caught along with the stack trace. + */ + actual fun catching(throwable: T) where T : Throwable +} diff --git a/src/linuxX64Main/kotlin/mu/KMarkerFactory.kt b/src/linuxX64Main/kotlin/mu/KMarkerFactory.kt new file mode 100644 index 00000000..037eacea --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/KMarkerFactory.kt @@ -0,0 +1,8 @@ +package mu + +import mu.internal.MarkerLinux + +actual object KMarkerFactory { + + actual fun getMarker(name: String): Marker = MarkerLinux(name) +} diff --git a/src/linuxX64Main/kotlin/mu/KotlinLogging.kt b/src/linuxX64Main/kotlin/mu/KotlinLogging.kt new file mode 100644 index 00000000..b249433c --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/KotlinLogging.kt @@ -0,0 +1,16 @@ +package mu + +import mu.internal.KLoggerLinux + + +actual object KotlinLogging { + /** + * This method allow defining the logger in a file in the following way: + * ``` + * val logger = KotlinLogging.logger {} + * ``` + */ + actual fun logger(func: () -> Unit): KLogger = KLoggerLinux(func::class.qualifiedName ?: "") + + actual fun logger(name: String): KLogger = KLoggerLinux(name) +} diff --git a/src/linuxX64Main/kotlin/mu/KotlinLoggingConfiguration.kt b/src/linuxX64Main/kotlin/mu/KotlinLoggingConfiguration.kt new file mode 100644 index 00000000..4eb1da13 --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/KotlinLoggingConfiguration.kt @@ -0,0 +1,25 @@ +package mu + +import kotlin.native.concurrent.AtomicReference + +@Suppress("ObjectPropertyName") +object KotlinLoggingConfiguration { + private val _logLevel = AtomicReference(KotlinLoggingLevel.INFO) + var logLevel: KotlinLoggingLevel + get() = _logLevel.value + set(value) { + _logLevel.value = value + } + private val _appender = AtomicReference(ConsoleOutputAppender) + var appender: Appender + get() = _appender.value + set(value) { + _appender.value = value + } + private val _formatter = AtomicReference(DefaultMessageFormatter) + var formatter: Formatter + get() = _formatter.value + set(value) { + _formatter.value = value + } +} diff --git a/src/linuxX64Main/kotlin/mu/KotlinLoggingLevel.kt b/src/linuxX64Main/kotlin/mu/KotlinLoggingLevel.kt new file mode 100644 index 00000000..e70713e2 --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/KotlinLoggingLevel.kt @@ -0,0 +1,13 @@ +package mu + +import mu.KotlinLoggingConfiguration.logLevel + +enum class KotlinLoggingLevel { + TRACE, + DEBUG, + INFO, + WARN, + ERROR +} + +fun KotlinLoggingLevel.isLoggingEnabled() = this.ordinal >= logLevel.ordinal diff --git a/src/linuxX64Main/kotlin/mu/Marker.kt b/src/linuxX64Main/kotlin/mu/Marker.kt new file mode 100644 index 00000000..eca0daf6 --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/Marker.kt @@ -0,0 +1,6 @@ +package mu + +actual interface Marker { + + actual fun getName(): String +} diff --git a/src/linuxX64Main/kotlin/mu/internal/KLoggerLinux.kt b/src/linuxX64Main/kotlin/mu/internal/KLoggerLinux.kt new file mode 100644 index 00000000..2b38668d --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/internal/KLoggerLinux.kt @@ -0,0 +1,114 @@ +package mu.internal + +import mu.KLogger +import mu.KotlinLoggingConfiguration.appender +import mu.KotlinLoggingConfiguration.formatter +import mu.KotlinLoggingLevel +import mu.KotlinLoggingLevel.DEBUG +import mu.KotlinLoggingLevel.ERROR +import mu.KotlinLoggingLevel.INFO +import mu.KotlinLoggingLevel.TRACE +import mu.KotlinLoggingLevel.WARN +import mu.Marker +import mu.isLoggingEnabled + +internal class KLoggerLinux( + private val loggerName: String +) : KLogger { + + override fun trace(msg: () -> Any?) = TRACE.logIfEnabled(msg, appender::trace) + + override fun debug(msg: () -> Any?) = DEBUG.logIfEnabled(msg, appender::debug) + + override fun info(msg: () -> Any?) = INFO.logIfEnabled(msg, appender::info) + + override fun warn(msg: () -> Any?) = WARN.logIfEnabled(msg, appender::warn) + + override fun error(msg: () -> Any?) = ERROR.logIfEnabled(msg, appender::error) + + override fun trace(t: Throwable?, msg: () -> Any?) = TRACE.logIfEnabled(msg, t, appender::trace) + + override fun debug(t: Throwable?, msg: () -> Any?) = DEBUG.logIfEnabled(msg, t, appender::debug) + + override fun info(t: Throwable?, msg: () -> Any?) = INFO.logIfEnabled(msg, t, appender::info) + + override fun warn(t: Throwable?, msg: () -> Any?) = WARN.logIfEnabled(msg, t, appender::warn) + + override fun error(t: Throwable?, msg: () -> Any?) = ERROR.logIfEnabled(msg, t, appender::error) + + override fun trace(marker: Marker?, msg: () -> Any?) = TRACE.logIfEnabled(marker, msg, appender::trace) + + override fun debug(marker: Marker?, msg: () -> Any?) = DEBUG.logIfEnabled(marker, msg, appender::debug) + + override fun info(marker: Marker?, msg: () -> Any?) = INFO.logIfEnabled(marker, msg, appender::info) + + override fun warn(marker: Marker?, msg: () -> Any?) = WARN.logIfEnabled(marker, msg, appender::warn) + + override fun error(marker: Marker?, msg: () -> Any?) = ERROR.logIfEnabled(marker, msg, appender::error) + + override fun trace(marker: Marker?, t: Throwable?, msg: () -> Any?) = + TRACE.logIfEnabled(marker, msg, t, appender::trace) + + override fun debug(marker: Marker?, t: Throwable?, msg: () -> Any?) = + DEBUG.logIfEnabled(marker, msg, t, appender::debug) + + override fun info(marker: Marker?, t: Throwable?, msg: () -> Any?) = + INFO.logIfEnabled(marker, msg, t, appender::info) + + override fun warn(marker: Marker?, t: Throwable?, msg: () -> Any?) = + WARN.logIfEnabled(marker, msg, t, appender::warn) + + override fun error(marker: Marker?, t: Throwable?, msg: () -> Any?) = + ERROR.logIfEnabled(marker, msg, t, appender::error) + + private fun KotlinLoggingLevel.logIfEnabled(msg: () -> Any?, logFunction: (Any?) -> Unit) { + if (isLoggingEnabled()) { + logFunction(formatter.formatMessage(this, loggerName, msg)) + } + } + + private fun KotlinLoggingLevel.logIfEnabled(msg: () -> Any?, t: Throwable?, logFunction: (Any?) -> Unit) { + if (isLoggingEnabled()) { + logFunction(formatter.formatMessage(this, loggerName, t, msg)) + } + } + + private fun KotlinLoggingLevel.logIfEnabled(marker: Marker?, msg: () -> Any?, logFunction: (Any?) -> Unit) { + if (isLoggingEnabled()) { + logFunction(formatter.formatMessage(this, loggerName, marker, msg)) + } + } + + private fun KotlinLoggingLevel.logIfEnabled( + marker: Marker?, + msg: () -> Any?, + t: Throwable?, + logFunction: (Any?) -> Unit + ) { + if (isLoggingEnabled()) { + logFunction(formatter.formatMessage(this, loggerName, marker, t, msg)) + } + } + + override fun entry(vararg argArray: Any?) { + TRACE.logIfEnabled({ "entry($argArray)" }, appender::trace) + } + + override fun exit() { + TRACE.logIfEnabled({ "exit()" }, appender::trace) + } + + override fun exit(result: T): T { + TRACE.logIfEnabled({ "exit($result)" }, appender::trace) + return result + } + + override fun throwing(throwable: T): T { + ERROR.logIfEnabled({ "throwing($throwable" }, throwable, appender::error) + return throwable + } + + override fun catching(throwable: T) { + ERROR.logIfEnabled({ "catching($throwable" }, throwable, appender::error) + } +} diff --git a/src/linuxX64Main/kotlin/mu/internal/MarkerLinux.kt b/src/linuxX64Main/kotlin/mu/internal/MarkerLinux.kt new file mode 100644 index 00000000..0825812c --- /dev/null +++ b/src/linuxX64Main/kotlin/mu/internal/MarkerLinux.kt @@ -0,0 +1,8 @@ +package mu.internal + +import mu.Marker + +internal class MarkerLinux(private val name: String) : Marker { + + override fun getName(): String = this.name +}