Skip to content

Commit

Permalink
Using filters for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
DariusIMP committed Sep 13, 2024
1 parent 5b51f5f commit 2d4804a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
15 changes: 8 additions & 7 deletions zenoh-jni/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ use crate::{errors::Result, jni_error, throw_exception};

/// Redirects the Rust logs either to logcat for Android systems or to the standard output (for non-Android systems).
///
/// This function is meant to be called from Java/Kotlin code through JNI. It takes a `log_level`
/// indicating the desired log level, which must be one of the following: "info", "debug", "warn",
/// "trace", or "error".
/// This function is meant to be called from Java/Kotlin code through JNI. It takes a `filter`
/// indicating the desired log level.
///
/// See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format.
///
/// # Parameters:
/// - `env`: The JNI environment.
/// - `_class`: The JNI class.
/// - `log_level`: The log level java string indicating the desired log level.
/// - `filter`: The logs filter.
///
/// # Errors:
/// - If there is an error parsing the log level string, a `JNIException` is thrown on the JVM.
Expand All @@ -38,10 +39,10 @@ use crate::{errors::Result, jni_error, throw_exception};
pub extern "C" fn Java_io_zenoh_Logger_00024Companion_startLogsViaJNI(
mut env: JNIEnv,
_class: JClass,
log_level: JString,
filter: JString,
) {
|| -> Result<()> {
let log_level = parse_log_level(&mut env, log_level)?;
let log_level = parse_filter(&mut env, filter)?;
android_logd_logger::builder()
.parse_filters(log_level.as_str())
.tag_target_strip()
Expand All @@ -52,7 +53,7 @@ pub extern "C" fn Java_io_zenoh_Logger_00024Companion_startLogsViaJNI(
.unwrap_or_else(|err| throw_exception!(env, err))
}

fn parse_log_level(env: &mut JNIEnv, log_level: JString) -> Result<String> {
fn parse_filter(env: &mut JNIEnv, log_level: JString) -> Result<String> {
let log_level = env.get_string(&log_level).map_err(|err| jni_error!(err))?;
log_level
.to_str()
Expand Down
8 changes: 4 additions & 4 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ internal class Logger {

internal const val LOG_ENV: String = "RUST_LOG"

fun start(logLevel: String) = runCatching {
startLogsViaJNI(logLevel)
fun start(filter: String) = runCatching {
startLogsViaJNI(filter)
}

/**
* Redirects the rust logs either to logcat for Android systems or to the standard output (for non-android
* systems).
*
* @param logLevel must be either "info", "debug", "warn", "trace" or "error".
* See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format.
*/
@Throws(Exception::class)
private external fun startLogsViaJNI(logLevel: String)
private external fun startLogsViaJNI(filter: String)
}
}
24 changes: 15 additions & 9 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Zenoh.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,33 @@ object Zenoh {
}

/**
* Try starting the logs with the level specified under the [LOG_ENV] environment variable.
* Initializes the zenoh runtime logger, using rust environment settings.
* E.g.: `RUST_LOG=info` will enable logging at info level. Similarly, you can set the variable to `error` or `debug`.
*
* Note that if the environment variable is not set, then logging will not be enabled.
* See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format.
*
* @see Logger
*/
fun tryInitLogFromEnv(): Result<Unit> = runCatching {
fun tryInitLogFromEnv() {
ZenohLoad
val logLevel = System.getenv(LOG_ENV)
?: return Result.failure(Exception("Failure during logs initialization: couldn't find environment variable '$LOG_ENV'."))
return Logger.start(logLevel)
Logger.start(System.getenv(LOG_ENV) ?: "")
}

/**
* Try starting the logs with the level specified under the [LOG_ENV] environment variable or by default [defaultLogLevel].
* Initializes the zenoh runtime logger, using rust environment settings or the provided fallback level.
* E.g.: `RUST_LOG=info` will enable logging at info level. Similarly, you can set the variable to `error` or `debug`.
*
* Note that if the environment variable is not set, then [fallbackFilter] will be used instead.
* See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format.
*
* @param defaultLogLevel A string that must be either "info", "debug", "error", "trace", "warn".
* @param fallbackFilter: The fallback filter if the `RUST_LOG` environment variable is not set.
* @see Logger
*/
fun initLogFromEnvOr(defaultLogLevel: String): Result<Unit> = runCatching {
fun initLogFromEnvOr(fallbackFilter: String): Result<Unit> = runCatching {
ZenohLoad
val logLevelProp = System.getenv(LOG_ENV)
logLevelProp?.let { Logger.start(it) } ?: Logger.start(defaultLogLevel)
logLevelProp?.let { Logger.start(it) } ?: Logger.start(fallbackFilter)
}
}

Expand Down

0 comments on commit 2d4804a

Please sign in to comment.