Skip to content

Commit f4317cc

Browse files
committed
fixup! Add log spans using thread local storage in std
1 parent 5d2f39f commit f4317cc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lightning/src/util/test_utils.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ use crate::util::config::UserConfig;
5555
use crate::util::dyn_signer::{
5656
DynKeysInterface, DynKeysInterfaceTrait, DynPhantomKeysInterface, DynSigner,
5757
};
58+
use crate::util::logger::LoggerScope;
5859
use crate::util::logger::{Logger, Record};
5960
#[cfg(feature = "std")]
6061
use crate::util::mut_global::MutGlobal;
6162
use crate::util::persist::{KVStore, KVStoreSync, MonitorName};
6263
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
6364
use crate::util::test_channel_signer::{EnforcementState, TestChannelSigner};
65+
#[cfg(feature = "std")]
66+
use std::cell::RefCell;
6467

6568
use bitcoin::amount::Amount;
6669
use bitcoin::block::Block;
@@ -1759,6 +1762,26 @@ impl Logger for TestLogger {
17591762
}
17601763
}
17611764

1765+
#[cfg(feature = "std")]
1766+
thread_local! {
1767+
pub(crate) static THREAD_LOG_SCOPE: RefCell<Option<LoggerScope<'static>>> = RefCell::new(None);
1768+
}
1769+
1770+
/// Sets up a logging scope for the current thread with the given span name. This is useful to split up a long (test) function
1771+
/// into multiple scopes for easier log analysis.
1772+
#[macro_export]
1773+
macro_rules! test_scope {
1774+
($span:expr) => {
1775+
#[cfg(feature = "std")]
1776+
crate::util::test_utils::THREAD_LOG_SCOPE.with(|cell| {
1777+
// Drop old scope if it exists.
1778+
let _ = cell.borrow_mut().take();
1779+
// Create new scope.
1780+
*cell.borrow_mut() = Some(crate::util::logger::LoggerScope::new($span));
1781+
});
1782+
};
1783+
}
1784+
17621785
pub struct TestNodeSigner {
17631786
node_secret: SecretKey,
17641787
}

0 commit comments

Comments
 (0)