rustfmt: Run on util/logger.rs

This commit is contained in:
Elias Rohrer 2024-09-18 09:40:16 +02:00
parent ea22956bf7
commit 9c6c9abb0e
No known key found for this signature in database
GPG key ID: 36153082BDF676FD

View file

@ -169,7 +169,10 @@ pub trait Logger {
/// ///
/// This is not exported to bindings users as lifetimes are problematic and there's little reason /// This is not exported to bindings users as lifetimes are problematic and there's little reason
/// for this to be used downstream anyway. /// 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. /// The logger to delegate to after adding context to the record.
logger: &'a L, logger: &'a L,
/// The node id of the peer pertaining to the logged record. /// The node id of the peer pertaining to the logged record.
@ -177,10 +180,13 @@ pub struct WithContext<'a, L: Deref> where L::Target: Logger {
/// The channel id of the channel pertaining to the logged record. /// The channel id of the channel pertaining to the logged record.
channel_id: Option<ChannelId>, channel_id: Option<ChannelId>,
/// The payment hash of the payment pertaining to the logged record. /// 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) { fn log(&self, mut record: Record) {
if self.peer_id.is_some() { if self.peer_id.is_some() {
record.peer_id = self.peer_id record.peer_id = self.peer_id
@ -195,15 +201,16 @@ impl<'a, L: Deref> Logger for WithContext<'a, L> where L::Target: Logger {
} }
} }
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. /// 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 { pub fn from(
WithContext { logger: &'a L, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>,
logger, payment_hash: Option<PaymentHash>,
peer_id, ) -> Self {
channel_id, WithContext { logger, peer_id, channel_id, payment_hash }
payment_hash,
}
} }
} }
@ -257,12 +264,12 @@ impl<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone> fmt::Display fo
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
use crate::ln::types::ChannelId; use crate::ln::types::ChannelId;
use crate::ln::PaymentHash; use crate::ln::PaymentHash;
use crate::util::logger::{Logger, Level, WithContext};
use crate::util::test_utils::TestLogger;
use crate::sync::Arc; use crate::sync::Arc;
use crate::util::logger::{Level, Logger, WithContext};
use crate::util::test_utils::TestLogger;
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
#[test] #[test]
fn test_level_show() { fn test_level_show() {
@ -272,14 +279,12 @@ mod tests {
} }
struct WrapperLog { struct WrapperLog {
logger: Arc<dyn Logger> logger: Arc<dyn Logger>,
} }
impl WrapperLog { impl WrapperLog {
fn new(logger: Arc<dyn Logger>) -> WrapperLog { fn new(logger: Arc<dyn Logger>) -> WrapperLog {
WrapperLog { WrapperLog { logger }
logger,
}
} }
fn call_macros(&self) { fn call_macros(&self) {
@ -295,7 +300,7 @@ mod tests {
#[test] #[test]
fn test_logging_macros() { fn test_logging_macros() {
let logger = TestLogger::new(); 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)); let wrapper = WrapperLog::new(Arc::clone(&logger));
wrapper.call_macros(); wrapper.call_macros();
} }
@ -306,7 +311,8 @@ mod tests {
let secp_ctx = Secp256k1::new(); let secp_ctx = Secp256k1::new();
let pk = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()); let pk = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
let payment_hash = PaymentHash([0; 32]); 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_error!(context_logger, "This is an error");
log_warn!(context_logger, "This is an error"); log_warn!(context_logger, "This is an error");
log_debug!(context_logger, "This is an error"); log_debug!(context_logger, "This is an error");
@ -314,7 +320,10 @@ mod tests {
log_gossip!(context_logger, "This is an error"); log_gossip!(context_logger, "This is an error");
log_info!(context_logger, "This is an error"); log_info!(context_logger, "This is an error");
logger.assert_log_context_contains( 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,
); );
} }
@ -324,7 +333,8 @@ mod tests {
let secp_ctx = Secp256k1::new(); let secp_ctx = Secp256k1::new();
let pk = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()); let pk = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
let payment_hash = PaymentHash([0; 32]); 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); let full_context_logger = WithContext::from(&context_logger, Some(pk), None, None);
log_error!(full_context_logger, "This is an error"); log_error!(full_context_logger, "This is an error");
log_warn!(full_context_logger, "This is an error"); log_warn!(full_context_logger, "This is an error");
@ -333,7 +343,10 @@ mod tests {
log_gossip!(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"); log_info!(full_context_logger, "This is an error");
logger.assert_log_context_contains( 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,
); );
} }