@@ -54,6 +54,7 @@ pub struct RpcClient {
5454 request_writer : rustdds:: no_key:: DataWriter < RpcReqMsg > ,
5555 response_reader : Arc < Mutex < DataReader < RpcRespMsg > > > ,
5656 default_timeout : Duration ,
57+ service_topic : String ,
5758}
5859
5960#[ derive( Debug , Deserialize , Default ) ]
@@ -99,6 +100,33 @@ fn normalize_service_topic(service_topic: &str) -> String {
99100 trimmed. to_owned ( )
100101}
101102
103+ fn rpc_debug_enabled ( ) -> bool {
104+ std:: env:: var ( "BOOSTER_RPC_DEBUG" )
105+ . map ( |value| {
106+ let value = value. trim ( ) ;
107+ value == "1"
108+ || value. eq_ignore_ascii_case ( "true" )
109+ || value. eq_ignore_ascii_case ( "yes" )
110+ || value. eq_ignore_ascii_case ( "on" )
111+ } )
112+ . unwrap_or ( false )
113+ }
114+
115+ fn preview_for_log ( value : & str , max_chars : usize ) -> String {
116+ let mut preview = String :: new ( ) ;
117+ let mut chars = value. chars ( ) ;
118+ for _ in 0 ..max_chars {
119+ match chars. next ( ) {
120+ Some ( ch) => preview. push ( ch) ,
121+ None => return preview. replace ( '\n' , "\\ n" ) ,
122+ }
123+ }
124+ if chars. next ( ) . is_some ( ) {
125+ preview. push_str ( "..." ) ;
126+ }
127+ preview. replace ( '\n' , "\\ n" )
128+ }
129+
102130impl RpcClient {
103131 pub fn for_topic ( options : RpcClientOptions , service_topic : impl Into < String > ) -> Result < Self > {
104132 Self :: new ( options. with_service_topic ( service_topic) )
@@ -120,6 +148,7 @@ impl RpcClient {
120148 request_writer : request_writer. into_inner ( ) ,
121149 response_reader : Arc :: new ( Mutex :: new ( response_reader) ) ,
122150 default_timeout : options. default_timeout ,
151+ service_topic,
123152 } )
124153 }
125154
@@ -199,9 +228,23 @@ impl RpcClient {
199228 where
200229 R : DeserializeOwned + Send + ' static ,
201230 {
231+ let debug_enabled = rpc_debug_enabled ( ) ;
202232 let request_id = Uuid :: new_v4 ( ) . to_string ( ) ;
203233 let body = body. into ( ) ;
204234 let header = serde_json:: json!( { "api_id" : api_id } ) . to_string ( ) ;
235+ let service_topic = self . service_topic . clone ( ) ;
236+
237+ if debug_enabled {
238+ tracing:: debug!(
239+ target: "booster_sdk::rpc" ,
240+ service_topic = %service_topic,
241+ api_id,
242+ request_uuid = %request_id,
243+ header = %preview_for_log( & header, 200 ) ,
244+ body = %preview_for_log( & body, 300 ) ,
245+ "send rpc request"
246+ ) ;
247+ }
205248
206249 let request = RpcReqMsg {
207250 uuid : request_id. clone ( ) ,
@@ -224,19 +267,48 @@ impl RpcClient {
224267 . map_err ( |err| DdsError :: ReceiveFailed ( err. to_string ( ) ) ) ?;
225268 loop {
226269 if Instant :: now ( ) >= deadline {
270+ if debug_enabled {
271+ tracing:: warn!(
272+ target: "booster_sdk::rpc" ,
273+ service_topic = %service_topic,
274+ api_id,
275+ request_uuid = %request_id,
276+ timeout_ms = timeout. as_millis( ) ,
277+ "rpc timeout"
278+ ) ;
279+ }
227280 return Err ( RpcError :: Timeout { timeout } . into ( ) ) ;
228281 }
229282
230283 match reader. take_next_sample ( ) {
231284 Ok ( Some ( sample) ) => {
232285 let response = sample. into_value ( ) ;
233- if response. uuid != request_id {
234- continue ;
235- }
236286
237287 let status_code = parse_status_from_header ( & response. header ) . unwrap_or ( 0 ) ;
288+ if debug_enabled {
289+ tracing:: debug!(
290+ target: "booster_sdk::rpc" ,
291+ service_topic = %service_topic,
292+ api_id,
293+ request_uuid = %request_id,
294+ response_uuid = %response. uuid,
295+ status_code,
296+ header = %preview_for_log( & response. header, 200 ) ,
297+ body = %preview_for_log( & response. body, 300 ) ,
298+ "recv rpc response"
299+ ) ;
300+ }
238301
239302 if status_code == -1 {
303+ if debug_enabled {
304+ tracing:: debug!(
305+ target: "booster_sdk::rpc" ,
306+ service_topic = %service_topic,
307+ api_id,
308+ request_uuid = %request_id,
309+ "ignoring intermediate status=-1"
310+ ) ;
311+ }
240312 continue ;
241313 }
242314
@@ -260,6 +332,16 @@ impl RpcClient {
260332 }
261333 Ok ( None ) => std:: thread:: sleep ( Duration :: from_millis ( 5 ) ) ,
262334 Err ( err) => {
335+ if debug_enabled {
336+ tracing:: warn!(
337+ target: "booster_sdk::rpc" ,
338+ service_topic = %service_topic,
339+ api_id,
340+ request_uuid = %request_id,
341+ error = %err,
342+ "rpc receive error"
343+ ) ;
344+ }
263345 return Err ( DdsError :: ReceiveFailed ( err. to_string ( ) ) . into ( ) ) ;
264346 }
265347 }
0 commit comments