]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/util/macro_logger.rs
Check `max_total_routing_fee` is reduced in `mpp_retry` test
[rust-lightning] / lightning / src / util / macro_logger.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 use crate::chain::transaction::OutPoint;
11 use crate::sign::SpendableOutputDescriptor;
12
13 use bitcoin::hash_types::Txid;
14 use bitcoin::blockdata::transaction::Transaction;
15
16 use crate::routing::router::Route;
17 use crate::ln::chan_utils::HTLCClaim;
18
19 macro_rules! log_iter {
20         ($obj: expr) => {
21                 $crate::util::logger::DebugIter($obj)
22         }
23 }
24
25 /// Logs a pubkey in hex format.
26 #[macro_export]
27 macro_rules! log_pubkey {
28         ($obj: expr) => {
29                 $crate::util::logger::DebugPubKey(&$obj)
30         }
31 }
32
33 /// Logs a byte slice in hex format.
34 #[macro_export]
35 macro_rules! log_bytes {
36         ($obj: expr) => {
37                 $crate::util::logger::DebugBytes(&$obj)
38         }
39 }
40
41 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Txid, pub u16);
42 impl<'a> core::fmt::Display for DebugFundingChannelId<'a> {
43         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
44                 (OutPoint { txid: self.0.clone(), index: self.1 }).to_channel_id().fmt(f)
45         }
46 }
47 macro_rules! log_funding_channel_id {
48         ($funding_txid: expr, $funding_txo: expr) => {
49                 $crate::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
50         }
51 }
52
53 pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a (OutPoint, T));
54 impl<'a, T> core::fmt::Display for DebugFundingInfo<'a, T> {
55         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
56                 (self.0).0.to_channel_id().fmt(f)
57         }
58 }
59 macro_rules! log_funding_info {
60         ($key_storage: expr) => {
61                 $crate::util::macro_logger::DebugFundingInfo(&$key_storage.get_funding_txo())
62         }
63 }
64
65 pub(crate) struct DebugRoute<'a>(pub &'a Route);
66 impl<'a> core::fmt::Display for DebugRoute<'a> {
67         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
68                 for (idx, p) in self.0.paths.iter().enumerate() {
69                         writeln!(f, "path {}:", idx)?;
70                         for h in p.hops.iter() {
71                                 writeln!(f, " node_id: {}, short_channel_id: {}, fee_msat: {}, cltv_expiry_delta: {}", log_pubkey!(h.pubkey), h.short_channel_id, h.fee_msat, h.cltv_expiry_delta)?;
72                         }
73                         writeln!(f, " blinded_tail: {:?}", p.blinded_tail)?;
74                 }
75                 Ok(())
76         }
77 }
78 macro_rules! log_route {
79         ($obj: expr) => {
80                 $crate::util::macro_logger::DebugRoute(&$obj)
81         }
82 }
83
84 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
85 impl<'a> core::fmt::Display for DebugTx<'a> {
86         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
87                 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
88                         let first_input = &self.0.input[0];
89                         let witness_script_len = first_input.witness.last().unwrap_or(&[]).len();
90                         if self.0.input.len() == 1 && witness_script_len == 71 &&
91                                         (first_input.sequence.0 >> 8*3) as u8 == 0x80 {
92                                 write!(f, "commitment tx ")?;
93                         } else if self.0.input.len() == 1 && witness_script_len == 71 {
94                                 write!(f, "closing tx ")?;
95                         } else if self.0.input.len() == 1 && HTLCClaim::from_witness(&first_input.witness) == Some(HTLCClaim::OfferedTimeout) {
96                                 write!(f, "HTLC-timeout tx ")?;
97                         } else if self.0.input.len() == 1 && HTLCClaim::from_witness(&first_input.witness) == Some(HTLCClaim::AcceptedPreimage) {
98                                 write!(f, "HTLC-success tx ")?;
99                         } else {
100                                 let mut num_preimage = 0;
101                                 let mut num_timeout = 0;
102                                 let mut num_revoked = 0;
103                                 for inp in &self.0.input {
104                                         let htlc_claim = HTLCClaim::from_witness(&inp.witness);
105                                         match htlc_claim {
106                                                 Some(HTLCClaim::AcceptedPreimage)|Some(HTLCClaim::OfferedPreimage) => num_preimage += 1,
107                                                 Some(HTLCClaim::AcceptedTimeout)|Some(HTLCClaim::OfferedTimeout) => num_timeout += 1,
108                                                 Some(HTLCClaim::Revocation) => num_revoked += 1,
109                                                 None => continue,
110                                         }
111                                 }
112                                 if num_preimage > 0 || num_timeout > 0 || num_revoked > 0 {
113                                         write!(f, "HTLC claim tx ({} preimage, {} timeout, {} revoked) ",
114                                                 num_preimage, num_timeout, num_revoked)?;
115                                 }
116                         }
117                 } else {
118                         debug_assert!(false, "We should never generate unknown transaction types");
119                         write!(f, "unknown tx type ").unwrap();
120                 }
121                 write!(f, "with txid {}", self.0.txid())?;
122                 Ok(())
123         }
124 }
125
126 macro_rules! log_tx {
127         ($obj: expr) => {
128                 $crate::util::macro_logger::DebugTx(&$obj)
129         }
130 }
131
132 pub(crate) struct DebugSpendable<'a>(pub &'a SpendableOutputDescriptor);
133 impl<'a> core::fmt::Display for DebugSpendable<'a> {
134         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
135                 match self.0 {
136                         &SpendableOutputDescriptor::StaticOutput { ref outpoint, .. } => {
137                                 write!(f, "StaticOutput {}:{} marked for spending", outpoint.txid, outpoint.index)?;
138                         }
139                         &SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) => {
140                                 write!(f, "DelayedPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
141                         }
142                         &SpendableOutputDescriptor::StaticPaymentOutput(ref descriptor) => {
143                                 write!(f, "StaticPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
144                         }
145                 }
146                 Ok(())
147         }
148 }
149
150 macro_rules! log_spendable {
151         ($obj: expr) => {
152                 $crate::util::macro_logger::DebugSpendable(&$obj)
153         }
154 }
155
156 /// Create a new Record and log it. You probably don't want to use this macro directly,
157 /// but it needs to be exported so `log_trace` etc can use it in external crates.
158 #[doc(hidden)]
159 #[macro_export]
160 macro_rules! log_internal {
161         ($logger: expr, $lvl:expr, $($arg:tt)+) => (
162                 $logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()))
163         );
164 }
165
166 /// Logs an entry at the given level.
167 #[doc(hidden)]
168 #[macro_export]
169 macro_rules! log_given_level {
170         ($logger: expr, $lvl:expr, $($arg:tt)+) => (
171                 match $lvl {
172                         #[cfg(not(any(feature = "max_level_off")))]
173                         $crate::util::logger::Level::Error => $crate::log_internal!($logger, $lvl, $($arg)*),
174                         #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
175                         $crate::util::logger::Level::Warn => $crate::log_internal!($logger, $lvl, $($arg)*),
176                         #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
177                         $crate::util::logger::Level::Info => $crate::log_internal!($logger, $lvl, $($arg)*),
178                         #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
179                         $crate::util::logger::Level::Debug => $crate::log_internal!($logger, $lvl, $($arg)*),
180                         #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
181                         $crate::util::logger::Level::Trace => $crate::log_internal!($logger, $lvl, $($arg)*),
182                         #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug", feature = "max_level_trace")))]
183                         $crate::util::logger::Level::Gossip => $crate::log_internal!($logger, $lvl, $($arg)*),
184
185                         #[cfg(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug", feature = "max_level_trace"))]
186                         _ => {
187                                 // The level is disabled at compile-time
188                         },
189                 }
190         );
191 }
192
193 /// Log at the `ERROR` level.
194 #[macro_export]
195 macro_rules! log_error {
196         ($logger: expr, $($arg:tt)*) => (
197                 $crate::log_given_level!($logger, $crate::util::logger::Level::Error, $($arg)*);
198         )
199 }
200
201 /// Log at the `WARN` level.
202 #[macro_export]
203 macro_rules! log_warn {
204         ($logger: expr, $($arg:tt)*) => (
205                 $crate::log_given_level!($logger, $crate::util::logger::Level::Warn, $($arg)*);
206         )
207 }
208
209 /// Log at the `INFO` level.
210 #[macro_export]
211 macro_rules! log_info {
212         ($logger: expr, $($arg:tt)*) => (
213                 $crate::log_given_level!($logger, $crate::util::logger::Level::Info, $($arg)*);
214         )
215 }
216
217 /// Log at the `DEBUG` level.
218 #[macro_export]
219 macro_rules! log_debug {
220         ($logger: expr, $($arg:tt)*) => (
221                 $crate::log_given_level!($logger, $crate::util::logger::Level::Debug, $($arg)*);
222         )
223 }
224
225 /// Log at the `TRACE` level.
226 #[macro_export]
227 macro_rules! log_trace {
228         ($logger: expr, $($arg:tt)*) => (
229                 $crate::log_given_level!($logger, $crate::util::logger::Level::Trace, $($arg)*)
230         )
231 }
232
233 /// Log at the `GOSSIP` level.
234 #[macro_export]
235 macro_rules! log_gossip {
236         ($logger: expr, $($arg:tt)*) => (
237                 $crate::log_given_level!($logger, $crate::util::logger::Level::Gossip, $($arg)*);
238         )
239 }