Skip to content

Commit

Permalink
feat(tracing): added init_log function that does not use the EnvFilte…
Browse files Browse the repository at this point in the history
…r, thus no static leaks

Signed-off-by: gabrik <[email protected]>
  • Loading branch information
gabrik committed Apr 11, 2024
1 parent b21c054 commit 809e71f
Show file tree
Hide file tree
Showing 53 changed files with 163 additions and 143 deletions.
5 changes: 4 additions & 1 deletion ci/valgrind-check/src/pub_sub/bin/z_pub_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
use std::time::Duration;
use zenoh::config::Config;
use zenoh::prelude::r#async::*;

use zenoh_util::init_log_from_env;
#[tokio::main]
async fn main() {

init_log_from_env();

let _z = zenoh_runtime::ZRuntimePoolGuard;

let pub_key_expr = KeyExpr::try_from("test/valgrind/data").unwrap();
Expand Down
3 changes: 3 additions & 0 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,9 +15,12 @@ use std::convert::TryFrom;
use std::time::Duration;
use zenoh::config::Config;
use zenoh::prelude::r#async::*;
use zenoh_util::init_log_from_env;

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

let _z = zenoh_runtime::ZRuntimePoolGuard;

let queryable_key_expr = KeyExpr::try_from("test/valgrind/data").unwrap();
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();
zenoh_util::init_log_from_env();

macro_rules! run_extension_single {
($ext:ty, $buff:expr) => {
Expand Down
15 changes: 14 additions & 1 deletion commons/zenoh-util/src/std_only/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tracing_subscriber::EnvFilter;
/// 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() {
pub fn init_log_from_env() {
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("z=info"));

let subscriber = tracing_subscriber::fmt()
Expand All @@ -21,3 +21,16 @@ pub fn init_log() {
let subscriber = subscriber.finish();
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() {
let subscriber = tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_thread_ids(true)
.with_thread_names(true)
.with_level(true)
.with_target(true);

let subscriber = subscriber.finish();
let _ = tracing::subscriber::set_global_default(subscriber);
}
1 change: 1 addition & 0 deletions commons/zenoh-util/src/std_only/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod timer;
pub use timer::*;
pub mod log;
pub use log::init_log;
pub use log::init_log_from_env;

/// 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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::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();
zenoh_util::init_log_from_env();

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

Expand Down
24 changes: 12 additions & 12 deletions io/zenoh-transport/tests/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async fn run(endpoints: &[EndPoint]) {
#[cfg(feature = "transport_tcp")]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn endpoint_tcp() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();
// Define the locators
let endpoints: Vec<EndPoint> = vec![
format!("tcp/127.0.0.1:{}", 7000).parse().unwrap(),
Expand All @@ -112,7 +112,7 @@ async fn endpoint_tcp() {
#[cfg(feature = "transport_udp")]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn endpoint_udp() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();
// Define the locators
let endpoints: Vec<EndPoint> = vec![
format!("udp/127.0.0.1:{}", 7010).parse().unwrap(),
Expand All @@ -125,7 +125,7 @@ async fn endpoint_udp() {
#[cfg(all(feature = "transport_unixsock-stream", target_family = "unix"))]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn endpoint_unix() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();
// Remove the files if they still exists
let f1 = "zenoh-test-unix-socket-0.sock";
let f2 = "zenoh-test-unix-socket-1.sock";
Expand All @@ -146,7 +146,7 @@ async fn endpoint_unix() {
#[cfg(feature = "transport_ws")]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn endpoint_ws() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();
// Define the locators
let endpoints: Vec<EndPoint> = vec![
format!("ws/127.0.0.1:{}", 7020).parse().unwrap(),
Expand All @@ -159,7 +159,7 @@ async fn endpoint_ws() {
#[cfg(feature = "transport_unixpipe")]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn endpoint_unixpipe() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();
// Define the locators
let endpoints: Vec<EndPoint> = vec![
"unixpipe/endpoint_unixpipe".parse().unwrap(),
Expand All @@ -173,7 +173,7 @@ async fn endpoint_unixpipe() {
#[cfg(all(feature = "transport_tcp", feature = "transport_udp"))]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn endpoint_tcp_udp() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();
// Define the locators
let endpoints: Vec<EndPoint> = vec![
format!("tcp/127.0.0.1:{}", 7030).parse().unwrap(),
Expand All @@ -192,7 +192,7 @@ async fn endpoint_tcp_udp() {
))]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn endpoint_tcp_udp_unix() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();
// Remove the file if it still exists
let f1 = "zenoh-test-unix-socket-2.sock";
let _ = std::fs::remove_file(f1);
Expand All @@ -216,7 +216,7 @@ async fn endpoint_tcp_udp_unix() {
))]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn endpoint_tcp_unix() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();
// Remove the file if it still exists
let f1 = "zenoh-test-unix-socket-3.sock";
let _ = std::fs::remove_file(f1);
Expand All @@ -238,7 +238,7 @@ async fn endpoint_tcp_unix() {
))]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn endpoint_udp_unix() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();
// Remove the file if it still exists
let f1 = "zenoh-test-unix-socket-4.sock";
let _ = std::fs::remove_file(f1); // Define the locators
Expand All @@ -257,7 +257,7 @@ async fn endpoint_udp_unix() {
async fn endpoint_tls() {
use zenoh_link::tls::config::*;

zenoh_util::init_log();
zenoh_util::init_log_from_env();

// NOTE: this an auto-generated pair of certificate and key.
// The target domain is localhost, so it has no real
Expand Down Expand Up @@ -336,7 +336,7 @@ AXVFFIgCSluyrolaD6CWD9MqOex4YOfJR2bNxI7lFvuK4AwjyUJzT1U1HXib17mM
async fn endpoint_quic() {
use zenoh_link::quic::config::*;

zenoh_util::init_log();
zenoh_util::init_log_from_env();

// NOTE: this an auto-generated pair of certificate and key.
// The target domain is localhost, so it has no real
Expand Down Expand Up @@ -412,7 +412,7 @@ AXVFFIgCSluyrolaD6CWD9MqOex4YOfJR2bNxI7lFvuK4AwjyUJzT1U1HXib17mM
#[cfg(all(feature = "transport_vsock", target_os = "linux"))]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn endpoint_vsock() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();
// Define the locators
let endpoints: Vec<EndPoint> = vec![
"vsock/-1:1234".parse().unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion io/zenoh-transport/tests/multicast_compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ mod tests {
#[cfg(feature = "transport_udp")]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn transport_multicast_compression_udp_only() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();

// Define the locator
let endpoints: Vec<EndPoint> = vec![
Expand Down
2 changes: 1 addition & 1 deletion io/zenoh-transport/tests/multicast_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ mod tests {
#[cfg(all(feature = "transport_compression", feature = "transport_udp"))]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn transport_multicast_udp_only() {
zenoh_util::init_log();
zenoh_util::init_log_from_env();

// Define the locator
let endpoints: Vec<EndPoint> = vec![
Expand Down
Loading

0 comments on commit 809e71f

Please sign in to comment.