Skip to content

Commit

Permalink
Addressing comments:
Browse files Browse the repository at this point in the history
- Renaming tryInitLogFromEnvOr to initLogFromEnvOr
- Adding Zenoh.initLogFromEnvOr("error") to all the examples
  • Loading branch information
DariusIMP committed Sep 13, 2024
1 parent 1f1baee commit 5b51f5f
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 27 deletions.
2 changes: 1 addition & 1 deletion examples/src/main/kotlin/io.zenoh/ZDelete.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ZDelete(private val emptyArgs: Boolean) : CliktCommand(
override fun run() {
val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode)

Zenoh.tryInitLogFromEnv()
Zenoh.initLogFromEnvOr("error")

println("Opening session...")
Zenoh.open(config).onSuccess { session ->
Expand Down
2 changes: 1 addition & 1 deletion examples/src/main/kotlin/io.zenoh/ZGet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ZGet(private val emptyArgs: Boolean) : CliktCommand(
override fun run() {
val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode)

Zenoh.tryInitLogFromEnv()
Zenoh.initLogFromEnvOr("error")

Zenoh.open(config).onSuccess { session ->
session.use {
Expand Down
2 changes: 1 addition & 1 deletion examples/src/main/kotlin/io.zenoh/ZPub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ZPub(private val emptyArgs: Boolean) : CliktCommand(
override fun run() {
val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode)

Zenoh.tryInitLogFromEnv()
Zenoh.initLogFromEnvOr("error")

println("Opening session...")
Zenoh.open(config).onSuccess { session ->
Expand Down
2 changes: 2 additions & 0 deletions examples/src/main/kotlin/io.zenoh/ZPubThr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class ZPubThr(private val emptyArgs: Boolean) : CliktCommand(
) {

override fun run() {
Zenoh.initLogFromEnvOr("error")

val data = ByteArray(payloadSize)
for (i in 0..<payloadSize) {
data[i] = (i % 10).toByte()
Expand Down
2 changes: 1 addition & 1 deletion examples/src/main/kotlin/io.zenoh/ZPut.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ZPut(private val emptyArgs: Boolean) : CliktCommand(
override fun run() {
val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode)

Zenoh.tryInitLogFromEnv()
Zenoh.initLogFromEnvOr("error")

println("Opening Session...")
Zenoh.open(config).onSuccess { session ->
Expand Down
2 changes: 1 addition & 1 deletion examples/src/main/kotlin/io.zenoh/ZQueryable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ZQueryable(private val emptyArgs: Boolean) : CliktCommand(
override fun run() {
val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode)

Zenoh.tryInitLogFromEnv()
Zenoh.initLogFromEnvOr("error")

Zenoh.open(config).onSuccess { session ->
session.use {
Expand Down
2 changes: 1 addition & 1 deletion examples/src/main/kotlin/io.zenoh/ZScout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ZScout : CliktCommand(
) {
override fun run() {

Zenoh.tryInitLogFromEnv()
Zenoh.initLogFromEnvOr("error")

println("Scouting...")

Expand Down
2 changes: 1 addition & 1 deletion examples/src/main/kotlin/io.zenoh/ZSub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ZSub(private val emptyArgs: Boolean) : CliktCommand(
override fun run() {
val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode)

Zenoh.tryInitLogFromEnv()
Zenoh.initLogFromEnvOr("error")

println("Opening session...")
Zenoh.open(config).onSuccess { session ->
Expand Down
2 changes: 2 additions & 0 deletions examples/src/main/kotlin/io.zenoh/ZSubThr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class ZSubThr(private val emptyArgs: Boolean) : CliktCommand(
override fun run() {
val config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode)

Zenoh.initLogFromEnvOr("error")

"test/thr".intoKeyExpr().onSuccess { keyExpr ->
keyExpr.use {
println("Opening Session")
Expand Down
16 changes: 2 additions & 14 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ internal class Logger {

internal const val LOG_ENV: String = "RUST_LOG"

fun start(logLevel: LogLevel) = runCatching {
startLogsViaJNI(logLevel.description)
fun start(logLevel: String) = runCatching {
startLogsViaJNI(logLevel)
}

/**
Expand All @@ -35,15 +35,3 @@ internal class Logger {
private external fun startLogsViaJNI(logLevel: String)
}
}

enum class LogLevel(internal val description: String) {
INFO("info"),
DEBUG("debug"),
WARN("warn"),
TRACE("trace"),
ERROR("error");

companion object {
internal fun fromString(description: String): LogLevel = LogLevel.entries.first { it.description == description }
}
}
11 changes: 5 additions & 6 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Zenoh.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,21 @@ object Zenoh {
*/
fun tryInitLogFromEnv(): Result<Unit> = runCatching {
ZenohLoad
val logProp = System.getenv(LOG_ENV)
val logLevel = System.getenv(LOG_ENV)
?: return Result.failure(Exception("Failure during logs initialization: couldn't find environment variable '$LOG_ENV'."))
val logLevel = LogLevel.fromString(logProp)
return Logger.start(logLevel)
}

/**
* Try starting the logs with the level specified under the [LOG_ENV] environment variable.
* Try starting the logs with the level specified under the [LOG_ENV] environment variable or by default [defaultLogLevel].
*
* @param defaultLogLevel A string that must be either "info", "debug", "error", "trace", "warn".
* @see Logger
*/
fun tryInitLogFromEnvOr(defaultLogLevel: LogLevel): Result<Unit> = runCatching {
fun initLogFromEnvOr(defaultLogLevel: String): Result<Unit> = runCatching {
ZenohLoad
val logLevelProp = System.getenv(LOG_ENV)
val logLevel = logLevelProp?.let { LogLevel.fromString(it) } ?: defaultLogLevel
Logger.start(logLevel)
logLevelProp?.let { Logger.start(it) } ?: Logger.start(defaultLogLevel)
}
}

Expand Down

0 comments on commit 5b51f5f

Please # to comment.