Update bitcoin crate to 0.29.0
[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 chain::transaction::OutPoint;
11 use chain::keysinterface::SpendableOutputDescriptor;
12
13 use bitcoin::hash_types::Txid;
14 use bitcoin::blockdata::transaction::Transaction;
15 use bitcoin::secp256k1::PublicKey;
16
17 use routing::router::Route;
18 use ln::chan_utils::HTLCType;
19 use util::logger::DebugBytes;
20
21 pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey);
22 impl<'a> core::fmt::Display for DebugPubKey<'a> {
23         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
24                 for i in self.0.serialize().iter() {
25                         write!(f, "{:02x}", i)?;
26                 }
27                 Ok(())
28         }
29 }
30 macro_rules! log_pubkey {
31         ($obj: expr) => {
32                 ::util::macro_logger::DebugPubKey(&$obj)
33         }
34 }
35
36 /// Logs a byte slice in hex format.
37 #[macro_export]
38 macro_rules! log_bytes {
39         ($obj: expr) => {
40                 $crate::util::logger::DebugBytes(&$obj)
41         }
42 }
43
44 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Txid, pub u16);
45 impl<'a> core::fmt::Display for DebugFundingChannelId<'a> {
46         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
47                 for i in (OutPoint { txid: self.0.clone(), index: self.1 }).to_channel_id().iter() {
48                         write!(f, "{:02x}", i)?;
49                 }
50                 Ok(())
51         }
52 }
53 macro_rules! log_funding_channel_id {
54         ($funding_txid: expr, $funding_txo: expr) => {
55                 ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
56         }
57 }
58
59 pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a (OutPoint, T));
60 impl<'a, T> core::fmt::Display for DebugFundingInfo<'a, T> {
61         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
62                 DebugBytes(&(self.0).0.to_channel_id()[..]).fmt(f)
63         }
64 }
65 macro_rules! log_funding_info {
66         ($key_storage: expr) => {
67                 ::util::macro_logger::DebugFundingInfo(&$key_storage.get_funding_txo())
68         }
69 }
70
71 pub(crate) struct DebugRoute<'a>(pub &'a Route);
72 impl<'a> core::fmt::Display for DebugRoute<'a> {
73         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
74                 for (idx, p) in self.0.paths.iter().enumerate() {
75                         writeln!(f, "path {}:", idx)?;
76                         for h in p.iter() {
77                                 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)?;
78                         }
79                 }
80                 Ok(())
81         }
82 }
83 macro_rules! log_route {
84         ($obj: expr) => {
85                 ::util::macro_logger::DebugRoute(&$obj)
86         }
87 }
88
89 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
90 impl<'a> core::fmt::Display for DebugTx<'a> {
91         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
92                 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
93                         if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 &&
94                                         (self.0.input[0].sequence.0 >> 8*3) as u8 == 0x80 {
95                                 write!(f, "commitment tx ")?;
96                         } else if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 {
97                                 write!(f, "closing tx ")?;
98                         } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) &&
99                                         self.0.input[0].witness.len() == 5 {
100                                 write!(f, "HTLC-timeout tx ")?;
101                         } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) &&
102                                         self.0.input[0].witness.len() == 5 {
103                                 write!(f, "HTLC-success tx ")?;
104                         } else {
105                                 for inp in &self.0.input {
106                                         if !inp.witness.is_empty() {
107                                                 if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) { write!(f, "preimage-")?; break }
108                                                 else if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) { write!(f, "timeout-")?; break }
109                                         }
110                                 }
111                                 write!(f, "tx ")?;
112                         }
113                 } else {
114                         debug_assert!(false, "We should never generate unknown transaction types");
115                         write!(f, "unknown tx type ").unwrap();
116                 }
117                 write!(f, "with txid {}", self.0.txid())?;
118                 Ok(())
119         }
120 }
121
122 macro_rules! log_tx {
123         ($obj: expr) => {
124                 ::util::macro_logger::DebugTx(&$obj)
125         }
126 }
127
128 pub(crate) struct DebugSpendable<'a>(pub &'a SpendableOutputDescriptor);
129 impl<'a> core::fmt::Display for DebugSpendable<'a> {
130         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
131                 match self.0 {
132                         &SpendableOutputDescriptor::StaticOutput { ref outpoint, .. } => {
133                                 write!(f, "StaticOutput {}:{} marked for spending", outpoint.txid, outpoint.index)?;
134                         }
135                         &SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) => {
136                                 write!(f, "DelayedPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
137                         }
138                         &SpendableOutputDescriptor::StaticPaymentOutput(ref descriptor) => {
139                                 write!(f, "StaticPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
140                         }
141                 }
142                 Ok(())
143         }
144 }
145
146 macro_rules! log_spendable {
147         ($obj: expr) => {
148                 ::util::macro_logger::DebugSpendable(&$obj)
149         }
150 }
151
152 /// Create a new Record and log it. You probably don't want to use this macro directly,
153 /// but it needs to be exported so `log_trace` etc can use it in external crates.
154 #[doc(hidden)]
155 #[macro_export]
156 macro_rules! log_internal {
157         ($logger: expr, $lvl:expr, $($arg:tt)+) => (
158                 $logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()))
159         );
160 }
161
162 /// Logs an entry at the given level.
163 #[macro_export]
164 macro_rules! log_given_level {
165         ($logger: expr, $lvl:expr, $($arg:tt)+) => (
166                 match $lvl {
167                         #[cfg(not(any(feature = "max_level_off")))]
168                         $crate::util::logger::Level::Error => log_internal!($logger, $lvl, $($arg)*),
169                         #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
170                         $crate::util::logger::Level::Warn => log_internal!($logger, $lvl, $($arg)*),
171                         #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
172                         $crate::util::logger::Level::Info => log_internal!($logger, $lvl, $($arg)*),
173                         #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
174                         $crate::util::logger::Level::Debug => log_internal!($logger, $lvl, $($arg)*),
175                         #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
176                         $crate::util::logger::Level::Trace => log_internal!($logger, $lvl, $($arg)*),
177                         #[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")))]
178                         $crate::util::logger::Level::Gossip => log_internal!($logger, $lvl, $($arg)*),
179
180                         #[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"))]
181                         _ => {
182                                 // The level is disabled at compile-time
183                         },
184                 }
185         );
186 }
187
188 /// Log an error.
189 #[macro_export]
190 macro_rules! log_error {
191         ($logger: expr, $($arg:tt)*) => (
192                 log_given_level!($logger, $crate::util::logger::Level::Error, $($arg)*);
193         )
194 }
195
196 macro_rules! log_warn {
197         ($logger: expr, $($arg:tt)*) => (
198                 log_given_level!($logger, $crate::util::logger::Level::Warn, $($arg)*);
199         )
200 }
201
202 macro_rules! log_info {
203         ($logger: expr, $($arg:tt)*) => (
204                 log_given_level!($logger, $crate::util::logger::Level::Info, $($arg)*);
205         )
206 }
207
208 macro_rules! log_debug {
209         ($logger: expr, $($arg:tt)*) => (
210                 log_given_level!($logger, $crate::util::logger::Level::Debug, $($arg)*);
211         )
212 }
213
214 /// Log a trace log.
215 #[macro_export]
216 macro_rules! log_trace {
217         ($logger: expr, $($arg:tt)*) => (
218                 log_given_level!($logger, $crate::util::logger::Level::Trace, $($arg)*)
219         )
220 }
221
222 /// Log a gossip log.
223 macro_rules! log_gossip {
224         ($logger: expr, $($arg:tt)*) => (
225                 log_given_level!($logger, $crate::util::logger::Level::Gossip, $($arg)*);
226         )
227 }