///
/// This is not exported to bindings users as lifetimes are problematic and there's little reason
/// for this to be used downstream anyway.
-pub struct WithContext<'a, L: Deref> where L::Target: Logger {
+pub struct WithContext<'a, L: Deref>
+where
+ L::Target: Logger,
+{
/// The logger to delegate to after adding context to the record.
logger: &'a L,
/// The node id of the peer pertaining to the logged record.
/// The channel id of the channel pertaining to the logged record.
channel_id: Option<ChannelId>,
/// The payment hash of the payment pertaining to the logged record.
- payment_hash: Option<PaymentHash>
+ payment_hash: Option<PaymentHash>,
}
-impl<'a, L: Deref> Logger for WithContext<'a, L> where L::Target: Logger {
+impl<'a, L: Deref> Logger for WithContext<'a, L>
+where
+ L::Target: Logger,
+{
fn log(&self, mut record: Record) {
if self.peer_id.is_some() {
record.peer_id = self.peer_id
}
}
-impl<'a, L: Deref> WithContext<'a, L> where L::Target: Logger {
+impl<'a, L: Deref> WithContext<'a, L>
+where
+ L::Target: Logger,
+{
/// Wraps the given logger, providing additional context to any logged records.
- pub fn from(logger: &'a L, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>, payment_hash: Option<PaymentHash>) -> Self {
- WithContext {
- logger,
- peer_id,
- channel_id,
- payment_hash,
- }
+ pub fn from(
+ logger: &'a L, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>,
+ payment_hash: Option<PaymentHash>,
+ ) -> Self {
+ WithContext { logger, peer_id, channel_id, payment_hash }
}
}
#[cfg(test)]
mod tests {
- use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
use crate::ln::types::ChannelId;
use crate::ln::PaymentHash;
- use crate::util::logger::{Logger, Level, WithContext};
- use crate::util::test_utils::TestLogger;
use crate::sync::Arc;
+ use crate::util::logger::{Level, Logger, WithContext};
+ use crate::util::test_utils::TestLogger;
+ use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
#[test]
fn test_level_show() {
}
struct WrapperLog {
- logger: Arc<dyn Logger>
+ logger: Arc<dyn Logger>,
}
impl WrapperLog {
fn new(logger: Arc<dyn Logger>) -> WrapperLog {
- WrapperLog {
- logger,
- }
+ WrapperLog { logger }
}
fn call_macros(&self) {
#[test]
fn test_logging_macros() {
let logger = TestLogger::new();
- let logger : Arc<dyn Logger> = Arc::new(logger);
+ let logger: Arc<dyn Logger> = Arc::new(logger);
let wrapper = WrapperLog::new(Arc::clone(&logger));
wrapper.call_macros();
}
let secp_ctx = Secp256k1::new();
let pk = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
let payment_hash = PaymentHash([0; 32]);
- let context_logger = WithContext::from(&logger, Some(pk), Some(ChannelId([0; 32])), Some(payment_hash));
+ let context_logger =
+ WithContext::from(&logger, Some(pk), Some(ChannelId([0; 32])), Some(payment_hash));
log_error!(context_logger, "This is an error");
log_warn!(context_logger, "This is an error");
log_debug!(context_logger, "This is an error");
log_gossip!(context_logger, "This is an error");
log_info!(context_logger, "This is an error");
logger.assert_log_context_contains(
- "lightning::util::logger::tests", Some(pk), Some(ChannelId([0;32])), 6
+ "lightning::util::logger::tests",
+ Some(pk),
+ Some(ChannelId([0; 32])),
+ 6,
);
}
let secp_ctx = Secp256k1::new();
let pk = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
let payment_hash = PaymentHash([0; 32]);
- let context_logger = &WithContext::from(&logger, None, Some(ChannelId([0; 32])), Some(payment_hash));
+ let context_logger =
+ &WithContext::from(&logger, None, Some(ChannelId([0; 32])), Some(payment_hash));
let full_context_logger = WithContext::from(&context_logger, Some(pk), None, None);
log_error!(full_context_logger, "This is an error");
log_warn!(full_context_logger, "This is an error");
log_gossip!(full_context_logger, "This is an error");
log_info!(full_context_logger, "This is an error");
logger.assert_log_context_contains(
- "lightning::util::logger::tests", Some(pk), Some(ChannelId([0;32])), 6
+ "lightning::util::logger::tests",
+ Some(pk),
+ Some(ChannelId([0; 32])),
+ 6,
);
}