Expand the Route object to include multiple paths.
[rust-lightning] / lightning / src / util / macro_logger.rs
1 use chain::transaction::OutPoint;
2
3 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
4 use bitcoin::blockdata::transaction::Transaction;
5 use secp256k1::key::PublicKey;
6
7 use ln::router::Route;
8 use ln::chan_utils::HTLCType;
9
10 use std;
11
12 pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey);
13 impl<'a> std::fmt::Display for DebugPubKey<'a> {
14         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
15                 for i in self.0.serialize().iter() {
16                         write!(f, "{:02x}", i)?;
17                 }
18                 Ok(())
19         }
20 }
21 macro_rules! log_pubkey {
22         ($obj: expr) => {
23                 ::util::macro_logger::DebugPubKey(&$obj)
24         }
25 }
26
27 pub(crate) struct DebugBytes<'a>(pub &'a [u8]);
28 impl<'a> std::fmt::Display for DebugBytes<'a> {
29         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
30                 for i in self.0 {
31                         write!(f, "{:02x}", i)?;
32                 }
33                 Ok(())
34         }
35 }
36 macro_rules! log_bytes {
37         ($obj: expr) => {
38                 ::util::macro_logger::DebugBytes(&$obj)
39         }
40 }
41
42 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Sha256dHash, pub u16);
43 impl<'a> std::fmt::Display for DebugFundingChannelId<'a> {
44         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
45                 for i in OutPoint::new(self.0.clone(), self.1).to_channel_id().iter() {
46                         write!(f, "{:02x}", i)?;
47                 }
48                 Ok(())
49         }
50 }
51 macro_rules! log_funding_channel_id {
52         ($funding_txid: expr, $funding_txo: expr) => {
53                 ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
54         }
55 }
56
57 pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a Option<(OutPoint, T)>);
58 impl<'a, T> std::fmt::Display for DebugFundingInfo<'a, T> {
59         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
60                 match self.0.as_ref() {
61                         Some(&(ref funding_output, _)) => DebugBytes(&funding_output.to_channel_id()[..]).fmt(f),
62                         None => write!(f, "without funding output set"),
63                 }
64         }
65 }
66 macro_rules! log_funding_info {
67         ($key_storage: expr) => {
68                 match $key_storage {
69                         Storage::Local { ref funding_info, .. } => {
70                                 ::util::macro_logger::DebugFundingInfo(&funding_info)
71                         },
72                         Storage::Watchtower { .. } => {
73                                 ::util::macro_logger::DebugFundingInfo(&None)
74                         }
75                 }
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 macro_rules! log_internal {
135         ($self: ident, $lvl:expr, $($arg:tt)+) => (
136                 &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
137         );
138 }
139
140 macro_rules! log_error {
141         ($self: ident, $($arg:tt)*) => (
142                 #[cfg(not(any(feature = "max_level_off")))]
143                 log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
144         )
145 }
146
147 macro_rules! log_warn {
148         ($self: ident, $($arg:tt)*) => (
149                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
150                 log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
151         )
152 }
153
154 macro_rules! log_info {
155         ($self: ident, $($arg:tt)*) => (
156                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
157                 log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
158         )
159 }
160
161 macro_rules! log_debug {
162         ($self: ident, $($arg:tt)*) => (
163                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
164                 log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
165         )
166 }
167
168 macro_rules! log_trace {
169         ($self: ident, $($arg:tt)*) => (
170                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
171                 log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);
172         )
173 }