Add log for every tx broadcast
[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[0].witness.last().unwrap().len() == 71 && (self.0.input[0].sequence >> 8*3) as u8 == 0x80 { write!(f, "commitment tx")?; }
97                 else if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 { write!(f, "closing tx")?; }
98                 else if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 133 && self.0.input[0].witness.len() == 5 { write!(f, "HTLC-timeout tx")?; }
99                 else if self.0.input.len() == 1 && (self.0.input[0].witness.last().unwrap().len() == 138 || self.0.input[0].witness.last().unwrap().len() == 139) && self.0.input[0].witness.len() == 5 { write!(f, "HTLC-success tx")?; }
100                 else {
101                         for inp in &self.0.input {
102                                 if inp.witness.last().unwrap().len() == 133 { write!(f, "preimage tx")?; break }
103                                 else if inp.witness.last().unwrap().len() == 138 { write!(f, "timeout tx")?; break }
104                         }
105                 }
106                 Ok(())
107         }
108 }
109
110 macro_rules! log_tx {
111         ($obj: expr) => {
112                 ::util::macro_logger::DebugTx(&$obj)
113         }
114 }
115
116 macro_rules! log_internal {
117         ($self: ident, $lvl:expr, $($arg:tt)+) => (
118                 &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
119         );
120 }
121
122 macro_rules! log_error {
123         ($self: ident, $($arg:tt)*) => (
124                 #[cfg(not(any(feature = "max_level_off")))]
125                 log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
126         )
127 }
128
129 macro_rules! log_warn {
130         ($self: ident, $($arg:tt)*) => (
131                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
132                 log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
133         )
134 }
135
136 macro_rules! log_info {
137         ($self: ident, $($arg:tt)*) => (
138                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
139                 log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
140         )
141 }
142
143 macro_rules! log_debug {
144         ($self: ident, $($arg:tt)*) => (
145                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
146                 log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
147         )
148 }
149
150 macro_rules! log_trace {
151         ($self: ident, $($arg:tt)*) => (
152                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
153                 log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);
154         )
155 }