From c82fe6c3c4914557d0d65ac4337ac554471a476c Mon Sep 17 00:00:00 2001 From: joshua Date: Tue, 28 Oct 2025 16:24:31 -0400 Subject: [PATCH 1/3] Merge init_logging into C layer --- Cargo.lock | 36 +----------------------------------- c/BUILD | 6 +++--- c/Cargo.toml | 11 +++-------- c/src/concept/mod.rs | 1 - c/src/error.rs | 39 ++++++++++++++++++++++++++++++++++----- rust/src/driver.rs | 40 ---------------------------------------- 6 files changed, 41 insertions(+), 92 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e20ee10ed..b02ae1807b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -790,19 +790,6 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" -[[package]] -name = "env_logger" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" -dependencies = [ - "humantime", - "is-terminal", - "log", - "regex", - "termcolor", -] - [[package]] name = "equivalent" version = "1.0.1" @@ -1365,17 +1352,6 @@ dependencies = [ "libc", ] -[[package]] -name = "is-terminal" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" -dependencies = [ - "hermit-abi 0.4.0", - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -2530,15 +2506,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "terminal_size" version = "0.3.0" @@ -2871,10 +2838,9 @@ name = "typedb_driver_clib" version = "0.0.0" dependencies = [ "chrono", - "env_logger", "itertools 0.10.5", - "log", "tracing", + "tracing-subscriber", "typedb-driver", ] diff --git a/c/BUILD b/c/BUILD index 809b805287..f1e113a53a 100644 --- a/c/BUILD +++ b/c/BUILD @@ -45,8 +45,8 @@ rust_static_library( "@crates//:chrono", "@crates//:itertools", - "@crates//:env_logger", "@crates//:tracing", + "@crates//:tracing-subscriber", ], tags = ["crate-name=typedb_driver_clib"], ) @@ -71,8 +71,8 @@ rust_shared_library( "@crates//:chrono", "@crates//:itertools", - "@crates//:env_logger", - "@crates//:log", + "@crates//:tracing", + "@crates//:tracing-subscriber", "@crates//:tracing", ], tags = ["crate-name=typedb_driver_clib"], diff --git a/c/Cargo.toml b/c/Cargo.toml index d97baa9158..ea998c4eb8 100644 --- a/c/Cargo.toml +++ b/c/Cargo.toml @@ -14,19 +14,14 @@ features = {} [dependencies] - [dependencies.env_logger] - features = ["auto-color", "color", "default", "humantime", "regex"] - version = "0.10.2" - default-features = false - [dependencies.tracing] features = ["attributes", "default", "log", "std", "tracing-attributes"] version = "0.1.41" default-features = false - [dependencies.log] - features = ["kv", "kv_unstable", "std", "value-bag"] - version = "0.4.27" + [dependencies.tracing-subscriber] + features = ["alloc", "ansi", "default", "env-filter", "fmt", "matchers", "nu-ansi-term", "once_cell", "regex", "registry", "sharded-slab", "smallvec", "std", "thread_local", "tracing", "tracing-log"] + version = "0.3.19" default-features = false [dependencies.typedb-driver] diff --git a/c/src/concept/mod.rs b/c/src/concept/mod.rs index 6f6bae970e..30632f60a1 100644 --- a/c/src/concept/mod.rs +++ b/c/src/concept/mod.rs @@ -19,7 +19,6 @@ use std::ptr::addr_of_mut; -use itertools::Itertools; use typedb_driver::{answer::ConceptRow, concept::Concept, BoxPromise, Promise, Result}; use super::{iterator::iterator_try_next, memory::free}; diff --git a/c/src/error.rs b/c/src/error.rs index a147cc4855..9aa61bf72c 100644 --- a/c/src/error.rs +++ b/c/src/error.rs @@ -24,8 +24,8 @@ use std::{ sync::Arc, }; -use env_logger::Env; use tracing::{debug, warn}; +use tracing_subscriber::{fmt as tracing_fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; use typedb_driver::{Error, Result}; use super::memory::{free, release_arc, release_optional, release_string}; @@ -35,12 +35,41 @@ thread_local! { } /// Enables logging in the TypeDB driver. +/// +/// This function sets up tracing with the following priority: +/// 1. TYPEDB_DRIVER_LOG environment variable (if set). Use TYPEDB_DRIVER_CLIB_LOG to see memory exchanges +/// 2. RUST_LOG environment variable (if set) +/// 3. Default level (INFO) +/// +/// The logging is initialized only once using a static flag to prevent +/// multiple initializations in applications that create multiple drivers. #[no_mangle] pub extern "C" fn init_logging() { - const ENV_VAR: &str = "TYPEDB_DRIVER_LOG_LEVEL"; - if let Err(err) = env_logger::try_init_from_env(Env::new().filter(ENV_VAR)) { - warn!("{err}"); - } + use std::sync::Once; + static INIT: Once = Once::new(); + INIT.call_once(|| { + let clib_level = if let Ok(typedb_driver_clib_log) = std::env::var("TYPEDB_DRIVER_CLIB_LOG") { + typedb_driver_clib_log + } else { + "info".to_owned() + }; + // Try to get log level from TYPEDB_DRIVER_LOG first + let env_filter = if let Ok(typedb_log_level) = std::env::var("TYPEDB_DRIVER_LOG") { + EnvFilter::new(&format!("typedb_driver={},typedb_driver_clib={}", typedb_log_level, clib_level)) + } else if let Ok(rust_log) = std::env::var("RUST_LOG") { + // If RUST_LOG is set, use it but scope it to typedb_driver only + EnvFilter::new(&format!("typedb_driver={},typedb_driver_clib={}", rust_log, clib_level)) + } else { + EnvFilter::new(&format!("typedb_driver=info,typedb_driver_clib={}", clib_level)) + }; + + // Initialize the tracing subscriber + if let Err(e) = + tracing_subscriber::registry().with(env_filter).with(tracing_fmt::layer().with_target(false)).try_init() + { + eprintln!("Failed to initialize logging: {}", e); + } + }); } fn ok_record(result: Result) -> Option { diff --git a/rust/src/driver.rs b/rust/src/driver.rs index 37aaa15783..43458e5b57 100644 --- a/rust/src/driver.rs +++ b/rust/src/driver.rs @@ -54,44 +54,6 @@ impl TypeDBDriver { pub const DEFAULT_ADDRESS: &'static str = "localhost:1729"; - /// Initialize logging configuration for the TypeDB driver. - /// - /// This function sets up tracing with the following priority: - /// 1. TYPEDB_DRIVER_LOG environment variable (if set). Use TYPEDB_DRIVER_CLIB_LOG to see memory exchanges - /// 1. environment variable (if set) - /// 2. RUST_LOG environment variable (if set) - /// 3. Default level (INFO) - /// - /// The logging is initialized only once using a static flag to prevent - /// multiple initializations in applications that create multiple drivers. - pub fn init_logging() { - use std::sync::Once; - static INIT: Once = Once::new(); - - INIT.call_once(|| { - let clib_level = if let Ok(typedb_driver_clib_log) = std::env::var("TYPEDB_DRIVER_CLIB_LOG") { - typedb_driver_clib_log - } else { - "info".to_owned() - }; - // Try to get log level from TYPEDB_DRIVER_LOG first - let env_filter = if let Ok(typedb_log_level) = std::env::var("TYPEDB_DRIVER_LOG") { - EnvFilter::new(&format!("typedb_driver={},typedb_driver_clib={}", typedb_log_level, clib_level)) - } else if let Ok(rust_log) = std::env::var("RUST_LOG") { - // If RUST_LOG is set, use it but scope it to typedb_driver only - EnvFilter::new(&format!("typedb_driver={},typedb_driver_clib={}", rust_log, clib_level)) - } else { - EnvFilter::new(&format!("typedb_driver=info,typedb_driver_clib={}", clib_level)) - }; - - // Initialize the tracing subscriber - if let Err(e) = - tracing_subscriber::registry().with(env_filter).with(tracing_fmt::layer().with_target(false)).try_init() - { - eprintln!("Failed to initialize logging: {}", e); - } - }); - } /// Creates a new TypeDB Server connection. /// @@ -153,8 +115,6 @@ impl TypeDBDriver { driver_options: DriverOptions, driver_lang: impl AsRef, ) -> Result { - Self::init_logging(); - debug!("Initializing TypeDB driver with description: {}", driver_lang.as_ref()); let id = address.as_ref().to_string(); let address: Address = id.parse()?; From a0f10e29010bc7cbdbc0e61bc749eea0146ece29 Mon Sep 17 00:00:00 2001 From: joshua Date: Tue, 28 Oct 2025 16:41:33 -0400 Subject: [PATCH 2/3] Clean up and docs --- .../rust/connection/TypeDBDriver.adoc | 21 ------------------- rust/src/driver.rs | 1 - 2 files changed, 22 deletions(-) diff --git a/docs/modules/ROOT/partials/rust/connection/TypeDBDriver.adoc b/docs/modules/ROOT/partials/rust/connection/TypeDBDriver.adoc index a4f4427d25..a97287f9bd 100644 --- a/docs/modules/ROOT/partials/rust/connection/TypeDBDriver.adoc +++ b/docs/modules/ROOT/partials/rust/connection/TypeDBDriver.adoc @@ -32,27 +32,6 @@ Result driver.force_close() ---- -[#_struct_TypeDBDriver_init_logging_] -==== init_logging - -[source,rust] ----- -pub fn init_logging() ----- - -Initialize logging configuration for the TypeDB driver. - -This function sets up tracing with the following priority: - -The logging is initialized only once using a static flag to prevent multiple initializations in applications that create multiple drivers. - -[caption=""] -.Returns -[source,rust] ----- -null ----- - [#_struct_TypeDBDriver_is_open_] ==== is_open diff --git a/rust/src/driver.rs b/rust/src/driver.rs index 43458e5b57..c7fe1b55c4 100644 --- a/rust/src/driver.rs +++ b/rust/src/driver.rs @@ -54,7 +54,6 @@ impl TypeDBDriver { pub const DEFAULT_ADDRESS: &'static str = "localhost:1729"; - /// Creates a new TypeDB Server connection. /// /// # Arguments From 3a1ec15166e42c78da41ffcbfb9307c78399ec94 Mon Sep 17 00:00:00 2001 From: joshua Date: Tue, 28 Oct 2025 16:43:44 -0400 Subject: [PATCH 3/3] Fix duplicate deps --- c/BUILD | 1 - rust/BUILD | 1 - 2 files changed, 2 deletions(-) diff --git a/c/BUILD b/c/BUILD index f1e113a53a..e6c45f7207 100644 --- a/c/BUILD +++ b/c/BUILD @@ -73,7 +73,6 @@ rust_shared_library( "@crates//:itertools", "@crates//:tracing", "@crates//:tracing-subscriber", - "@crates//:tracing", ], tags = ["crate-name=typedb_driver_clib"], ) diff --git a/rust/BUILD b/rust/BUILD index 3d3786f342..947cd174fd 100644 --- a/rust/BUILD +++ b/rust/BUILD @@ -35,7 +35,6 @@ typedb_driver_deps = [ "@crates//:futures", "@crates//:http", "@crates//:itertools", - "@crates//:log", "@crates//:prost", "@crates//:serde", "@crates//:tokio",