Skip to content

Commit

Permalink
Logging: Set tracing subscriber globally
Browse files Browse the repository at this point in the history
Set default tracing subscriber globally for all threads for the duration
of the entire program. This can be set only once, so subsequent attemps
to set the log level through `cass_log_set_level` will be ignored. The logging
configuration must be done before any other driver function is called,
otherwise, the default logging callback will be used, and logs will
appear on stderr.

Fixes scylladb#110
  • Loading branch information
Gor027 committed Apr 26, 2023
1 parent 0b0c393 commit 265cc41
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 26 deletions.
5 changes: 0 additions & 5 deletions scylla-rust-wrapper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,9 +38,6 @@ lazy_static! {
cb: Some(stderr_log_callback),
data: std::ptr::null_mut(),
});
pub static ref LOG: RwLock<Option<DefaultGuard>> = RwLock::new(Some(
set_tracing_subscriber_with_level(tracing::Level::WARN)
));
}

// To send a Rust object to C:
Expand Down
24 changes: 6 additions & 18 deletions scylla-rust-wrapper/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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;
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;
Expand Down Expand Up @@ -143,41 +141,31 @@ 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()
.add_directive(level.to_owned().into()),
)
.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);
}

Expand Down
3 changes: 0 additions & 3 deletions scylla-rust-wrapper/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -29,8 +28,6 @@ pub type CassSession = RwLock<Option<Session>>;

#[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)
}
Expand Down

0 comments on commit 265cc41

Please sign in to comment.