Move ChannelMonitor state behind a Mutex
[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.get_funding_txo())
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                         writeln!(f, "path {}:", idx)?;
84                         for h in p.iter() {
85                                 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)?;
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::DelayedPaymentOutput(ref descriptor) => {
142                                 write!(f, "DelayedPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
143                         }
144                         &SpendableOutputDescriptor::StaticPaymentOutput(ref descriptor) => {
145                                 write!(f, "DynamicOutputP2WPKH {}:{} marked for spending", descriptor.outpoint.txid, descriptor.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 /// Create a new Record and log it. You probably don't want to use this macro directly,
159 /// but it needs to be exported so `log_trace` etc can use it in external crates.
160 #[macro_export]
161 macro_rules! log_internal {
162         ($logger: expr, $lvl:expr, $($arg:tt)+) => (
163                 $logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
164         );
165 }
166
167 /// Log an error.
168 #[macro_export]
169 macro_rules! log_error {
170         ($logger: expr, $($arg:tt)*) => (
171                 #[cfg(not(any(feature = "max_level_off")))]
172                 log_internal!($logger, $crate::util::logger::Level::Error, $($arg)*);
173         )
174 }
175
176 macro_rules! log_warn {
177         ($logger: expr, $($arg:tt)*) => (
178                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
179                 log_internal!($logger, $crate::util::logger::Level::Warn, $($arg)*);
180         )
181 }
182
183 macro_rules! log_info {
184         ($logger: expr, $($arg:tt)*) => (
185                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
186                 log_internal!($logger, $crate::util::logger::Level::Info, $($arg)*);
187         )
188 }
189
190 macro_rules! log_debug {
191         ($logger: expr, $($arg:tt)*) => (
192                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
193                 log_internal!($logger, $crate::util::logger::Level::Debug, $($arg)*);
194         )
195 }
196
197 /// Log a trace log.
198 #[macro_export]
199 macro_rules! log_trace {
200         ($logger: expr, $($arg:tt)*) => (
201                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
202                 log_internal!($logger, $crate::util::logger::Level::Trace, $($arg)*);
203         )
204 }