1f9cb1ad27e62d21f916272be115deca7d26710d
[rust-lightning] / lightning / src / util / macro_logger.rs
1 use chain::transaction::OutPoint;
2 use chain::keysinterface::SpendableOutputDescriptor;
3
4 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
5 use bitcoin::blockdata::transaction::Transaction;
6 use secp256k1::key::PublicKey;
7
8 use ln::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 Sha256dHash, 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::new(self.0.clone(), 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 Option<(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                 match self.0.as_ref() {
62                         Some(&(ref funding_output, _)) => DebugBytes(&funding_output.to_channel_id()[..]).fmt(f),
63                         None => write!(f, "without funding output set"),
64                 }
65         }
66 }
67 macro_rules! log_funding_info {
68         ($key_storage: expr) => {
69                 ::util::macro_logger::DebugFundingInfo(&$key_storage.funding_info)
70         }
71 }
72
73 pub(crate) struct DebugRoute<'a>(pub &'a Route);
74 impl<'a> std::fmt::Display for DebugRoute<'a> {
75         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
76                 for (idx, p) in self.0.paths.iter().enumerate() {
77                         write!(f, "path {}:\n", idx)?;
78                         for h in p.iter() {
79                                 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)?;
80                         }
81                 }
82                 Ok(())
83         }
84 }
85 macro_rules! log_route {
86         ($obj: expr) => {
87                 ::util::macro_logger::DebugRoute(&$obj)
88         }
89 }
90
91 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
92 impl<'a> std::fmt::Display for DebugTx<'a> {
93         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
94                 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
95                         if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 &&
96                                         (self.0.input[0].sequence >> 8*3) as u8 == 0x80 {
97                                 write!(f, "commitment tx")?;
98                         } else if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 {
99                                 write!(f, "closing tx")?;
100                         } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) &&
101                                         self.0.input[0].witness.len() == 5 {
102                                 write!(f, "HTLC-timeout tx")?;
103                         } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) &&
104                                         self.0.input[0].witness.len() == 5 {
105                                 write!(f, "HTLC-success tx")?;
106                         } else {
107                                 for inp in &self.0.input {
108                                         if !inp.witness.is_empty() {
109                                                 if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) { write!(f, "preimage-")?; break }
110                                                 else if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) { write!(f, "timeout-")?; break }
111                                         }
112                                 }
113                                 write!(f, "tx")?;
114                         }
115                 } else {
116                         write!(f, "INVALID TRANSACTION")?;
117                 }
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> std::fmt::Display for DebugSpendable<'a> {
130         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
131                 match self.0 {
132                         &SpendableOutputDescriptor::StaticOutput { ref outpoint, .. } => {
133                                 write!(f, "StaticOutput {}:{} marked for spending", outpoint.txid, outpoint.vout)?;
134                         }
135                         &SpendableOutputDescriptor::DynamicOutputP2WSH { ref outpoint, .. } => {
136                                 write!(f, "DynamicOutputP2WSH {}:{} marked for spending", outpoint.txid, outpoint.vout)?;
137                         }
138                         &SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, .. } => {
139                                 write!(f, "DynamicOutputP2WPKH {}:{} marked for spending", outpoint.txid, outpoint.vout)?;
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 macro_rules! log_internal {
153         ($self: ident, $lvl:expr, $($arg:tt)+) => (
154                 &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
155         );
156 }
157
158 macro_rules! log_error {
159         ($self: ident, $($arg:tt)*) => (
160                 #[cfg(not(any(feature = "max_level_off")))]
161                 log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
162         )
163 }
164
165 macro_rules! log_warn {
166         ($self: ident, $($arg:tt)*) => (
167                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
168                 log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
169         )
170 }
171
172 macro_rules! log_info {
173         ($self: ident, $($arg:tt)*) => (
174                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
175                 log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
176         )
177 }
178
179 macro_rules! log_debug {
180         ($self: ident, $($arg:tt)*) => (
181                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
182                 log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
183         )
184 }
185
186 macro_rules! log_trace {
187         ($self: ident, $($arg:tt)*) => (
188                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
189                 log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);
190         )
191 }