Skip to content

Commit

Permalink
Fix tracing logging functions (#945)
Browse files Browse the repository at this point in the history
* Add file header

* Fix log functions

* Remove erroneous code in valgrind CI

* Address comments
  • Loading branch information
Mallets authored Apr 17, 2024
1 parent 23c5932 commit 4f42ce7
Show file tree
Hide file tree
Showing 56 changed files with 193 additions and 159 deletions.
4 changes: 2 additions & 2 deletions ci/valgrind-check/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ description = "Internal crate for zenoh."

[dependencies]
tokio = { version = "1.35.1", features = ["rt-multi-thread", "time", "io-std"] }
tracing-subscriber = {version = "0.3", features = ["json", "env-filter"]}
tracing-subscriber = { version = "0.3", features = ["json", "env-filter"] }
futures = "0.3.25"
zenoh = { path = "../../zenoh/" }
zenoh-runtime = { path = "../../commons/zenoh-runtime/" }
zenoh-util = { path = "../../commons/zenoh-util/" }
zenoh-util = { path = "../../commons/zenoh-util/", features = ["test"] }

[[bin]]
name = "pub_sub"
Expand Down
5 changes: 2 additions & 3 deletions ci/valgrind-check/src/pub_sub/bin/z_pub_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
use std::time::Duration;
use zenoh::config::Config;
use zenoh::prelude::r#async::*;
use zenoh_util::init_log;

#[tokio::main]
async fn main() {

init_log();
zenoh_util::init_log_test();

let _z = zenoh_runtime::ZRuntimePoolGuard;

Expand Down
9 changes: 4 additions & 5 deletions ci/valgrind-check/src/queryable_get/bin/z_queryable_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ use std::convert::TryFrom;
use std::time::Duration;
use zenoh::config::Config;
use zenoh::prelude::r#async::*;
use zenoh_util::init_log;

#[tokio::main]
async fn main() {
init_log();
zenoh_util::init_log_test();

let _z = zenoh_runtime::ZRuntimePoolGuard;

Expand All @@ -36,9 +35,9 @@ async fn main() {
queryable_key_expr.clone(),
query.value().unwrap().clone(),
));
zenoh_runtime::ZRuntime::Application.block_in_place(
async move { query.reply(reply).res().await.unwrap(); }
);
zenoh_runtime::ZRuntime::Application.block_in_place(async move {
query.reply(reply).res().await.unwrap();
});
})
.complete(true)
.res()
Expand Down
2 changes: 1 addition & 1 deletion commons/zenoh-codec/tests/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ fn codec_shm_info() {
// Common
#[test]
fn codec_extension() {
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

macro_rules! run_extension_single {
($ext:ty, $buff:expr) => {
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ maintenance = { status = "actively-developed" }

[features]
std = []
test = []
default = ["std"]

[dependencies]
Expand Down
46 changes: 41 additions & 5 deletions commons/zenoh-util/src/std_only/log.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
use tracing_subscriber::EnvFilter;

/// This is an utility function to enable the tracing formatting subscriber from
/// the `RUST_LOG` environment variable.
/// the `RUST_LOG` environment variable. If `RUST_LOG` is not set, then logging is not enabled.
///
/// # Safety
/// Calling this function initializes a `lazy_static` in the `tracing` crate
/// such static is not deallocated prior to process existing, thus tools such as `valgrind`
/// will report a memory leak.
/// Refer to this issue: https://github.com/tokio-rs/tracing/issues/2069
pub fn init_log_from_env() {
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("z=info"));
pub fn try_init_log_from_env() {
if let Ok(env_filter) = EnvFilter::try_from_default_env() {
init_env_filter(env_filter);
}
}

/// This is an utility function to enable the tracing formatting subscriber from
/// the environment variable. If `RUST_LOG` is not set, then fallback directives are used.
///
/// # Safety
/// Calling this function initializes a `lazy_static` in the `tracing` crate
/// such static is not deallocated prior to process existing, thus tools such as `valgrind`
/// will report a memory leak.
/// Refer to this issue: https://github.com/tokio-rs/tracing/issues/2069
pub fn init_log_from_env_or<S>(fallback: S)
where
S: AsRef<str>,
{
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(fallback));
init_env_filter(env_filter);
}

fn init_env_filter(env_filter: EnvFilter) {
let subscriber = tracing_subscriber::fmt()
.with_env_filter(env_filter)
.with_thread_ids(true)
Expand All @@ -22,8 +55,11 @@ pub fn init_log_from_env() {
let _ = tracing::subscriber::set_global_default(subscriber);
}

/// This is an utility function to enables the default tracing subscriber with INFO level
pub fn init_log() {
#[cfg(feature = "test")]
// Used to verify memory leaks for valgrind CI.
// `EnvFilter` internally uses a static reference that is not cleaned up yielding to false positive in valgrind.
// This function enables logging without calling `EnvFilter` for env configuration.
pub fn init_log_test() {
let subscriber = tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_thread_ids(true)
Expand Down
3 changes: 1 addition & 2 deletions commons/zenoh-util/src/std_only/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ pub use lib_loader::*;
pub mod timer;
pub use timer::*;
pub mod log;
pub use log::init_log;
pub use log::init_log_from_env;
pub use log::*;

/// The "ZENOH_HOME" environement variable name
pub const ZENOH_HOME_ENV_VAR: &str = "ZENOH_HOME";
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh_ext::SubscriberForward;
#[tokio::main]
async fn main() {
// Initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr, forward) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, selector, value, target, timeout) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_get_liveliness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr, timeout) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let config = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_liveliness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// Initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh_examples::CommonArgs;

fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, warmup, size, n) = parse_args();
let session = zenoh::open(config).res().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_examples::CommonArgs;

fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let config = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// Initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr, value, attachment) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_pub_shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const K: u32 = 3;
#[tokio::main]
async fn main() -> Result<(), zenoh::Error> {
// Initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (mut config, path, value) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_pub_shm_thr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();
let (mut config, sm_size, size) = parse_args();

// A probing procedure for shared memory is performed upon session opening. To enable `z_pub_shm_thr` to operate
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_pub_thr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh_examples::CommonArgs;

fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();
let args = Args::parse();

let mut prio = Priority::default();
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr, value) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_put_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr, value) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr, value, complete) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_scout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use zenoh::scouting::WhatAmI;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

println!("Scouting...");
let receiver = zenoh::scout(WhatAmI::Peer | WhatAmI::Router, Config::default())
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr, complete) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// Initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (mut config, key_expr) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_sub_liveliness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_examples::CommonArgs;
#[tokio::main]
async fn main() {
// Initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (config, key_expr) = parse_args();

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/z_sub_thr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Drop for Stats {

fn main() {
// initiate logging
zenoh_util::init_log_from_env();
zenoh_util::try_init_log_from_env();

let (mut config, m, n) = parse_args();

Expand Down
Loading

0 comments on commit 4f42ce7

Please sign in to comment.