Skip to content

Commit 1a5a03d

Browse files
ResuBakagefjon
andauthored
refactor(sdk): improve the log message source line reference (#2924)
# Description of Changes Currently with the function maybe_log_error to log errors we hide the real source line where the error happened. So to still have the same logic but with more accurate source line, I moved it to a macro to have the same abstraction for logging. # API and ABI breaking changes - # Expected complexity level and risk 1 # Testing - [ ] I have tested it my application and it now show the better source line of the log message --------- Signed-off-by: ResuBaka <ResuBaka@users.noreply.github.com> Co-authored-by: Phoebe Goldman <phoebe@goldman-tribe.org>
1 parent 080c501 commit 1a5a03d

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

crates/sdk/src/websocket.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ fn request_insert_auth_header(req: &mut http::Request<()>, token: Option<&str>)
200200
}
201201
}
202202

203+
/// If `res` evaluates to `Err(e)`, log a warning in the form `"{}: {:?}", $cause, e`.
204+
///
205+
/// Could be trivially written as a function, but macro-ifying it preserves the source location of the log.
206+
macro_rules! maybe_log_error {
207+
($cause:expr, $res:expr) => {
208+
if let Err(e) = $res {
209+
log::warn!("{}: {:?}", $cause, e);
210+
}
211+
};
212+
}
213+
203214
impl WsConnection {
204215
pub(crate) async fn connect(
205216
host: Uri,
@@ -241,12 +252,6 @@ impl WsConnection {
241252
WebSocketMessage::Binary(bsatn::to_vec(&msg).unwrap().into())
242253
}
243254

244-
fn maybe_log_error<T, U: std::fmt::Debug>(cause: &str, res: std::result::Result<T, U>) {
245-
if let Err(e) = res {
246-
log::warn!("{cause}: {e:?}");
247-
}
248-
}
249-
250255
async fn message_loop(
251256
mut self,
252257
incoming_messages: mpsc::UnboundedSender<ServerMessage<BsatnFormat>>,
@@ -303,9 +308,9 @@ impl WsConnection {
303308
},
304309

305310
Err(e) => {
306-
Self::maybe_log_error::<(), _>(
311+
maybe_log_error!(
307312
"Error reading message from read WebSocket stream",
308-
Err(e),
313+
Result::<(), _>::Err(e)
309314
);
310315
break;
311316
},
@@ -314,13 +319,13 @@ impl WsConnection {
314319
idle = false;
315320
record_metrics(bytes.len());
316321
match Self::parse_response(&bytes) {
317-
Err(e) => Self::maybe_log_error::<(), _>(
322+
Err(e) => maybe_log_error!(
318323
"Error decoding WebSocketMessage::Binary payload",
319-
Err(e),
324+
Result::<(), _>::Err(e)
320325
),
321-
Ok(msg) => Self::maybe_log_error(
326+
Ok(msg) => maybe_log_error!(
322327
"Error sending decoded message to incoming_messages queue",
323-
incoming_messages.unbounded_send(msg),
328+
incoming_messages.unbounded_send(msg)
324329
),
325330
}
326331
}
@@ -376,7 +381,7 @@ impl WsConnection {
376381
}
377382
}
378383
None => {
379-
Self::maybe_log_error("Error sending close frame", SinkExt::close(&mut self.sock).await);
384+
maybe_log_error!("Error sending close frame", SinkExt::close(&mut self.sock).await);
380385
outgoing_messages = None;
381386
}
382387
},

0 commit comments

Comments
 (0)