Add semantics to logger::Records
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 21 Nov 2023 16:47:12 +0000 (10:47 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 1 Dec 2023 17:30:19 +0000 (11:30 -0600)
Include optional peer and channel ids to logger::Record. This will be
used by wrappers around Logger in order to provide more context (e.g.,
the peer that sent a message, the channel an operation is pertaining to,
etc.). Implementations of Logger can include this as metadata to aid in
searching logs.

lightning/src/util/logger.rs
lightning/src/util/macro_logger.rs

index 4534c9dd8cfc10d062b0fba15b32f4cad1537bda..4018b8bf7af4180be712fb7c433ccfe69b2e2bd0 100644 (file)
@@ -19,6 +19,7 @@ use bitcoin::secp256k1::PublicKey;
 use core::cmp;
 use core::fmt;
 
+use crate::ln::ChannelId;
 #[cfg(c_bindings)]
 use crate::prelude::*; // Needed for String
 
@@ -95,6 +96,11 @@ impl Level {
 pub struct Record<'a> {
        /// The verbosity level of the message.
        pub level: Level,
+       /// The node id of the peer pertaining to the logged record.
+       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.
+       pub channel_id: Option<ChannelId>,
        #[cfg(not(c_bindings))]
        /// The message body.
        pub args: fmt::Arguments<'a>,
@@ -119,9 +125,14 @@ impl<'a> Record<'a> {
        ///
        /// This is not exported to bindings users as fmt can't be used in C
        #[inline]
-       pub fn new(level: Level, args: fmt::Arguments<'a>, module_path: &'static str, file: &'static str, line: u32) -> Record<'a> {
+       pub fn new(
+               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 {
                        level,
+                       peer_id,
+                       channel_id,
                        #[cfg(not(c_bindings))]
                        args,
                        #[cfg(c_bindings)]
index f1d4fe54087ef49a0e6f0edfbea23c28fbf62ee6..203c544e0096385c959d124f051bdc15295e958e 100644 (file)
@@ -159,7 +159,7 @@ macro_rules! log_spendable {
 #[macro_export]
 macro_rules! log_internal {
        ($logger: expr, $lvl:expr, $($arg:tt)+) => (
-               $logger.log($crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()))
+               $logger.log($crate::util::logger::Record::new($lvl, None, None, format_args!($($arg)+), module_path!(), file!(), line!()))
        );
 }