Skip to content

Commit ba31105

Browse files
committed
Add LoggerScope
1 parent ec0b969 commit ba31105

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard};
5353
use crate::types::features::{InitFeatures, NodeFeatures};
5454
use crate::util::async_poll::{MaybeSend, MaybeSync};
5555
use crate::util::errors::APIError;
56-
use crate::util::logger::{Logger, WithContext};
56+
use crate::util::logger::{Logger, LoggerScope, WithContext};
5757
use crate::util::native_async::FutureSpawner;
5858
use crate::util::persist::{KVStore, MonitorName, MonitorUpdatingPersisterAsync};
5959
#[cfg(peer_storage)]
@@ -1426,6 +1426,8 @@ where
14261426
Some(monitor_state) => {
14271427
let monitor = &monitor_state.monitor;
14281428
let logger = WithChannelMonitor::from(&self.logger, &monitor, None);
1429+
let _scope = LoggerScope::new(&logger);
1430+
14291431
log_trace!(
14301432
logger,
14311433
"Updating ChannelMonitor to id {} for channel {}",

lightning/src/chain/channelmonitor.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3815,6 +3815,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38153815
}
38163816
}));
38173817
}
3818+
3819+
log_info_tls!(
3820+
"Recording counterparty fulfillment of HTLC id {:?} with preimage {} in channel monitor",
3821+
*claimed_htlc_id,
3822+
*claimed_preimage
3823+
);
38183824
self.counterparty_fulfilled_htlcs.insert(*claimed_htlc_id, *claimed_preimage);
38193825
}
38203826

lightning/src/util/logger.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
1616
use bitcoin::secp256k1::PublicKey;
1717

18+
use core::cell::RefCell;
1819
use core::cmp;
1920
use core::fmt;
2021
use core::ops::Deref;
@@ -261,15 +262,60 @@ impl<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone> fmt::Display fo
261262
}
262263
}
263264

265+
thread_local! {
266+
pub(crate) static TLS_LOGGER: RefCell<Option<&'static dyn Logger>> = RefCell::new(None);
267+
}
268+
269+
pub struct LoggerScope<'a> {
270+
_marker: std::marker::PhantomData<&'a ()>,
271+
}
272+
273+
impl<'a> LoggerScope<'a> {
274+
pub fn new<L: Logger + 'a>(logger: &'a L) -> Self {
275+
TLS_LOGGER.with(|cell| {
276+
let mut borrow = cell.borrow_mut();
277+
278+
// Prevent nested scopes
279+
if borrow.is_some() {
280+
panic!("LoggerScope already active in this thread");
281+
}
282+
283+
// Transmute is safe in practice because the RAII pattern ensures:
284+
// - The TLS logger reference is only used while the guard exists.
285+
// - The guard cannot outlive the logger (thanks to PhantomData<'a>).
286+
let logger_ref: &dyn Logger = logger;
287+
let logger_ref_static: &'static dyn Logger = unsafe { std::mem::transmute(logger_ref) };
288+
289+
*borrow = Some(logger_ref_static);
290+
});
291+
LoggerScope { _marker: std::marker::PhantomData }
292+
}
293+
}
294+
295+
impl<'a> Drop for LoggerScope<'a> {
296+
fn drop(&mut self) {
297+
TLS_LOGGER.with(|cell| {
298+
*cell.borrow_mut() = None;
299+
});
300+
}
301+
}
302+
264303
#[cfg(test)]
265304
mod tests {
266305
use crate::ln::types::ChannelId;
267306
use crate::sync::Arc;
268307
use crate::types::payment::PaymentHash;
269-
use crate::util::logger::{Level, Logger, WithContext};
308+
use crate::util::logger::{Level, Logger, LoggerScope, WithContext};
270309
use crate::util::test_utils::TestLogger;
271310
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
272311

312+
#[test]
313+
fn logger_scope() {
314+
let logger = TestLogger::new();
315+
let _scope = LoggerScope::new(&logger);
316+
log_info_tls!("Info")
317+
}
318+
273319
#[test]
274320
fn test_level_show() {
275321
assert_eq!("INFO", Level::Info.to_string());

lightning/src/util/macro_logger.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ macro_rules! log_info {
203203
)
204204
}
205205

206+
#[macro_export]
207+
macro_rules! log_info_tls {
208+
($($arg:tt)+) => {
209+
$crate::util::logger::TLS_LOGGER.with(|cell| {
210+
if let Some(logger) = &*cell.borrow() {
211+
log_info!(logger, $($arg)*);
212+
}
213+
})
214+
};
215+
}
216+
206217
/// Log at the `DEBUG` level.
207218
#[macro_export]
208219
macro_rules! log_debug {

lightning/src/util/persist.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::ln::types::ChannelId;
3535
use crate::sign::{ecdsa::EcdsaChannelSigner, EntropySource, SignerProvider};
3636
use crate::sync::Mutex;
3737
use crate::util::async_poll::{dummy_waker, AsyncResult, MaybeSend, MaybeSync};
38-
use crate::util::logger::Logger;
38+
use crate::util::logger::{Logger, LoggerScope};
3939
use crate::util::native_async::FutureSpawner;
4040
use crate::util::ser::{Readable, ReadableArgs, Writeable};
4141
use crate::util::wakers::Notifier;
@@ -1063,6 +1063,7 @@ where
10631063
Err(err) => return Err(err),
10641064
};
10651065

1066+
// let _scope = LoggerScope::new(&*self.logger); // DOES NOT WORK
10661067
monitor
10671068
.update_monitor(&update, &self.broadcaster, &self.fee_estimator, &self.logger)
10681069
.map_err(|e| {

0 commit comments

Comments
 (0)