Skip to content

Commit ccbeb17

Browse files
authored
Use track_caller approach for logging from a method (#964)
1 parent 7f09829 commit ccbeb17

File tree

14 files changed

+113
-78
lines changed

14 files changed

+113
-78
lines changed

AGENTS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ If changes are needed in these files, that must happen in the separate Positron
9595

9696
- Use fully qualified result types (`anyhow::Result`) instead of importing them.
9797

98+
- You can log `Result::Err` by using the `.log_err()` method from the extension trait `stdext::ResultExt`. Add some `.context()` if that would be helpful, but never do it for errors that are quite unexpected, such as from `.send()` to a channel (that would be too verbose).
99+
98100
- When writing tests, prefer simple assertion macros without custom error messages:
99101
- Use `assert_eq!(actual, expected);` instead of `assert_eq!(actual, expected, "custom message");`
100102
- Use `assert!(condition);` instead of `assert!(condition, "custom message");`

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/amalthea/src/comm/comm_manager.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crossbeam::channel::Select;
1212
use crossbeam::channel::Sender;
1313
use log::info;
1414
use log::warn;
15-
use stdext::result::ResultOrLog;
15+
use stdext::result::ResultExt;
1616
use stdext::spawn;
1717

1818
use crate::comm::comm_channel::CommMsg;
@@ -161,9 +161,7 @@ impl CommManager {
161161
if let Some(index) = index {
162162
// Notify the comm that it's been closed
163163
let comm = self.open_comms.get(index).unwrap();
164-
comm.incoming_tx
165-
.send(CommMsg::Close)
166-
.or_log_error("Failed to send comm_close to comm.");
164+
comm.incoming_tx.send(CommMsg::Close).log_err();
167165

168166
// Remove it from our list of open comms
169167
self.open_comms.remove(index);

crates/amalthea/src/socket/shell.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::sync::Mutex;
1414
use crossbeam::channel::Receiver;
1515
use crossbeam::channel::Sender;
1616
use futures::executor::block_on;
17-
use stdext::result::ResultOrLog;
17+
use stdext::result::ResultExt;
1818

1919
use crate::comm::comm_channel::comm_rpc_message;
2020
use crate::comm::comm_channel::Comm;
@@ -429,10 +429,7 @@ impl Shell {
429429
// comm has been opened
430430
self.comm_manager_tx
431431
.send(CommManagerEvent::Opened(comm_socket.clone(), comm_data))
432-
.or_log_warning(&format!(
433-
"Failed to send '{}' comm open notification to listener thread",
434-
comm_socket.comm_name
435-
));
432+
.log_err();
436433

437434
// If the comm wraps a server, send notification once the server is ready to
438435
// accept connections. This also sends back the port number to connect on. Failing

crates/ark/src/dap/dap.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use amalthea::comm::server_comm::ServerStartedMessage;
1515
use amalthea::language::server_handler::ServerHandler;
1616
use crossbeam::channel::Sender;
1717
use harp::object::RObject;
18-
use stdext::log_error;
18+
use stdext::result::ResultExt;
1919
use stdext::spawn;
2020

2121
use crate::dap::dap_r_main::FrameInfo;
@@ -134,14 +134,15 @@ impl Dap {
134134

135135
if self.is_debugging {
136136
if let Some(tx) = &self.backend_events_tx {
137-
log_error!(tx.send(DapBackendEvent::Stopped(DapStoppedEvent { preserve_focus })));
137+
tx.send(DapBackendEvent::Stopped(DapStoppedEvent { preserve_focus }))
138+
.log_err();
138139
}
139140
} else {
140141
if let Some(tx) = &self.comm_tx {
141142
// Ask frontend to connect to the DAP
142143
log::trace!("DAP: Sending `start_debug` event");
143144
let msg = amalthea::comm_rpc_message!("start_debug");
144-
log_error!(tx.send(msg));
145+
tx.send(msg).log_err();
145146
}
146147

147148
self.is_debugging = true;
@@ -162,7 +163,7 @@ impl Dap {
162163
// terminate the debugging session and disconnect.
163164
if let Some(tx) = &self.backend_events_tx {
164165
log::trace!("DAP: Sending `stop_debug` event");
165-
log_error!(tx.send(DapBackendEvent::Terminated));
166+
tx.send(DapBackendEvent::Terminated).log_err();
166167
}
167168
}
168169
// else: If not connected to a frontend, the DAP client should

crates/ark/src/dap/dap_r_main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use libr::INTSXP;
2727
use libr::SET_INTEGER_ELT;
2828
use libr::SEXP;
2929
use libr::VECTOR_ELT;
30-
use stdext::log_error;
30+
use stdext::result::ResultExt;
3131

3232
use crate::dap::dap::DapBackendEvent;
3333
use crate::dap::Dap;
@@ -180,7 +180,7 @@ impl RMainDap {
180180
pub fn send_dap(&self, event: DapBackendEvent) {
181181
let dap = self.dap.lock().unwrap();
182182
if let Some(tx) = &dap.backend_events_tx {
183-
log_error!(tx.send(event));
183+
tx.send(event).log_err();
184184
}
185185
}
186186

crates/ark/src/dap/dap_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use dap::requests::*;
2828
use dap::responses::*;
2929
use dap::server::ServerOutput;
3030
use dap::types::*;
31-
use stdext::result::ResultOrLog;
31+
use stdext::result::ResultExt;
3232
use stdext::spawn;
3333

3434
use super::dap::Dap;
@@ -73,7 +73,7 @@ pub fn start_dap(
7373
// Send the port back to `Shell` and eventually out to the frontend so it can connect
7474
server_started_tx
7575
.send(ServerStartedMessage::new(port))
76-
.or_log_error("DAP: Can't send init notification");
76+
.log_err();
7777

7878
loop {
7979
log::trace!("DAP: Waiting for client");

crates/ark/src/data_explorer/r_data_explorer.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ use amalthea::socket::comm::CommInitiator;
6666
use amalthea::socket::comm::CommSocket;
6767
use anyhow::anyhow;
6868
use anyhow::bail;
69+
use anyhow::Context;
6970
use crossbeam::channel::unbounded;
7071
use crossbeam::channel::Sender;
7172
use crossbeam::select;
@@ -84,7 +85,7 @@ use libr::*;
8485
use serde::Deserialize;
8586
use serde::Serialize;
8687
use stdext::local;
87-
use stdext::result::ResultOrLog;
88+
use stdext::result::ResultExt;
8889
use stdext::spawn;
8990
use stdext::unwrap;
9091
use tracing::Instrument;
@@ -236,7 +237,7 @@ impl RDataExplorer {
236237
// the schema
237238
comm_manager_tx
238239
.send(CommManagerEvent::Closed(comm.comm_id))
239-
.or_log_error("Error sending comm closed event")
240+
.log_err();
240241
},
241242
}
242243
});
@@ -680,7 +681,8 @@ impl RDataExplorer {
680681
handle_columns_profiles_requests(params, comm)
681682
.instrument(tracing::info_span!("get_columns_profile", ns = id))
682683
.await
683-
.or_log_error("Unable to handle get_columns_profile");
684+
.context("Unable to handle get_columns_profile")
685+
.log_err();
684686
});
685687
}
686688

crates/ark/src/interface.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ use libr::SEXP;
8787
use once_cell::sync::Lazy;
8888
use regex::Regex;
8989
use serde_json::json;
90-
use stdext::result::ResultOrLog;
90+
use stdext::result::ResultExt;
9191
use stdext::*;
9292
use tokio::sync::mpsc::UnboundedReceiver as AsyncUnboundedReceiver;
9393
use uuid::Uuid;
@@ -440,7 +440,8 @@ impl RMain {
440440
// Optionally run a frontend specified R startup script (after harp init)
441441
if let Some(file) = &startup_file {
442442
harp::source(file)
443-
.or_log_error(&format!("Failed to source startup file '{file}' due to"));
443+
.context(format!("Failed to source startup file '{file}' due to"))
444+
.log_err();
444445
}
445446

446447
// Initialize support functions (after routine registration, after r_task initialization)

crates/ark/src/lsp/backend.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ use amalthea::comm::server_comm::ServerStartMessage;
1414
use amalthea::comm::server_comm::ServerStartedMessage;
1515
use amalthea::comm::ui_comm::ShowMessageParams as UiShowMessageParams;
1616
use amalthea::comm::ui_comm::UiFrontendEvent;
17+
use anyhow::Context;
1718
use crossbeam::channel::Sender;
1819
use serde_json::Value;
19-
use stdext::result::ResultOrLog;
20+
use stdext::result::ResultExt;
2021
use tokio::net::TcpListener;
2122
use tokio::runtime::Runtime;
2223
use tokio::sync::mpsc::unbounded_channel as tokio_unbounded_channel;
@@ -501,7 +502,8 @@ pub fn start_lsp(
501502
// Send the port back to `Shell` and eventually out to the frontend so it can connect
502503
server_started_tx
503504
.send(ServerStartedMessage::new(port))
504-
.or_log_error("LSP: Can't send server started notification");
505+
.context("LSP: Can't send server started notification")
506+
.log_err();
505507

506508
log::trace!("LSP: Waiting for client");
507509
let (stream, address) = listener.accept().await.unwrap();

0 commit comments

Comments
 (0)