From fd13c82aa23e3fec10ab2ce344e28e7e70e55dff Mon Sep 17 00:00:00 2001 From: Jiangeng Dong Date: Wed, 31 May 2023 14:20:54 -0700 Subject: [PATCH] int: replace the (e)println! with log --- r2r/Cargo.toml | 1 + r2r/src/action_clients.rs | 22 +++++++++---------- r2r/src/action_clients_untyped.rs | 20 ++++++++--------- r2r/src/action_servers.rs | 36 +++++++++++++++---------------- r2r/src/clients.rs | 12 +++++------ r2r/src/clocks.rs | 4 ++-- r2r/src/nodes.rs | 14 ++++++------ r2r/src/parameters.rs | 2 +- r2r/src/publishers.rs | 14 ++++++------ r2r/src/services.rs | 2 +- r2r/src/subscribers.rs | 6 +++--- r2r/src/utils.rs | 2 +- 12 files changed, 68 insertions(+), 67 deletions(-) diff --git a/r2r/Cargo.toml b/r2r/Cargo.toml index 913502d..b474f80 100644 --- a/r2r/Cargo.toml +++ b/r2r/Cargo.toml @@ -24,6 +24,7 @@ r2r_msg_gen = { path = "../r2r_msg_gen", version = "0.3.11" } r2r_actions = { path = "../r2r_actions", version = "0.3.10" } uuid = { version = "1.2.2", features = ["serde", "v4"] } futures = "0.3.25" +log = "0.4.18" [dev-dependencies] serde_json = "1.0.89" diff --git a/r2r/src/action_clients.rs b/r2r/src/action_clients.rs index f37834c..49479c6 100644 --- a/r2r/src/action_clients.rs +++ b/r2r/src/action_clients.rs @@ -164,7 +164,7 @@ where }); Ok(future) } else { - eprintln!("coult not send goal request {}", result); + log::error!("could not send goal request {}", result); Err(Error::from_rcl_error(result)) } } @@ -260,7 +260,7 @@ where }); Ok(future) } else { - eprintln!("coult not send goal request {}", result); + log::error!("could not send goal request {}", result); Err(Error::from_rcl_error(result)) } } @@ -300,7 +300,7 @@ where match sender.send((accept, stamp)) { Ok(()) => {} Err(e) => { - println!("error sending to action client: {:?}", e); + log::debug!("error sending to action client: {:?}", e); } } } else { @@ -310,7 +310,7 @@ where .map(|(id, _)| id.to_string()) .collect::>() .join(","); - eprintln!( + log::error!( "no such req id: {}, we have [{}], ignoring", request_id.sequence_number, we_have ); @@ -339,7 +339,7 @@ where let (_, sender) = self.cancel_response_channels.swap_remove(idx); let response = action_msgs::srv::CancelGoal::Response::from_native(&response_msg); if let Err(e) = sender.send(response) { - eprintln!("warning: could not send cancel response msg ({:?})", e) + log::error!("warning: could not send cancel response msg ({:?})", e) } } else { let we_have: String = self @@ -348,7 +348,7 @@ where .map(|(id, _)| id.to_string()) .collect::>() .join(","); - eprintln!( + log::error!( "no such req id: {}, we have [{}], ignoring", request_id.sequence_number, we_have ); @@ -370,7 +370,7 @@ where .find(|(uuid, _)| uuid == &msg_uuid) { if let Err(e) = sender.try_send(feedback) { - eprintln!("warning: could not send feedback msg ({})", e) + log::error!("warning: could not send feedback msg ({})", e) } } } @@ -426,7 +426,7 @@ where match sender.send((status, result)) { Ok(()) => {} Err(e) => { - println!("error sending result to action client: {:?}", e); + log::debug!("error sending result to action client: {:?}", e); } } } @@ -437,7 +437,7 @@ where .map(|(id, _)| id.to_string()) .collect::>() .join(","); - eprintln!( + log::error!( "no such req id: {}, we have [{}], ignoring", request_id.sequence_number, we_have ); @@ -461,7 +461,7 @@ where if result == RCL_RET_OK as i32 { self.result_requests.push((seq_no, uuid)); } else { - eprintln!("coult not send request {}", result); + log::error!("could not send request {}", result); } } @@ -576,7 +576,7 @@ pub fn action_server_available_helper( if result == RCL_RET_OK as i32 { Ok(avail) } else { - eprintln!("coult not check if action server is available {}", result); + log::error!("could not check if action server is available {}", result); Err(Error::from_rcl_error(result)) } } diff --git a/r2r/src/action_clients_untyped.rs b/r2r/src/action_clients_untyped.rs index 6e62785..3239f46 100644 --- a/r2r/src/action_clients_untyped.rs +++ b/r2r/src/action_clients_untyped.rs @@ -153,7 +153,7 @@ impl ActionClientUntyped { }); Ok(future) } else { - eprintln!("coult not send goal request {}", result); + log::error!("could not send goal request {}", result); Err(Error::from_rcl_error(result)) } } @@ -227,7 +227,7 @@ impl WrappedActionClientUntyped { }); Ok(future) } else { - eprintln!("coult not send goal request {}", result); + log::error!("could not send goal request {}", result); Err(Error::from_rcl_error(result)) } } @@ -262,7 +262,7 @@ impl ActionClient_ for WrappedActionClientUntyped { match sender.send((accept, stamp)) { Ok(()) => {} Err(e) => { - println!("error sending to action client: {:?}", e); + log::debug!("error sending to action client: {:?}", e); } } } else { @@ -272,7 +272,7 @@ impl ActionClient_ for WrappedActionClientUntyped { .map(|(id, _)| id.to_string()) .collect::>() .join(","); - eprintln!( + log::error!( "no such req id: {}, we have [{}], ignoring", request_id.sequence_number, we_have ); @@ -301,7 +301,7 @@ impl ActionClient_ for WrappedActionClientUntyped { let (_, sender) = self.cancel_response_channels.swap_remove(idx); let response = action_msgs::srv::CancelGoal::Response::from_native(&response_msg); if let Err(e) = sender.send(response) { - eprintln!("warning: could not send cancel response msg ({:?})", e) + log::error!("warning: could not send cancel response msg ({:?})", e) } } else { let we_have: String = self @@ -310,7 +310,7 @@ impl ActionClient_ for WrappedActionClientUntyped { .map(|(id, _)| id.to_string()) .collect::>() .join(","); - eprintln!( + log::error!( "no such req id: {}, we have [{}], ignoring", request_id.sequence_number, we_have ); @@ -332,7 +332,7 @@ impl ActionClient_ for WrappedActionClientUntyped { .find(|(uuid, _)| uuid == &msg_uuid) { if let Err(e) = sender.try_send(feedback) { - eprintln!("warning: could not send feedback msg ({})", e) + log::error!("warning: could not send feedback msg ({})", e) } } } @@ -385,7 +385,7 @@ impl ActionClient_ for WrappedActionClientUntyped { match sender.send((status, result)) { Ok(()) => {} Err(e) => { - println!("error sending result to action client: {:?}", e); + log::debug!("error sending result to action client: {:?}", e); } } } @@ -396,7 +396,7 @@ impl ActionClient_ for WrappedActionClientUntyped { .map(|(id, _)| id.to_string()) .collect::>() .join(","); - eprintln!( + log::error!( "no such req id: {}, we have [{}], ignoring", request_id.sequence_number, we_have ); @@ -417,7 +417,7 @@ impl ActionClient_ for WrappedActionClientUntyped { if result == RCL_RET_OK as i32 { self.result_requests.push((seq_no, uuid)); } else { - eprintln!("coult not send request {}", result); + log::error!("could not send request {}", result); } } diff --git a/r2r/src/action_servers.rs b/r2r/src/action_servers.rs index 3d0f397..80d2f7d 100644 --- a/r2r/src/action_servers.rs +++ b/r2r/src/action_servers.rs @@ -44,13 +44,13 @@ impl ActionServerCancelRequest { /// Accepts the cancel request. The action server should now cancel the corresponding goal. pub fn accept(self) { if self.response_sender.send((self.uuid, true)).is_err() { - eprintln!("warning: could not send goal canellation accept msg") + log::error!("warning: could not send goal canellation accept msg") } } /// Rejects the cancel request. pub fn reject(self) { if self.response_sender.send((self.uuid, false)).is_err() { - eprintln!("warning: could not send goal cancellation rejection") + log::error!("warning: could not send goal cancellation rejection") } } } @@ -195,7 +195,7 @@ where let ret = unsafe { rcl_action_goal_handle_get_status(*handle, &mut state) }; if ret != RCL_RET_OK as i32 { - println!("action server: Failed to get goal handle state: {}", ret); + log::debug!("action server: Failed to get goal handle state: {}", ret); return Err(Error::from_rcl_error(ret)); } return Ok(state == action_msgs::msg::GoalStatus::STATUS_CANCELING as u8); @@ -213,7 +213,7 @@ where }; if ret != RCL_RET_OK as i32 { - println!( + log::debug!( "action server: could not cancel goal: {}", Error::from_rcl_error(ret) ); @@ -239,7 +239,7 @@ where unsafe { rcl_action_server_goal_exists(self.handle(), &*goal_info_native) }; if !goal_exists { - println!("tried to publish result without a goal"); + log::debug!("tried to publish result without a goal"); return Err(Error::RCL_RET_ACTION_GOAL_HANDLE_INVALID); } @@ -286,7 +286,7 @@ where }); } Err(oneshot::Canceled) => { - eprintln!("Warning, cancel request not handled!"); + log::error!("Warning, cancel request not handled!"); return false; // skip this request. } } @@ -325,7 +325,7 @@ where }; if ret != RCL_RET_OK as i32 { - println!("action server: could send cancel response. {}", ret); + log::debug!("action server: could send cancel response. {}", ret); } } } @@ -364,7 +364,7 @@ where // send out request. if let Err(e) = self.goal_request_sender.try_send(gr) { - eprintln!("warning: could not send service request ({})", e) + log::error!("warning: could not send service request ({})", e) } } @@ -392,7 +392,7 @@ where }; if ret != RCL_RET_OK as i32 { - println!("action server: could not process cancel request. {}", ret); + log::debug!("action server: could not process cancel request. {}", ret); return; } @@ -414,7 +414,7 @@ where }; match cancel_sender.try_send(cr) { Err(_) => { - eprintln!("warning: could not send goal cancellation request"); + log::error!("warning: could not send goal cancellation request"); None } _ => Some(r), @@ -438,12 +438,12 @@ where rcl_action_expire_goals(&self.rcl_handle, &mut *goal_info, 1, &mut num_expired) }; if ret != RCL_RET_OK as i32 { - println!("action server: could not expire goal."); + log::debug!("action server: could not expire goal."); return; } let gi = action_msgs::msg::GoalInfo::from_native(&goal_info); let uuid = uuid_msg_to_uuid(&gi.goal_id); - println!("goal expired: {} - {}", uuid, num_expired); + log::debug!("goal expired: {} - {}", uuid, num_expired); // todo // self.goals.remove(&uuid); self.result_msgs.remove(&uuid); @@ -456,7 +456,7 @@ where let mut status = rcl_action_get_zero_initialized_goal_status_array(); let ret = rcl_action_get_goal_status_array(&self.rcl_handle, &mut status); if ret != RCL_RET_OK as i32 { - println!( + log::debug!( "action server: failed to get goal status array: {}", Error::from_rcl_error(ret) ); @@ -467,7 +467,7 @@ where &status as *const _ as *const std::os::raw::c_void, ); if ret != RCL_RET_OK as i32 { - println!( + log::debug!( "action server: failed to publish status: {}", Error::from_rcl_error(ret) ); @@ -490,7 +490,7 @@ where rcl_action_send_result_response(&self.rcl_handle, &mut req, msg.void_ptr_mut()) }; if ret != RCL_RET_OK as i32 { - println!( + log::debug!( "action server: could send result request response. {}", Error::from_rcl_error(ret) ); @@ -551,7 +551,7 @@ where }; if ret != RCL_RET_OK as i32 { - println!( + log::debug!( "action server: could send result request response. {}", Error::from_rcl_error(ret) ); @@ -624,7 +624,7 @@ where }; if ret != RCL_RET_OK as i32 { - eprintln!("coult not publish {}", Error::from_rcl_error(ret)); + log::error!("could not publish {}", Error::from_rcl_error(ret)); } Ok(()) // todo: error codes } @@ -651,7 +651,7 @@ where unsafe { rcl_action_server_goal_exists(action_server.handle(), &*goal_info_native) }; if !goal_exists { - println!("tried to publish result without a goal"); + log::debug!("tried to publish result without a goal"); return Err(Error::RCL_RET_ACTION_GOAL_HANDLE_INVALID); } diff --git a/r2r/src/clients.rs b/r2r/src/clients.rs index 9ff2ade..7bea2eb 100644 --- a/r2r/src/clients.rs +++ b/r2r/src/clients.rs @@ -100,7 +100,7 @@ where // instead of "canceled" we return invalid client. Ok(receiver.map_err(|_| Error::RCL_RET_CLIENT_INVALID)) } else { - eprintln!("coult not send request {}", result); + log::error!("could not send request {}", result); Err(Error::from_rcl_error(result)) } } @@ -127,7 +127,7 @@ impl UntypedClient_ { // instead of "canceled" we return invalid client. Ok(receiver.map_err(|_| Error::RCL_RET_CLIENT_INVALID)) } else { - eprintln!("coult not send request {}", result); + log::error!("could not send request {}", result); Err(Error::from_rcl_error(result)) } } @@ -181,7 +181,7 @@ where match sender.send(response) { Ok(()) => {} Err(e) => { - println!("error sending to client: {:?}", e); + log::debug!("error sending to client: {:?}", e); } } } else { @@ -191,7 +191,7 @@ where .map(|(id, _)| id.to_string()) .collect::>() .join(","); - eprintln!( + log::error!( "no such req id: {}, we have [{}], ignoring", request_id.sequence_number, we_have ); @@ -267,7 +267,7 @@ impl Client_ for UntypedClient_ { match sender.send(response) { Ok(()) => {} Err(e) => { - println!("error sending to client: {:?}", e); + log::debug!("error sending to client: {:?}", e); } } } else { @@ -277,7 +277,7 @@ impl Client_ for UntypedClient_ { .map(|(id, _)| id.to_string()) .collect::>() .join(","); - eprintln!( + log::error!( "no such req id: {}, we have [{}], ignoring", request_id.sequence_number, we_have ); diff --git a/r2r/src/clocks.rs b/r2r/src/clocks.rs index 9ca7cb5..993e164 100644 --- a/r2r/src/clocks.rs +++ b/r2r/src/clocks.rs @@ -43,7 +43,7 @@ impl Clock { ) }; if ret != RCL_RET_OK as i32 { - eprintln!("could not create {:?} clock: {}", ct, ret); + log::error!("could not create {:?} clock: {}", ct, ret); return Err(Error::from_rcl_error(ret)); } @@ -60,7 +60,7 @@ impl Clock { let ret = unsafe { rcl_clock_get_now(&mut *self.clock_handle, &mut tp) }; if ret != RCL_RET_OK as i32 { - eprintln!("could not create steady clock: {}", ret); + log::error!("could not create steady clock: {}", ret); return Err(Error::from_rcl_error(ret)); } diff --git a/r2r/src/nodes.rs b/r2r/src/nodes.rs index 9d4e020..bc26f4b 100644 --- a/r2r/src/nodes.rs +++ b/r2r/src/nodes.rs @@ -94,7 +94,7 @@ impl Node { let ret = unsafe { rcl_arguments_get_param_overrides(&ctx.global_arguments, params.as_mut()) }; if ret != RCL_RET_OK as i32 { - eprintln!("could not read parameters: {}", ret); + log::error!("could not read parameters: {}", ret); return Err(Error::from_rcl_error(ret)); } @@ -192,7 +192,7 @@ impl Node { node.load_params()?; Ok(node) } else { - eprintln!("could not create node{}", res); + log::error!("could not create node{}", res); Err(Error::from_rcl_error(res)) } } @@ -242,7 +242,7 @@ impl Node { // if the value changed, send out new value on parameter event stream if changed { if let Err(e) = event_tx.try_send((p.name.clone(), val)) { - println!("Warning: could not send parameter event ({}).", e); + log::debug!("Warning: could not send parameter event ({}).", e); } } } @@ -528,7 +528,7 @@ impl Node { ) }; if ret != RCL_RET_OK as i32 { - eprintln!("could not create steady clock: {}", ret); + log::error!("could not create steady clock: {}", ret); return Err(Error::from_rcl_error(ret)); } let mut clock_handle = Box::new(unsafe { clock_handle.assume_init() }); @@ -925,7 +925,7 @@ impl Node { ) }; if ret != RCL_RET_OK as i32 { - eprintln!("could not get topic names and types {}", ret); + log::error!("could not get topic names and types {}", ret); return Err(Error::from_rcl_error(ret)); } @@ -971,7 +971,7 @@ impl Node { }; if ret != RCL_RET_OK as i32 { - eprintln!("could not create timer: {}", ret); + log::error!("could not create timer: {}", ret); return Err(Error::from_rcl_error(ret)); } @@ -1023,7 +1023,7 @@ impl Timer_ { return true; } if e.is_full() { - println!( + log::debug!( "Warning: timer tick not handled in time - no wakeup will occur" ); } diff --git a/r2r/src/parameters.rs b/r2r/src/parameters.rs index 0402b8f..4ea1b10 100644 --- a/r2r/src/parameters.rs +++ b/r2r/src/parameters.rs @@ -90,7 +90,7 @@ impl ParameterValue { 8 => ParameterValue::DoubleArray(msg.double_array_value), 9 => ParameterValue::StringArray(msg.string_array_value), _ => { - println!("warning: malformed parametervalue message"); + log::debug!("warning: malformed parametervalue message"); ParameterValue::NotSet } } diff --git a/r2r/src/publishers.rs b/r2r/src/publishers.rs index 2c0f0b9..a860e21 100644 --- a/r2r/src/publishers.rs +++ b/r2r/src/publishers.rs @@ -128,7 +128,7 @@ impl PublisherUntyped { if result == RCL_RET_OK as i32 { Ok(()) } else { - eprintln!("coult not publish {}", result); + log::error!("could not publish {}", result); Err(Error::from_rcl_error(result)) } } @@ -160,7 +160,7 @@ where if result == RCL_RET_OK as i32 { Ok(()) } else { - eprintln!("coult not publish {}", result); + log::error!("could not publish {}", result); Err(Error::from_rcl_error(result)) } } @@ -182,7 +182,7 @@ where }; if ret != RCL_RET_OK as i32 { // TODO: Switch to logging library - eprintln!("Failed getting loaned message"); + log::error!("Failed getting loaned message"); return Err(Error::from_rcl_error(ret)) } @@ -208,11 +208,11 @@ where Ok(msg) } else { static LOG_LOANED_ERROR: Once = Once::new(); - LOG_LOANED_ERROR.call_once(|| { + LOG_LOANED_ERROR.call_once(|| { // TODO: Switch to logging library - eprintln!("Currently used middleware can't loan messages. Local allocator will be used."); + log::error!("Currently used middleware can't loan messages. Local allocator will be used."); }); - + Ok(WrappedNativeMsg::::new()) } } @@ -250,7 +250,7 @@ where if result == RCL_RET_OK as i32 { Ok(()) } else { - eprintln!("could not publish native {}", result); + log::error!("could not publish native {}", result); Err(Error::from_rcl_error(result)) } } diff --git a/r2r/src/services.rs b/r2r/src/services.rs index f3c4068..79529a2 100644 --- a/r2r/src/services.rs +++ b/r2r/src/services.rs @@ -104,7 +104,7 @@ where if e.is_disconnected() { return true; } - eprintln!("warning: could not send service request ({})", e) + log::error!("warning: could not send service request ({})", e) } } // TODO handle failure. false diff --git a/r2r/src/subscribers.rs b/r2r/src/subscribers.rs index d672063..4184ea0 100644 --- a/r2r/src/subscribers.rs +++ b/r2r/src/subscribers.rs @@ -63,7 +63,7 @@ where // user dropped the handle to the stream, signal removal. return true; } - println!("error {:?}", e) + log::debug!("error {:?}", e) } } false @@ -140,7 +140,7 @@ where // user dropped the handle to the stream, signal removal. return true; } - eprintln!("error {:?}", e) + log::error!("error {:?}", e) } false } @@ -176,7 +176,7 @@ impl Subscriber_ for UntypedSubscriber { // user dropped the handle to the stream, signal removal. return true; } - println!("error {:?}", e) + log::debug!("error {:?}", e) } } false diff --git a/r2r/src/utils.rs b/r2r/src/utils.rs index 9f738e8..ca7c139 100644 --- a/r2r/src/utils.rs +++ b/r2r/src/utils.rs @@ -20,7 +20,7 @@ pub fn log(msg: &str, logger_name: &str, file: &str, line: u32, severity: LogSev if !is_init { let ret = unsafe { rcutils_logging_initialize() }; if ret != RCL_RET_OK as i32 { - eprintln!("could not create logging system (Err: {})", ret); + log::error!("could not create logging system (Err: {})", ret); return; } }