Skip to content

Commit ddcfc59

Browse files
committed
fix: logging consistency
1 parent dd35a60 commit ddcfc59

2 files changed

Lines changed: 18 additions & 32 deletions

File tree

sdk-libs/program-test/src/program_test/light_program_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ impl LightProgramTest {
7171
.expect("forester airdrop failed.");
7272

7373
if !config.skip_protocol_init {
74+
let restore_logs = context.config.no_logs;
7475
if context.config.skip_startup_logs {
7576
context.config.no_logs = true;
7677
}
7778
initialize_accounts(&mut context, &config, &keypairs).await?;
7879
if context.config.skip_startup_logs {
79-
context.config.no_logs = false;
80+
context.config.no_logs = restore_logs;
8081
}
8182
let batch_size = config
8283
.v2_state_tree_config

sdk-libs/program-test/src/program_test/rpc.rs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ impl Rpc for LightProgramTest {
3939
where
4040
Self: Sized,
4141
{
42-
unimplemented!()
42+
Err(RpcError::CustomError(
43+
"LightProgramTest::new is not supported in program-test context".into(),
44+
))
4345
}
4446

4547
fn get_payer(&self) -> &Keypair {
@@ -146,14 +148,7 @@ impl Rpc for LightProgramTest {
146148

147149
RpcError::TransactionError(x.err)
148150
})?;
149-
if !self.config.no_logs {
150-
#[cfg(debug_assertions)]
151-
{
152-
if std::env::var("RUST_BACKTRACE").is_ok() {
153-
println!("{}", _res.pretty_logs());
154-
}
155-
}
156-
}
151+
self.maybe_print_logs(_res.pretty_logs());
157152
}
158153
Ok(sig)
159154
}
@@ -164,20 +159,12 @@ impl Rpc for LightProgramTest {
164159
) -> Result<(Signature, Slot), RpcError> {
165160
let sig = *transaction.signatures.first().unwrap();
166161
let _res = self.context.send_transaction(transaction).map_err(|x| {
167-
#[cfg(not(debug_assertions))]
168-
{
169-
if self.config.log_failed_tx {
170-
println!("{}", x.meta.pretty_logs());
171-
}
162+
if self.config.log_failed_tx {
163+
println!("{}", x.meta.pretty_logs());
172164
}
173165
RpcError::TransactionError(x.err)
174166
})?;
175-
#[cfg(debug_assertions)]
176-
{
177-
if std::env::var("RUST_BACKTRACE").is_ok() {
178-
println!("{}", _res.pretty_logs());
179-
}
180-
}
167+
self.maybe_print_logs(_res.pretty_logs());
181168

182169
let slot = self.context.get_sysvar::<Clock>().slot;
183170
Ok((sig, slot))
@@ -303,6 +290,12 @@ impl Rpc for LightProgramTest {
303290
}
304291

305292
impl LightProgramTest {
293+
fn maybe_print_logs(&self, logs: impl std::fmt::Display) {
294+
if !self.config.no_logs && cfg!(debug_assertions) && std::env::var("RUST_BACKTRACE").is_ok()
295+
{
296+
println!("{}", logs);
297+
}
298+
}
306299
#[cfg(feature = "v2")]
307300
pub fn get_address_tree_v2(&self) -> TreeInfo {
308301
TreeInfo {
@@ -474,20 +467,12 @@ impl LightProgramTest {
474467
});
475468
// If transaction was successful, execute it.
476469
let _res = self.context.send_transaction(transaction).map_err(|x| {
477-
#[cfg(not(debug_assertions))]
478-
{
479-
if self.config.log_failed_tx {
480-
println!("{}", x.meta.pretty_logs());
481-
}
470+
if self.config.log_failed_tx {
471+
println!("{}", x.meta.pretty_logs());
482472
}
483473
RpcError::TransactionError(x.err)
484474
})?;
485-
#[cfg(debug_assertions)]
486-
{
487-
if std::env::var("RUST_BACKTRACE").is_ok() {
488-
println!("{}", _res.pretty_logs());
489-
}
490-
}
475+
self.maybe_print_logs(_res.pretty_logs());
491476

492477
let slot = self.get_slot().await?;
493478
let result = event.map(|event| (event, signature, slot));

0 commit comments

Comments
 (0)