Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OSLogAppender to append logs to os_log on Darwin platforms
- Loading branch information
Showing
11 changed files
with
142 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package mu | ||
|
||
public actual val DefaultAppender: Appender = OSLogAppender() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package mu | ||
|
||
import kotlinx.cinterop.ptr | ||
import platform.darwin.OS_LOG_DEFAULT | ||
import platform.darwin.OS_LOG_TYPE_DEBUG | ||
import platform.darwin.OS_LOG_TYPE_DEFAULT | ||
import platform.darwin.OS_LOG_TYPE_ERROR | ||
import platform.darwin.OS_LOG_TYPE_INFO | ||
import platform.darwin.__dso_handle | ||
import platform.darwin._os_log_internal | ||
import platform.darwin.os_log_t | ||
import platform.darwin.os_log_type_enabled | ||
import platform.darwin.os_log_type_t | ||
import kotlin.native.concurrent.AtomicReference | ||
|
||
public open class OSLogAppender: Appender { | ||
override val includePrefix: Boolean = true | ||
|
||
protected open fun logger(loggerName: String): os_log_t { | ||
return OS_LOG_DEFAULT | ||
} | ||
|
||
private fun log(level: os_log_type_t, loggerName: String, message: String) { | ||
val logger = logger(loggerName) | ||
if (os_log_type_enabled(logger, level)) { | ||
_os_log_internal(__dso_handle.ptr, logger, level, message) | ||
} | ||
} | ||
|
||
override fun trace(loggerName: String, message: String) { | ||
log(OS_LOG_TYPE_DEBUG, loggerName, message) | ||
} | ||
|
||
override fun debug(loggerName: String, message: String) { | ||
log(OS_LOG_TYPE_DEBUG, loggerName, message) | ||
} | ||
|
||
override fun info(loggerName: String, message: String) { | ||
log(OS_LOG_TYPE_INFO, loggerName, message) | ||
} | ||
|
||
override fun warn(loggerName: String, message: String) { | ||
log(OS_LOG_TYPE_DEFAULT, loggerName, message) | ||
} | ||
|
||
override fun error(loggerName: String, message: String) { | ||
log(OS_LOG_TYPE_ERROR, loggerName, message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package mu | ||
|
||
import platform.darwin.os_log_create | ||
import platform.darwin.os_log_t | ||
import kotlin.native.concurrent.AtomicReference | ||
|
||
public class OSLogSubsystemAppender(public val subsystem: String) : OSLogAppender() { | ||
override val includePrefix: Boolean = false | ||
|
||
private val logs: AtomicReference<Map<String, os_log_t>> = AtomicReference(mapOf()) | ||
|
||
override fun logger(loggerName: String): os_log_t { | ||
var logger: os_log_t | ||
do { | ||
val existing = logs.value | ||
logger = existing[loggerName] | ||
if (logger != null) { | ||
return logger | ||
} | ||
|
||
val updated = existing.toMutableMap() | ||
logger = os_log_create(subsystem, loggerName) | ||
updated[loggerName] = logger | ||
} while (!logs.compareAndSet(existing, updated)) | ||
|
||
return logger | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package mu | ||
|
||
public actual val DefaultAppender: Appender = ConsoleOutputAppender |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters