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