Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Linux X64 Support #119

Merged
merged 10 commits into from
Jul 14, 2020
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -67,6 +67,7 @@ kotlin {
}
}
}
linuxX64("linuxX64")
sourceSets {
commonMain {
dependencies {
Expand Down
9 changes: 9 additions & 0 deletions src/linuxX64Main/kotlin/mu/Appender.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mu

interface Appender {
napperley marked this conversation as resolved.
Show resolved Hide resolved
fun trace(message: Any?)
fun debug(message: Any?)
fun info(message: Any?)
fun warn(message: Any?)
fun error(message: Any?)
}
15 changes: 15 additions & 0 deletions src/linuxX64Main/kotlin/mu/ConsoleOutputAppender.kt
Original file line number Diff line number Diff line change
@@ -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)
napperley marked this conversation as resolved.
Show resolved Hide resolved

override fun error(message: Any?) {
fprintf(stderr, "$message\n")
}
}
36 changes: 36 additions & 0 deletions src/linuxX64Main/kotlin/mu/DefaultMessageFormatter.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
14 changes: 14 additions & 0 deletions src/linuxX64Main/kotlin/mu/Formatter.kt
Original file line number Diff line number Diff line change
@@ -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?
}
129 changes: 129 additions & 0 deletions src/linuxX64Main/kotlin/mu/KLogger.kt
Original file line number Diff line number Diff line change
@@ -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 <T> exit(result: T): T where T : Any?

/**
* Add a log message indicating an exception will be thrown along with the stack trace.
*/
actual fun <T> throwing(throwable: T): T where T : Throwable

/**
* Add a log message indicating an exception is caught along with the stack trace.
*/
actual fun <T> catching(throwable: T) where T : Throwable
}
8 changes: 8 additions & 0 deletions src/linuxX64Main/kotlin/mu/KMarkerFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mu

import mu.internal.MarkerLinux

actual object KMarkerFactory {

actual fun getMarker(name: String): Marker = MarkerLinux(name)
}
16 changes: 16 additions & 0 deletions src/linuxX64Main/kotlin/mu/KotlinLogging.kt
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions src/linuxX64Main/kotlin/mu/KotlinLoggingConfiguration.kt
Original file line number Diff line number Diff line change
@@ -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<Appender>(ConsoleOutputAppender)
var appender: Appender
get() = _appender.value
set(value) {
_appender.value = value
}
private val _formatter = AtomicReference<Formatter>(DefaultMessageFormatter)
var formatter: Formatter
get() = _formatter.value
set(value) {
_formatter.value = value
}
}
13 changes: 13 additions & 0 deletions src/linuxX64Main/kotlin/mu/KotlinLoggingLevel.kt
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions src/linuxX64Main/kotlin/mu/Marker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package mu

actual interface Marker {

actual fun getName(): String
}
Loading