dba63663471d48e9181ed25d1e460c4c304b0153
[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
9 use std;
10
11 pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey);
12 impl<'a> std::fmt::Display for DebugPubKey<'a> {
13         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
14                 for i in self.0.serialize().iter() {
15                         write!(f, "{:02x}", i)?;
16                 }
17                 Ok(())
18         }
19 }
20 macro_rules! log_pubkey {
21         ($obj: expr) => {
22                 ::util::macro_logger::DebugPubKey(&$obj)
23         }
24 }
25
26 pub(crate) struct DebugBytes<'a>(pub &'a [u8]);
27 impl<'a> std::fmt::Display for DebugBytes<'a> {
28         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
29                 for i in self.0 {
30                         write!(f, "{:02x}", i)?;
31                 }
32                 Ok(())
33         }
34 }
35 macro_rules! log_bytes {
36         ($obj: expr) => {
37                 ::util::macro_logger::DebugBytes(&$obj)
38         }
39 }
40
41 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Sha256dHash, pub u16);
42 impl<'a> std::fmt::Display for DebugFundingChannelId<'a> {
43         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
44                 for i in OutPoint::new(self.0.clone(), self.1).to_channel_id().iter() {
45                         write!(f, "{:02x}", i)?;
46                 }
47                 Ok(())
48         }
49 }
50 macro_rules! log_funding_channel_id {
51         ($funding_txid: expr, $funding_txo: expr) => {
52                 ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
53         }
54 }
55
56 pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a Option<(OutPoint, T)>);
57 impl<'a, T> std::fmt::Display for DebugFundingInfo<'a, T> {
58         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
59                 match self.0.as_ref() {
60                         Some(&(ref funding_output, _)) => DebugBytes(&funding_output.to_channel_id()[..]).fmt(f),
61                         None => write!(f, "without funding output set"),
62                 }
63         }
64 }
65 macro_rules! log_funding_info {
66         ($key_storage: expr) => {
67                 match $key_storage {
68                         Storage::Local { ref funding_info, .. } => {
69                                 ::util::macro_logger::DebugFundingInfo(&funding_info)
70                         },
71                         Storage::Watchtower { .. } => {
72                                 ::util::macro_logger::DebugFundingInfo(&None)
73                         }
74                 }
75         }
76 }
77
78 pub(crate) struct DebugRoute<'a>(pub &'a Route);
79 impl<'a> std::fmt::Display for DebugRoute<'a> {
80         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
81                 for h in self.0.hops.iter() {
82                         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)?;
83                 }
84                 Ok(())
85         }
86 }
87 macro_rules! log_route {
88         ($obj: expr) => {
89                 ::util::macro_logger::DebugRoute(&$obj)
90         }
91 }
92
93 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
94 impl<'a> std::fmt::Display for DebugTx<'a> {
95         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
96                 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
97                         if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 &&
98                                         (self.0.input[0].sequence >> 8*3) as u8 == 0x80 {
99                                 write!(f, "commitment tx")?;
100                         } else if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 {
101                                 write!(f, "closing tx")?;
102                         } else if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 133 &&
103                                         self.0.input[0].witness.len() == 5 {
104                                 write!(f, "HTLC-timeout tx")?;
105                         } else if self.0.input.len() == 1 &&
106                                         (self.0.input[0].witness.last().unwrap().len() == 138 || self.0.input[0].witness.last().unwrap().len() == 139) &&
107                                         self.0.input[0].witness.len() == 5 {
108                                 write!(f, "HTLC-success tx")?;
109                         } else {
110                                 for inp in &self.0.input {
111                                         if !inp.witness.is_empty() {
112                                                 if inp.witness.last().unwrap().len() == 133 { write!(f, "preimage-")?; break }
113                                                 else if inp.witness.last().unwrap().len() == 138 { write!(f, "timeout-")?; break }
114                                         }
115                                 }
116                                 write!(f, "tx")?;
117                         }
118                 } else {
119                         write!(f, "INVALID TRANSACTION")?;
120                 }
121                 Ok(())
122         }
123 }
124
125 macro_rules! log_tx {
126         ($obj: expr) => {
127                 ::util::macro_logger::DebugTx(&$obj)
128         }
129 }
130
131 macro_rules! log_internal {
132         ($self: ident, $lvl:expr, $($arg:tt)+) => (
133                 &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
134         );
135 }
136
137 macro_rules! log_error {
138         ($self: ident, $($arg:tt)*) => (
139                 #[cfg(not(any(feature = "max_level_off")))]
140                 log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
141         )
142 }
143
144 macro_rules! log_warn {
145         ($self: ident, $($arg:tt)*) => (
146                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
147                 log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
148         )
149 }
150
151 macro_rules! log_info {
152         ($self: ident, $($arg:tt)*) => (
153                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
154                 log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
155         )
156 }
157
158 macro_rules! log_debug {
159         ($self: ident, $($arg:tt)*) => (
160                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
161                 log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
162         )
163 }
164
165 macro_rules! log_trace {
166         ($self: ident, $($arg:tt)*) => (
167                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
168                 log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);
169         )
170 }