Skip to content

Commit

Permalink
Add tracing subscriber to enable logging
Browse files Browse the repository at this point in the history
Motivation:

Added `tracing-subscriber` crate to have one common way of logging with
the Rust driver, as the Rust driver uses `tracing` crate for logging and
cpp-rust driver should allow clients to configure logging by specifying
the max log level and providing callback that should be called on each
event.

Solution:

As logging should be configured globally, the current implementation
creates a custom tracing subscriber with default configuration, which
later can be changed by the user. The global and mutable state of the
logging configuration is initialized in the `lazy_static!` block,
however, it will be evaluated only after the first access in the code.
As, the `cass_session_new()` function most probably is the first
function that the user will call (almost all tests do that, too), the
initialization of logging is chosen to be done in this function.

The CassLogMessage has a field `function` that should always be empty,
as it cannot be properly set, because event metadata does not include
information about it.

Enabled `LoggingTests` tests in github actions.
  • Loading branch information
Gor027 committed Dec 15, 2022
1 parent 358faae commit 4062a31
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
:TracingTests.*\
:ByNameTests.*\
:CompressionTests.*\
:LoggingTests.*\
:-PreparedTests.Integration_Cassandra_PreparedIDUnchangedDuringReprepare\
:*5.Integration_Cassandra_*\
:*19.Integration_Cassandra_*"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/cassandra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
:TracingTests.*\
:ByNameTests.*\
:CompressionTests.*\
:LoggingTests.*\
:-PreparedTests.Integration_Cassandra_PreparedIDUnchangedDuringReprepare\
:*5.Integration_Cassandra_*\
:*19.Integration_Cassandra_*\
Expand Down
74 changes: 74 additions & 0 deletions scylla-rust-wrapper/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions scylla-rust-wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ num-derive = "0.3"
libc = "0.2.108"
openssl-sys = "0.9.75"
openssl = "0.10.32"
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
tracing = "0.1.25"

[build-dependencies]
bindgen = "0.59.1"
Expand Down
10 changes: 10 additions & 0 deletions scylla-rust-wrapper/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ fn main() {
&["CassInet_", "CassInet"],
&out_path,
);
prepare_cppdriver_data(
"cppdriver_log.rs",
&[
"CassLogLevel_",
"CassLogLevel",
"CassLogMessage_",
"CassLogMessage",
],
&out_path,
);
prepare_cppdriver_data(
"cppdriver_data_query_error.rs",
&[
Expand Down
24 changes: 24 additions & 0 deletions scylla-rust-wrapper/src/argconv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::types::size_t;
use std::cmp::min;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::sync::Arc;
Expand Down Expand Up @@ -38,6 +39,29 @@ pub unsafe fn ptr_to_cstr_n(ptr: *const c_char, size: size_t) -> Option<&'static
std::str::from_utf8(std::slice::from_raw_parts(ptr as *const u8, size as usize)).ok()
}

pub unsafe fn arr_to_cstr<const N: usize>(arr: &[c_char]) -> Option<&'static str> {
let null_char = '\0' as c_char;
let end_index = arr[..N].iter().position(|c| c == &null_char).unwrap_or(N);
ptr_to_cstr_n(arr.as_ptr(), end_index as size_t)
}

pub fn str_to_arr<const N: usize>(s: &str) -> [c_char; N] {
let mut result = ['\0' as c_char; N];

// Max length must be null-terminated
let mut max_len = min(N - 1, s.as_bytes().len());

while !s.is_char_boundary(max_len) {
max_len -= 1;
}

for (i, c) in s.as_bytes().iter().enumerate().take(max_len) {
result[i] = *c as c_char;
}

result
}

pub unsafe fn write_str_to_c(s: &str, c_str: *mut *const c_char, c_strlen: *mut size_t) {
*c_str = s.as_ptr() as *const c_char;
*c_strlen = s.len() as u64;
Expand Down
14 changes: 14 additions & 0 deletions scylla-rust-wrapper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#![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 All @@ -13,6 +19,7 @@ pub mod collection;
mod external;
pub mod future;
pub mod inet;
mod logging;
pub mod prepared;
pub mod query_error;
pub mod query_result;
Expand All @@ -28,6 +35,13 @@ pub mod uuid;

lazy_static! {
pub static ref RUNTIME: Runtime = Runtime::new().unwrap();
pub static ref LOGGER: RwLock<Logger> = RwLock::new(Logger {
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
Loading

0 comments on commit 4062a31

Please sign in to comment.