11//! AimDB handle for managing the sync API runtime thread.
22
33use crate :: { SyncError , SyncResult } ;
4- use aimdb_core:: { AimDb , AimDbBuilder , DbError , DbResult } ;
4+ use aimdb_core:: { log_error , log_warn , AimDb , AimDbBuilder , DbError , DbResult } ;
55use std:: fmt:: Debug ;
66use std:: sync:: Arc ;
77use std:: thread:: { self , JoinHandle } ;
@@ -46,11 +46,12 @@ pub trait AimDbBuilderSyncExt {
4646 /// ```no_run
4747 /// use aimdb_core::AimDbBuilder;
4848 /// use aimdb_tokio_adapter::TokioAdapter;
49- /// use aimdb_sync::AimDbBuilderSyncExt;
49+ /// use aimdb_sync::{ AimDbBuilderSyncExt, SyncResult} ;
5050 /// use std::sync::Arc;
5151 ///
5252 /// # #[derive(Debug, Clone)] struct MyData { value: f32 }
53- /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
53+ /// # #[cfg(feature = "std")]
54+ /// # fn main() -> SyncResult<()> {
5455 /// let mut builder = AimDbBuilder::new()
5556 /// .runtime(Arc::new(TokioAdapter::new()?));
5657 /// builder.configure::<MyData>("my.data", |reg| {
@@ -87,10 +88,10 @@ pub trait AimDbSyncExt {
8788 ///
8889 /// ```no_run
8990 /// use aimdb_core::AimDb;
90- /// use aimdb_sync::AimDbSyncExt;
91+ /// use aimdb_sync::{ AimDbSyncExt, SyncResult} ;
9192 ///
9293 /// // `db` comes out of an async `AimDbBuilder::build()` elsewhere
93- /// # fn demo(db: AimDb) -> Result <(), Box<dyn std::error::Error> > {
94+ /// # fn demo(db: AimDb) -> SyncResult <()> {
9495 /// let handle = db.attach()?;
9596 /// # Ok(())
9697 /// # }
@@ -156,7 +157,7 @@ impl AimDbHandle {
156157 let runtime = match tokio:: runtime:: Runtime :: new ( ) {
157158 Ok ( rt) => rt,
158159 Err ( e) => {
159- eprintln ! ( "Failed to create Tokio runtime: {}" , e) ;
160+ log_error ! ( "Failed to create Tokio runtime: {}" , e) ;
160161 return ;
161162 }
162163 } ;
@@ -166,7 +167,7 @@ impl AimDbHandle {
166167
167168 // Send the runtime handle to the main thread
168169 if handle_tx. blocking_send ( rt_handle) . is_err ( ) {
169- eprintln ! ( "Failed to send runtime handle to main thread" ) ;
170+ log_error ! ( "Failed to send runtime handle to main thread" ) ;
170171 return ;
171172 }
172173
@@ -175,14 +176,14 @@ impl AimDbHandle {
175176 let ( db, runner) = match builder. build ( ) . await {
176177 Ok ( d) => ( Arc :: new ( d. 0 ) , d. 1 ) ,
177178 Err ( e) => {
178- eprintln ! ( "Failed to build database: {}" , e) ;
179+ log_error ! ( "Failed to build database: {}" , e) ;
179180 return ;
180181 }
181182 } ;
182183
183184 // Send the database to the main thread
184185 if db_tx. send ( db. clone ( ) ) . await . is_err ( ) {
185- eprintln ! ( "Failed to send database to main thread" ) ;
186+ log_error ! ( "Failed to send database to main thread" ) ;
186187 return ;
187188 }
188189
@@ -242,7 +243,7 @@ impl AimDbHandle {
242243 let runtime = match tokio:: runtime:: Runtime :: new ( ) {
243244 Ok ( rt) => rt,
244245 Err ( e) => {
245- eprintln ! ( "Failed to create Tokio runtime: {}" , e) ;
246+ log_error ! ( "Failed to create Tokio runtime: {}" , e) ;
246247 return ;
247248 }
248249 } ;
@@ -302,7 +303,7 @@ impl AimDbHandle {
302303 /// # use serde::{Serialize, Deserialize};
303304 /// # #[derive(Debug, Clone, Serialize, Deserialize)]
304305 /// # struct Temperature { celsius: f32 }
305- /// # fn example(handle: &AimDbHandle) -> Result <(), Box<dyn std::error::Error> > {
306+ /// # fn example(handle: &AimDbHandle) -> SyncResult <()> {
306307 /// let producer = handle.producer::<Temperature>("sensor::temp")?;
307308 /// producer.set(Temperature { celsius: 25.0 })?;
308309 /// # Ok(())
@@ -337,7 +338,7 @@ impl AimDbHandle {
337338 /// # use serde::{Serialize, Deserialize};
338339 /// # #[derive(Clone, Debug, Serialize, Deserialize)]
339340 /// # struct Temperature { celsius: f32 }
340- /// # fn example(handle: &AimDbHandle) -> Result <(), Box<dyn std::error::Error> > {
341+ /// # fn example(handle: &AimDbHandle) -> SyncResult <()> {
341342 /// let consumer = handle.consumer::<Temperature>("sensor::temp")?;
342343 /// let temp = consumer.get()?;
343344 /// # Ok(())
@@ -376,7 +377,7 @@ impl AimDbHandle {
376377 /// # use serde::{Serialize, Deserialize};
377378 /// # #[derive(Debug, Clone, Serialize, Deserialize)]
378379 /// # struct HighFrequencySensor { value: f32 }
379- /// # fn example(handle: &AimDbHandle) -> Result <(), Box<dyn std::error::Error> > {
380+ /// # fn example(handle: &AimDbHandle) -> SyncResult <()> {
380381 /// // High-frequency sensor needs larger buffer
381382 /// let producer = handle.producer_with_capacity::<HighFrequencySensor>("sensor::high_freq", 1000)?;
382383 /// producer.set(HighFrequencySensor { value: 42.0 })?;
@@ -438,7 +439,7 @@ impl AimDbHandle {
438439 /// # use serde::{Serialize, Deserialize};
439440 /// # #[derive(Clone, Debug, Serialize, Deserialize)]
440441 /// # struct RareEvent { id: u32 }
441- /// # fn example(handle: &AimDbHandle) -> Result <(), Box<dyn std::error::Error> > {
442+ /// # fn example(handle: &AimDbHandle) -> SyncResult <()> {
442443 /// // Rare events need smaller buffer
443444 /// let consumer = handle.consumer_with_capacity::<RareEvent>("events::rare", 10)?;
444445 /// let event = consumer.get()?;
@@ -482,7 +483,7 @@ impl AimDbHandle {
482483 Err ( DbError :: BufferLagged { lag_count, .. } ) => {
483484 // Consumer fell behind - this is not fatal
484485 // Log warning but continue receiving
485- eprintln ! (
486+ log_warn ! (
486487 "Warning: Consumer for {} lagged by {} messages" ,
487488 std:: any:: type_name:: <T >( ) ,
488489 lag_count
@@ -495,7 +496,7 @@ impl AimDbHandle {
495496 }
496497 Err ( e) => {
497498 // Other unexpected errors - log and stop
498- eprintln ! (
499+ log_error ! (
499500 "Error reading from buffer for {}: {}" ,
500501 std:: any:: type_name:: <T >( ) ,
501502 e
@@ -506,7 +507,7 @@ impl AimDbHandle {
506507 }
507508 }
508509 Err ( e) => {
509- eprintln ! (
510+ log_error ! (
510511 "Failed to subscribe to record type {}: {}" ,
511512 std:: any:: type_name:: <T >( ) ,
512513 e
@@ -541,7 +542,7 @@ impl AimDbHandle {
541542 ///
542543 /// ```rust,no_run
543544 /// # use aimdb_sync::*;
544- /// # fn example(handle: AimDbHandle) -> Result <(), Box<dyn std::error::Error> > {
545+ /// # fn example(handle: AimDbHandle) -> SyncResult <()> {
545546 /// handle.detach()?;
546547 /// # Ok(())
547548 /// # }
@@ -568,7 +569,7 @@ impl AimDbHandle {
568569 /// ```rust,no_run
569570 /// # use aimdb_sync::*;
570571 /// # use std::time::Duration;
571- /// # fn example(handle: AimDbHandle) -> Result <(), Box<dyn std::error::Error> > {
572+ /// # fn example(handle: AimDbHandle) -> SyncResult <()> {
572573 /// handle.detach_timeout(Duration::from_secs(5))?;
573574 /// # Ok(())
574575 /// # }
@@ -641,12 +642,12 @@ impl Drop for AimDbHandle {
641642 /// If shutdown fails, the runtime thread may be left running.
642643 fn drop ( & mut self ) {
643644 if self . thread_handle . is_some ( ) {
644- eprintln ! ( "Warning: AimDbHandle dropped without calling detach()" ) ;
645- eprintln ! ( "Attempting emergency shutdown with 5 second timeout" ) ;
645+ log_warn ! ( "Warning: AimDbHandle dropped without calling detach()" ) ;
646+ log_warn ! ( "Attempting emergency shutdown with 5 second timeout" ) ;
646647
647648 let timeout = Duration :: from_secs ( 5 ) ;
648649 if let Err ( e) = self . detach_internal ( Some ( timeout) ) {
649- eprintln ! ( "Error during emergency shutdown: {}" , e) ;
650+ log_error ! ( "Error during emergency shutdown: {}" , e) ;
650651 }
651652 }
652653 }
0 commit comments