Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracing subscriber to enable logging #78

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] {
piodul marked this conversation as resolved.
Show resolved Hide resolved
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