-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
MosaicLogger
for output logs to another terminal or file
Solution for #494
- Loading branch information
Showing
10 changed files
with
204 additions
and
0 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
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 |
---|---|---|
|
@@ -38,6 +38,17 @@ final enum class com.jakewharton.mosaic.ui/AnsiLevel : kotlin/Enum<com.jakewhart | |
final fun values(): kotlin/Array<com.jakewharton.mosaic.ui/AnsiLevel> // com.jakewharton.mosaic.ui/AnsiLevel.values|values#static(){}[0] | ||
} | ||
|
||
final enum class com.jakewharton.mosaic/MosaicLogLevel : kotlin/Enum<com.jakewharton.mosaic/MosaicLogLevel> { // com.jakewharton.mosaic/MosaicLogLevel|null[0] | ||
enum entry DEBUG // com.jakewharton.mosaic/MosaicLogLevel.DEBUG|null[0] | ||
enum entry VERBOSE // com.jakewharton.mosaic/MosaicLogLevel.VERBOSE|null[0] | ||
|
||
final val entries // com.jakewharton.mosaic/MosaicLogLevel.entries|#static{}entries[0] | ||
final fun <get-entries>(): kotlin.enums/EnumEntries<com.jakewharton.mosaic/MosaicLogLevel> // com.jakewharton.mosaic/MosaicLogLevel.entries.<get-entries>|<get-entries>#static(){}[0] | ||
|
||
final fun valueOf(kotlin/String): com.jakewharton.mosaic/MosaicLogLevel // com.jakewharton.mosaic/MosaicLogLevel.valueOf|valueOf#static(kotlin.String){}[0] | ||
final fun values(): kotlin/Array<com.jakewharton.mosaic/MosaicLogLevel> // com.jakewharton.mosaic/MosaicLogLevel.values|values#static(){}[0] | ||
} | ||
|
||
abstract fun interface com.jakewharton.mosaic.layout/MeasurePolicy { // com.jakewharton.mosaic.layout/MeasurePolicy|null[0] | ||
abstract fun (com.jakewharton.mosaic.layout/MeasureScope).measure(kotlin.collections/List<com.jakewharton.mosaic.layout/Measurable>, com.jakewharton.mosaic.ui.unit/Constraints): com.jakewharton.mosaic.layout/MeasureResult // com.jakewharton.mosaic.layout/MeasurePolicy.measure|[email protected](kotlin.collections.List<com.jakewharton.mosaic.layout.Measurable>;com.jakewharton.mosaic.ui.unit.Constraints){}[0] | ||
open fun maxIntrinsicHeight(kotlin.collections/List<com.jakewharton.mosaic.layout/IntrinsicMeasurable>, kotlin/Int): kotlin/Int // com.jakewharton.mosaic.layout/MeasurePolicy.maxIntrinsicHeight|maxIntrinsicHeight(kotlin.collections.List<com.jakewharton.mosaic.layout.IntrinsicMeasurable>;kotlin.Int){}[0] | ||
|
@@ -209,6 +220,11 @@ abstract interface com.jakewharton.mosaic/Mosaic { // com.jakewharton.mosaic/Mos | |
abstract suspend fun awaitComplete() // com.jakewharton.mosaic/Mosaic.awaitComplete|awaitComplete(){}[0] | ||
} | ||
|
||
abstract interface com.jakewharton.mosaic/MosaicLogWriter { // com.jakewharton.mosaic/MosaicLogWriter|null[0] | ||
abstract fun close() // com.jakewharton.mosaic/MosaicLogWriter.close|close(){}[0] | ||
abstract fun writeLine(kotlin/String) // com.jakewharton.mosaic/MosaicLogWriter.writeLine|writeLine(kotlin.String){}[0] | ||
} | ||
|
||
abstract interface com.jakewharton.mosaic/TextCanvas { // com.jakewharton.mosaic/TextCanvas|null[0] | ||
abstract val height // com.jakewharton.mosaic/TextCanvas.height|{}height[0] | ||
abstract fun <get-height>(): kotlin/Int // com.jakewharton.mosaic/TextCanvas.height.<get-height>|<get-height>(){}[0] | ||
|
@@ -625,6 +641,19 @@ final object com.jakewharton.mosaic.ui/Arrangement { // com.jakewharton.mosaic.u | |
} | ||
} | ||
|
||
final object com.jakewharton.mosaic/MosaicLogger { // com.jakewharton.mosaic/MosaicLogger|null[0] | ||
final var logLevel // com.jakewharton.mosaic/MosaicLogger.logLevel|{}logLevel[0] | ||
final fun <get-logLevel>(): com.jakewharton.mosaic/MosaicLogLevel // com.jakewharton.mosaic/MosaicLogger.logLevel.<get-logLevel>|<get-logLevel>(){}[0] | ||
final fun <set-logLevel>(com.jakewharton.mosaic/MosaicLogLevel) // com.jakewharton.mosaic/MosaicLogger.logLevel.<set-logLevel>|<set-logLevel>(com.jakewharton.mosaic.MosaicLogLevel){}[0] | ||
final var logWriter // com.jakewharton.mosaic/MosaicLogger.logWriter|{}logWriter[0] | ||
final fun <get-logWriter>(): com.jakewharton.mosaic/MosaicLogWriter? // com.jakewharton.mosaic/MosaicLogger.logWriter.<get-logWriter>|<get-logWriter>(){}[0] | ||
final fun <set-logWriter>(com.jakewharton.mosaic/MosaicLogWriter?) // com.jakewharton.mosaic/MosaicLogger.logWriter.<set-logWriter>|<set-logWriter>(com.jakewharton.mosaic.MosaicLogWriter?){}[0] | ||
|
||
final inline fun d(kotlin/Function0<kotlin/String>) // com.jakewharton.mosaic/MosaicLogger.d|d(kotlin.Function0<kotlin.String>){}[0] | ||
final inline fun log(com.jakewharton.mosaic/MosaicLogLevel, kotlin/Function0<kotlin/String>) // com.jakewharton.mosaic/MosaicLogger.log|log(com.jakewharton.mosaic.MosaicLogLevel;kotlin.Function0<kotlin.String>){}[0] | ||
final inline fun v(kotlin/Function0<kotlin/String>) // com.jakewharton.mosaic/MosaicLogger.v|v(kotlin.Function0<kotlin.String>){}[0] | ||
} | ||
|
||
final const val com.jakewharton.mosaic.ui/EmptyTextStyle // com.jakewharton.mosaic.ui/EmptyTextStyle|{}EmptyTextStyle[0] | ||
final fun <get-EmptyTextStyle>(): kotlin/Int // com.jakewharton.mosaic.ui/EmptyTextStyle.<get-EmptyTextStyle>|<get-EmptyTextStyle>(){}[0] | ||
final const val com.jakewharton.mosaic.ui/UnspecifiedColor // com.jakewharton.mosaic.ui/UnspecifiedColor|{}UnspecifiedColor[0] | ||
|
@@ -686,6 +715,7 @@ final val com.jakewharton.mosaic/com_jakewharton_mosaic_AnsiRendering$stableprop | |
final val com.jakewharton.mosaic/com_jakewharton_mosaic_DebugRendering$stableprop // com.jakewharton.mosaic/com_jakewharton_mosaic_DebugRendering$stableprop|#static{}com_jakewharton_mosaic_DebugRendering$stableprop[0] | ||
final val com.jakewharton.mosaic/com_jakewharton_mosaic_GlobalSnapshotManager$stableprop // com.jakewharton.mosaic/com_jakewharton_mosaic_GlobalSnapshotManager$stableprop|#static{}com_jakewharton_mosaic_GlobalSnapshotManager$stableprop[0] | ||
final val com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicComposition$stableprop // com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicComposition$stableprop|#static{}com_jakewharton_mosaic_MosaicComposition$stableprop[0] | ||
final val com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicLogger$stableprop // com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicLogger$stableprop|#static{}com_jakewharton_mosaic_MosaicLogger$stableprop[0] | ||
final val com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicNodeApplier$stableprop // com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicNodeApplier$stableprop|#static{}com_jakewharton_mosaic_MosaicNodeApplier$stableprop[0] | ||
final val com.jakewharton.mosaic/com_jakewharton_mosaic_Terminal$stableprop // com.jakewharton.mosaic/com_jakewharton_mosaic_Terminal$stableprop|#static{}com_jakewharton_mosaic_Terminal$stableprop[0] | ||
final val com.jakewharton.mosaic/com_jakewharton_mosaic_TextPixel$stableprop // com.jakewharton.mosaic/com_jakewharton_mosaic_TextPixel$stableprop|#static{}com_jakewharton_mosaic_TextPixel$stableprop[0] | ||
|
@@ -793,6 +823,7 @@ final fun com.jakewharton.mosaic/com_jakewharton_mosaic_AnsiRendering$stableprop | |
final fun com.jakewharton.mosaic/com_jakewharton_mosaic_DebugRendering$stableprop_getter(): kotlin/Int // com.jakewharton.mosaic/com_jakewharton_mosaic_DebugRendering$stableprop_getter|com_jakewharton_mosaic_DebugRendering$stableprop_getter(){}[0] | ||
final fun com.jakewharton.mosaic/com_jakewharton_mosaic_GlobalSnapshotManager$stableprop_getter(): kotlin/Int // com.jakewharton.mosaic/com_jakewharton_mosaic_GlobalSnapshotManager$stableprop_getter|com_jakewharton_mosaic_GlobalSnapshotManager$stableprop_getter(){}[0] | ||
final fun com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicComposition$stableprop_getter(): kotlin/Int // com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicComposition$stableprop_getter|com_jakewharton_mosaic_MosaicComposition$stableprop_getter(){}[0] | ||
final fun com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicLogger$stableprop_getter(): kotlin/Int // com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicLogger$stableprop_getter|com_jakewharton_mosaic_MosaicLogger$stableprop_getter(){}[0] | ||
final fun com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicNodeApplier$stableprop_getter(): kotlin/Int // com.jakewharton.mosaic/com_jakewharton_mosaic_MosaicNodeApplier$stableprop_getter|com_jakewharton_mosaic_MosaicNodeApplier$stableprop_getter(){}[0] | ||
final fun com.jakewharton.mosaic/com_jakewharton_mosaic_Terminal$stableprop_getter(): kotlin/Int // com.jakewharton.mosaic/com_jakewharton_mosaic_Terminal$stableprop_getter|com_jakewharton_mosaic_Terminal$stableprop_getter(){}[0] | ||
final fun com.jakewharton.mosaic/com_jakewharton_mosaic_TextPixel$stableprop_getter(): kotlin/Int // com.jakewharton.mosaic/com_jakewharton_mosaic_TextPixel$stableprop_getter|com_jakewharton_mosaic_TextPixel$stableprop_getter(){}[0] | ||
|
66 changes: 66 additions & 0 deletions
66
mosaic-runtime/src/commonMain/kotlin/com/jakewharton/mosaic/logging.kt
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,66 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import com.github.ajalt.mordant.platform.MultiplatformSystem | ||
import kotlin.jvm.JvmField | ||
|
||
private const val MOSAIC_LOG_PATH = "MOSAIC_LOG_PATH" | ||
private const val MOSAIC_LOG_LEVEL = "MOSAIC_LOG_LEVEL" | ||
|
||
public enum class MosaicLogLevel(internal val envVarValue: String) { | ||
VERBOSE(envVarValue = "verbose"), | ||
DEBUG(envVarValue = "debug"), | ||
} | ||
|
||
public object MosaicLogger { | ||
|
||
@JvmField | ||
@PublishedApi | ||
internal var logWriter: MosaicLogWriter? = null | ||
|
||
@JvmField | ||
@PublishedApi | ||
internal var logLevel: MosaicLogLevel = MosaicLogLevel.DEBUG | ||
|
||
internal fun init() { | ||
logWriter = getDefaultLogWriter() | ||
logLevel = getDefaultLogLevel() | ||
} | ||
|
||
private fun getDefaultLogWriter(): MosaicLogWriter? { | ||
val logPath = MultiplatformSystem.readEnvironmentVariable(MOSAIC_LOG_PATH) | ||
if (logPath.isNullOrEmpty()) return null | ||
return createDefaultLogWriter(logPath) | ||
} | ||
|
||
private fun getDefaultLogLevel(): MosaicLogLevel { | ||
val logLevelValue = MultiplatformSystem.readEnvironmentVariable(MOSAIC_LOG_LEVEL) | ||
return MosaicLogLevel.entries.find { it.envVarValue == logLevelValue } | ||
?: MosaicLogLevel.DEBUG | ||
} | ||
|
||
internal fun finalize() { | ||
logWriter?.let { | ||
logWriter = null | ||
it.close() | ||
} | ||
} | ||
|
||
public inline fun log(level: MosaicLogLevel, message: () -> String) { | ||
val logWriter = logWriter | ||
if (logWriter != null && level >= logLevel) { | ||
logWriter.writeLine(message()) | ||
} | ||
} | ||
|
||
public inline fun v(message: () -> String): Unit = log(MosaicLogLevel.VERBOSE, message) | ||
|
||
public inline fun d(message: () -> String): Unit = log(MosaicLogLevel.DEBUG, message) | ||
} | ||
|
||
@PublishedApi | ||
internal interface MosaicLogWriter { | ||
fun writeLine(message: String) | ||
fun close() | ||
} | ||
|
||
internal expect fun createDefaultLogWriter(logPath: String): MosaicLogWriter? |
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
25 changes: 25 additions & 0 deletions
25
mosaic-runtime/src/jvmMain/kotlin/com/jakewharton/mosaic/logging.jvm.kt
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,25 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import java.io.Writer | ||
import java.nio.file.StandardOpenOption | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.writer | ||
|
||
internal actual fun createDefaultLogWriter(logPath: String): MosaicLogWriter? { | ||
return object : MosaicLogWriter { | ||
|
||
private val writer: Writer = Path(logPath).writer( | ||
options = arrayOf(StandardOpenOption.WRITE, StandardOpenOption.APPEND), | ||
) | ||
|
||
override fun writeLine(message: String) { | ||
writer.write(message) | ||
writer.write("\n") | ||
writer.flush() | ||
} | ||
|
||
override fun close() { | ||
writer.close() | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
mosaic-runtime/src/linuxMain/kotlin/com/jakewharton/mosaic/logging.linux.kt
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,10 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.cstr | ||
import platform.posix.write | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal actual inline fun write(fileDescriptor: Int, str: String) { | ||
write(fileDescriptor, str.cstr, str.length.toULong()) | ||
} |
10 changes: 10 additions & 0 deletions
10
mosaic-runtime/src/macosMain/kotlin/com/jakewharton/mosaic/logging.macos.kt
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,10 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.cstr | ||
import platform.posix.write | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal actual inline fun write(fileDescriptor: Int, str: String) { | ||
write(fileDescriptor, str.cstr, str.length.toULong()) | ||
} |
10 changes: 10 additions & 0 deletions
10
mosaic-runtime/src/mingwMain/kotlin/com/jakewharton/mosaic/logging.mingw.kt
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,10 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.cstr | ||
import platform.posix.write | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal actual inline fun write(fileDescriptor: Int, str: String) { | ||
write(fileDescriptor, str.cstr, str.length.toUInt()) | ||
} |
26 changes: 26 additions & 0 deletions
26
mosaic-runtime/src/nativeMain/kotlin/com/jakewharton/mosaic/logging.native.kt
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,26 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import platform.posix.O_APPEND | ||
import platform.posix.O_WRONLY | ||
import platform.posix.close | ||
import platform.posix.open | ||
|
||
internal actual fun createDefaultLogWriter(logPath: String): MosaicLogWriter? { | ||
val fileDescriptor = open(logPath, O_WRONLY or O_APPEND) | ||
if (fileDescriptor < 0) { | ||
return null | ||
} | ||
return object : MosaicLogWriter { | ||
|
||
override fun writeLine(message: String) { | ||
write(fileDescriptor, message) | ||
write(fileDescriptor, "\n") | ||
} | ||
|
||
override fun close() { | ||
close(fileDescriptor) | ||
} | ||
} | ||
} | ||
|
||
internal expect inline fun write(fileDescriptor: Int, str: String) |