1 use chain::transaction::OutPoint;
2 use chain::keysinterface::SpendableOutputDescriptor;
4 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
5 use bitcoin::blockdata::transaction::Transaction;
6 use secp256k1::key::PublicKey;
9 use ln::chan_utils::HTLCType;
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)?;
22 macro_rules! log_pubkey {
24 ::util::macro_logger::DebugPubKey(&$obj)
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> {
32 write!(f, "{:02x}", i)?;
37 macro_rules! log_bytes {
39 ::util::macro_logger::DebugBytes(&$obj)
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)?;
52 macro_rules! log_funding_channel_id {
53 ($funding_txid: expr, $funding_txo: expr) => {
54 ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
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"),
67 macro_rules! log_funding_info {
68 ($key_storage: expr) => {
70 Storage::Local { ref funding_info, .. } => {
71 ::util::macro_logger::DebugFundingInfo(&funding_info)
73 Storage::Watchtower { .. } => {
74 ::util::macro_logger::DebugFundingInfo(&None)
80 pub(crate) struct DebugRoute<'a>(pub &'a Route);
81 impl<'a> std::fmt::Display for DebugRoute<'a> {
82 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
83 for h in self.0.hops.iter() {
84 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)?;
89 macro_rules! log_route {
91 ::util::macro_logger::DebugRoute(&$obj)
95 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
96 impl<'a> std::fmt::Display for DebugTx<'a> {
97 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
98 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
99 if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 &&
100 (self.0.input[0].sequence >> 8*3) as u8 == 0x80 {
101 write!(f, "commitment tx")?;
102 } else if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 {
103 write!(f, "closing tx")?;
104 } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) &&
105 self.0.input[0].witness.len() == 5 {
106 write!(f, "HTLC-timeout tx")?;
107 } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) &&
108 self.0.input[0].witness.len() == 5 {
109 write!(f, "HTLC-success tx")?;
111 for inp in &self.0.input {
112 if !inp.witness.is_empty() {
113 if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) { write!(f, "preimage-")?; break }
114 else if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) { write!(f, "timeout-")?; break }
120 write!(f, "INVALID TRANSACTION")?;
126 macro_rules! log_tx {
128 ::util::macro_logger::DebugTx(&$obj)
132 pub(crate) struct DebugSpendable<'a>(pub &'a SpendableOutputDescriptor);
133 impl<'a> std::fmt::Display for DebugSpendable<'a> {
134 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
136 &SpendableOutputDescriptor::StaticOutput { ref outpoint, .. } => {
137 write!(f, "StaticOutput {}:{} marked for spending", outpoint.txid, outpoint.vout)?;
139 &SpendableOutputDescriptor::DynamicOutputP2WSH { ref outpoint, .. } => {
140 write!(f, "DynamicOutputP2WSH {}:{} marked for spending", outpoint.txid, outpoint.vout)?;
142 &SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, .. } => {
143 write!(f, "DynamicOutputP2WPKH {}:{} marked for spending", outpoint.txid, outpoint.vout)?;
150 macro_rules! log_spendable {
152 ::util::macro_logger::DebugSpendable(&$obj)
156 macro_rules! log_internal {
157 ($self: ident, $lvl:expr, $($arg:tt)+) => (
158 &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
162 macro_rules! log_error {
163 ($self: ident, $($arg:tt)*) => (
164 #[cfg(not(any(feature = "max_level_off")))]
165 log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
169 macro_rules! log_warn {
170 ($self: ident, $($arg:tt)*) => (
171 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
172 log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
176 macro_rules! log_info {
177 ($self: ident, $($arg:tt)*) => (
178 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
179 log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
183 macro_rules! log_debug {
184 ($self: ident, $($arg:tt)*) => (
185 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
186 log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
190 macro_rules! log_trace {
191 ($self: ident, $($arg:tt)*) => (
192 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
193 log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);