Merge pull request #3054 from TheBlueMatt/2024-04-fuzz-bolt11
[rust-lightning] / lightning / src / util / logger.rs
index f1534933792949a41318c3b80e0cc1847cbf63f4..904b7edd2f164f98b3a2572112fa033aa914a082 100644 (file)
@@ -20,7 +20,7 @@ use core::cmp;
 use core::fmt;
 use core::ops::Deref;
 
-use crate::ln::ChannelId;
+use crate::ln::types::ChannelId;
 #[cfg(c_bindings)]
 use crate::prelude::*; // Needed for String
 
@@ -91,13 +91,19 @@ impl Level {
        }
 }
 
+macro_rules! impl_record {
+       ($($args: lifetime)?, $($nonstruct_args: lifetime)?) => {
 /// A Record, unit of logging output with Metadata to enable filtering
 /// Module_path, file, line to inform on log's source
 #[derive(Clone, Debug)]
-pub struct Record<'a> {
+pub struct Record<$($args)?> {
        /// The verbosity level of the message.
        pub level: Level,
        /// The node id of the peer pertaining to the logged record.
+       ///
+       /// Note that in some cases a [`Self::channel_id`] may be filled in but this may still be
+       /// `None`, depending on if the peer information is readily available in LDK when the log is
+       /// generated.
        pub peer_id: Option<PublicKey>,
        /// The channel id of the channel pertaining to the logged record. May be a temporary id before
        /// the channel has been funded.
@@ -114,22 +120,17 @@ pub struct Record<'a> {
        pub file: &'static str,
        /// The line containing the message.
        pub line: u32,
-
-       #[cfg(c_bindings)]
-       /// We don't actually use the lifetime parameter in C bindings (as there is no good way to
-       /// communicate a lifetime to a C, or worse, Java user).
-       _phantom: core::marker::PhantomData<&'a ()>,
 }
 
-impl<'a> Record<'a> {
+impl<$($args)?> Record<$($args)?> {
        /// Returns a new Record.
        ///
        /// This is not exported to bindings users as fmt can't be used in C
        #[inline]
-       pub fn new(
+       pub fn new<$($nonstruct_args)?>(
                level: Level, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>,
                args: fmt::Arguments<'a>, module_path: &'static str, file: &'static str, line: u32
-       ) -> Record<'a> {
+       ) -> Record<$($args)?> {
                Record {
                        level,
                        peer_id,
@@ -141,11 +142,14 @@ impl<'a> Record<'a> {
                        module_path,
                        file,
                        line,
-                       #[cfg(c_bindings)]
-                       _phantom: core::marker::PhantomData,
                }
        }
 }
+} }
+#[cfg(not(c_bindings))]
+impl_record!('a, );
+#[cfg(c_bindings)]
+impl_record!(, 'a);
 
 /// A trait encapsulating the operations required of a logger.
 pub trait Logger {
@@ -154,6 +158,9 @@ pub trait Logger {
 }
 
 /// Adds relevant context to a [`Record`] before passing it to the wrapped [`Logger`].
+///
+/// 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 {
        /// The logger to delegate to after adding context to the record.
        logger: &'a L,
@@ -237,7 +244,7 @@ impl<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone> fmt::Display fo
 #[cfg(test)]
 mod tests {
        use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
-       use crate::ln::ChannelId;
+       use crate::ln::types::ChannelId;
        use crate::util::logger::{Logger, Level, WithContext};
        use crate::util::test_utils::TestLogger;
        use crate::sync::Arc;
@@ -250,11 +257,11 @@ mod tests {
        }
 
        struct WrapperLog {
-               logger: Arc<Logger>
+               logger: Arc<dyn Logger>
        }
 
        impl WrapperLog {
-               fn new(logger: Arc<Logger>) -> WrapperLog {
+               fn new(logger: Arc<dyn Logger>) -> WrapperLog {
                        WrapperLog {
                                logger,
                        }
@@ -274,7 +281,7 @@ mod tests {
        fn test_logging_macros() {
                let mut logger = TestLogger::new();
                logger.enable(Level::Gossip);
-               let logger : Arc<Logger> = Arc::new(logger);
+               let logger : Arc<dyn Logger> = Arc::new(logger);
                let wrapper = WrapperLog::new(Arc::clone(&logger));
                wrapper.call_macros();
        }