]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/util/macro_logger.rs
Underscore TxCreationKeys ownership
[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::key::PublicKey;
16
17 use routing::router::Route;
18 use ln::chan_utils::HTLCType;
19
20 use std;
21
22 pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey);
23 impl<'a> std::fmt::Display for DebugPubKey<'a> {
24         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
25                 for i in self.0.serialize().iter() {
26                         write!(f, "{:02x}", i)?;
27                 }
28                 Ok(())
29         }
30 }
31 macro_rules! log_pubkey {
32         ($obj: expr) => {
33                 ::util::macro_logger::DebugPubKey(&$obj)
34         }
35 }
36
37 pub(crate) struct DebugBytes<'a>(pub &'a [u8]);
38 impl<'a> std::fmt::Display for DebugBytes<'a> {
39         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
40                 for i in self.0 {
41                         write!(f, "{:02x}", i)?;
42                 }
43                 Ok(())
44         }
45 }
46 macro_rules! log_bytes {
47         ($obj: expr) => {
48                 ::util::macro_logger::DebugBytes(&$obj)
49         }
50 }
51
52 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Txid, pub u16);
53 impl<'a> std::fmt::Display for DebugFundingChannelId<'a> {
54         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
55                 for i in (OutPoint { txid: self.0.clone(), index: self.1 }).to_channel_id().iter() {
56                         write!(f, "{:02x}", i)?;
57                 }
58                 Ok(())
59         }
60 }
61 macro_rules! log_funding_channel_id {
62         ($funding_txid: expr, $funding_txo: expr) => {
63                 ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
64         }
65 }
66
67 pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a (OutPoint, T));
68 impl<'a, T> std::fmt::Display for DebugFundingInfo<'a, T> {
69         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
70                 DebugBytes(&(self.0).0.to_channel_id()[..]).fmt(f)
71         }
72 }
73 macro_rules! log_funding_info {
74         ($key_storage: expr) => {
75                 ::util::macro_logger::DebugFundingInfo(&$key_storage.funding_info)
76         }
77 }
78
79 pub(crate) struct DebugRoute<'a>(pub &'a Route);
80 impl<'a> std::fmt::Display for DebugRoute<'a> {
81         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
82                 for (idx, p) in self.0.paths.iter().enumerate() {
83                         write!(f, "path {}:\n", idx)?;
84                         for h in p.iter() {
85                                 write!(f, " node_id: {}, short_channel_id: {}, fee_msat: {}, cltv_expiry_delta: {}\n", log_pubkey!(h.pubkey), h.short_channel_id, h.fee_msat, h.cltv_expiry_delta)?;
86                         }
87                 }
88                 Ok(())
89         }
90 }
91 macro_rules! log_route {
92         ($obj: expr) => {
93                 ::util::macro_logger::DebugRoute(&$obj)
94         }
95 }
96
97 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
98 impl<'a> std::fmt::Display for DebugTx<'a> {
99         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
100                 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
101                         if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 &&
102                                         (self.0.input[0].sequence >> 8*3) as u8 == 0x80 {
103                                 write!(f, "commitment tx")?;
104                         } else if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 {
105                                 write!(f, "closing tx")?;
106                         } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) &&
107                                         self.0.input[0].witness.len() == 5 {
108                                 write!(f, "HTLC-timeout tx")?;
109                         } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) &&
110                                         self.0.input[0].witness.len() == 5 {
111                                 write!(f, "HTLC-success tx")?;
112                         } else {
113                                 for inp in &self.0.input {
114                                         if !inp.witness.is_empty() {
115                                                 if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) { write!(f, "preimage-")?; break }
116                                                 else if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) { write!(f, "timeout-")?; break }
117                                         }
118                                 }
119                                 write!(f, "tx")?;
120                         }
121                 } else {
122                         write!(f, "INVALID TRANSACTION")?;
123                 }
124                 Ok(())
125         }
126 }
127
128 macro_rules! log_tx {
129         ($obj: expr) => {
130                 ::util::macro_logger::DebugTx(&$obj)
131         }
132 }
133
134 pub(crate) struct DebugSpendable<'a>(pub &'a SpendableOutputDescriptor);
135 impl<'a> std::fmt::Display for DebugSpendable<'a> {
136         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
137                 match self.0 {
138                         &SpendableOutputDescriptor::StaticOutput { ref outpoint, .. } => {
139                                 write!(f, "StaticOutput {}:{} marked for spending", outpoint.txid, outpoint.index)?;
140                         }
141                         &SpendableOutputDescriptor::DynamicOutputP2WSH { ref outpoint, .. } => {
142                                 write!(f, "DynamicOutputP2WSH {}:{} marked for spending", outpoint.txid, outpoint.index)?;
143                         }
144                         &SpendableOutputDescriptor::StaticOutputRemotePayment { ref outpoint, .. } => {
145                                 write!(f, "DynamicOutputP2WPKH {}:{} marked for spending", outpoint.txid, outpoint.index)?;
146                         }
147                 }
148                 Ok(())
149         }
150 }
151
152 macro_rules! log_spendable {
153         ($obj: expr) => {
154                 ::util::macro_logger::DebugSpendable(&$obj)
155         }
156 }
157
158 macro_rules! log_internal {
159         ($logger: expr, $lvl:expr, $($arg:tt)+) => (
160                 $logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
161         );
162 }
163
164 macro_rules! log_error {
165         ($logger: expr, $($arg:tt)*) => (
166                 #[cfg(not(any(feature = "max_level_off")))]
167                 log_internal!($logger, $crate::util::logger::Level::Error, $($arg)*);
168         )
169 }
170
171 macro_rules! log_warn {
172         ($logger: expr, $($arg:tt)*) => (
173                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
174                 log_internal!($logger, $crate::util::logger::Level::Warn, $($arg)*);
175         )
176 }
177
178 macro_rules! log_info {
179         ($logger: expr, $($arg:tt)*) => (
180                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
181                 log_internal!($logger, $crate::util::logger::Level::Info, $($arg)*);
182         )
183 }
184
185 macro_rules! log_debug {
186         ($logger: expr, $($arg:tt)*) => (
187                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
188                 log_internal!($logger, $crate::util::logger::Level::Debug, $($arg)*);
189         )
190 }
191
192 macro_rules! log_trace {
193         ($logger: expr, $($arg:tt)*) => (
194                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
195                 log_internal!($logger, $crate::util::logger::Level::Trace, $($arg)*);
196         )
197 }