diff --git a/scylla-rust-wrapper/src/lib.rs b/scylla-rust-wrapper/src/lib.rs index 1f4adb02..4a093094 100644 --- a/scylla-rust-wrapper/src/lib.rs +++ b/scylla-rust-wrapper/src/lib.rs @@ -1,12 +1,10 @@ #![allow(clippy::missing_safety_doc)] -use crate::logging::set_tracing_subscriber_with_level; use crate::logging::stderr_log_callback; use crate::logging::Logger; use lazy_static::lazy_static; use std::sync::RwLock; use tokio::runtime::Runtime; -use tracing::dispatcher::DefaultGuard; #[macro_use] mod binding; @@ -40,9 +38,6 @@ lazy_static! { cb: Some(stderr_log_callback), data: std::ptr::null_mut(), }); - pub static ref LOG: RwLock> = RwLock::new(Some( - set_tracing_subscriber_with_level(tracing::Level::WARN) - )); } // To send a Rust object to C: diff --git a/scylla-rust-wrapper/src/logging.rs b/scylla-rust-wrapper/src/logging.rs index 14e6785f..3d387c4c 100644 --- a/scylla-rust-wrapper/src/logging.rs +++ b/scylla-rust-wrapper/src/logging.rs @@ -1,6 +1,5 @@ use crate::argconv::{arr_to_cstr, ptr_to_cstr, ptr_to_ref, str_to_arr}; use crate::types::size_t; -use crate::LOG; use crate::LOGGER; use std::convert::TryFrom; use std::fmt::Debug; @@ -8,7 +7,6 @@ use std::fmt::Write; use std::os::raw::{c_char, c_void}; use std::time::{SystemTime, UNIX_EPOCH}; use tracing::debug; -use tracing::dispatcher::DefaultGuard; use tracing::field::Field; use tracing::Level; use tracing_subscriber::layer::Context; @@ -143,10 +141,10 @@ where } } -// Sets tracing subscriber with specified `level` and returns `DefaultGuard`. -// The subscriber is valid for the duration of the lifetime of the returned `DefaultGuard`. -pub fn set_tracing_subscriber_with_level(level: Level) -> DefaultGuard { - tracing::subscriber::set_default( +// Sets tracing subscriber with specified `level`. +// The subscriber is valid for the duration of the entire program. +pub fn set_tracing_subscriber_with_level(level: Level) { + tracing::subscriber::set_global_default( tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::from_default_env() @@ -154,30 +152,20 @@ pub fn set_tracing_subscriber_with_level(level: Level) -> DefaultGuard { ) .with(CustomLayer), ) -} - -pub fn init_logging() { - let _log = (*LOG.read().unwrap()).as_ref().unwrap(); + .unwrap_or(()) // Ignore if it is set already } #[no_mangle] pub unsafe extern "C" fn cass_log_set_level(log_level: CassLogLevel) { if log_level == CassLogLevel::CASS_LOG_DISABLED { debug!("Logging is disabled!"); - *LOG.write().unwrap() = None; return; } let level = Level::try_from(log_level).unwrap_or(Level::WARN); - // Drops the `DefaultGuard` of the current tracing subscriber making it invalid. - // Setting LOG to None is vital, otherwise, the new tracing subscriber created in - // `set_tracing_subscriber_with_level` will be ignored as the previous `DefaultGuard` is - // still alive. - *LOG.write().unwrap() = None; // Sets the tracing subscriber with new log level. - *LOG.write().unwrap() = Some(set_tracing_subscriber_with_level(level)); - + set_tracing_subscriber_with_level(level); debug!("Log level is set to {}", level); } diff --git a/scylla-rust-wrapper/src/session.rs b/scylla-rust-wrapper/src/session.rs index 5ea4e5f6..6e5f7094 100644 --- a/scylla-rust-wrapper/src/session.rs +++ b/scylla-rust-wrapper/src/session.rs @@ -5,7 +5,6 @@ use crate::cass_types::{get_column_type, CassDataType, UDTDataType}; use crate::cluster::build_session_builder; use crate::cluster::CassCluster; use crate::future::{CassFuture, CassResultValue}; -use crate::logging::init_logging; use crate::metadata::create_table_metadata; use crate::metadata::{CassKeyspaceMeta, CassMaterializedViewMeta, CassSchemaMeta}; use crate::query_result::Value::{CollectionValue, RegularValue}; @@ -29,8 +28,6 @@ pub type CassSession = RwLock>; #[no_mangle] pub unsafe extern "C" fn cass_session_new() -> *const CassSession { - init_logging(); - let session = Arc::new(RwLock::new(None)); Arc::into_raw(session) }