1 // This file is Copyright its original authors, visible in version control
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
10 use chain::transaction::OutPoint;
11 use chain::keysinterface::SpendableOutputDescriptor;
13 use bitcoin::hash_types::Txid;
14 use bitcoin::blockdata::transaction::Transaction;
15 use bitcoin::secp256k1::key::PublicKey;
17 use routing::router::Route;
18 use ln::chan_utils::HTLCType;
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)?;
31 macro_rules! log_pubkey {
33 ::util::macro_logger::DebugPubKey(&$obj)
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> {
41 write!(f, "{:02x}", i)?;
46 macro_rules! log_bytes {
48 ::util::macro_logger::DebugBytes(&$obj)
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)?;
61 macro_rules! log_funding_channel_id {
62 ($funding_txid: expr, $funding_txo: expr) => {
63 ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
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)
73 macro_rules! log_funding_info {
74 ($key_storage: expr) => {
75 ::util::macro_logger::DebugFundingInfo(&$key_storage.funding_info)
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 write!(f, "path {}:\n", idx)?;
85 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)?;
91 macro_rules! log_route {
93 ::util::macro_logger::DebugRoute(&$obj)
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")?;
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 }
122 write!(f, "INVALID TRANSACTION")?;
128 macro_rules! log_tx {
130 ::util::macro_logger::DebugTx(&$obj)
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> {
138 &SpendableOutputDescriptor::StaticOutput { ref outpoint, .. } => {
139 write!(f, "StaticOutput {}:{} marked for spending", outpoint.txid, outpoint.index)?;
141 &SpendableOutputDescriptor::DynamicOutputP2WSH { ref outpoint, .. } => {
142 write!(f, "DynamicOutputP2WSH {}:{} marked for spending", outpoint.txid, outpoint.index)?;
144 &SpendableOutputDescriptor::StaticOutputRemotePayment { ref outpoint, .. } => {
145 write!(f, "DynamicOutputP2WPKH {}:{} marked for spending", outpoint.txid, outpoint.index)?;
152 macro_rules! log_spendable {
154 ::util::macro_logger::DebugSpendable(&$obj)
158 macro_rules! log_internal {
159 ($logger: expr, $lvl:expr, $($arg:tt)+) => (
160 $logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
164 macro_rules! log_error {
165 ($logger: expr, $($arg:tt)*) => (
166 #[cfg(not(any(feature = "max_level_off")))]
167 log_internal!($logger, $crate::util::logger::Level::Error, $($arg)*);
171 macro_rules! log_warn {
172 ($logger: expr, $($arg:tt)*) => (
173 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
174 log_internal!($logger, $crate::util::logger::Level::Warn, $($arg)*);
178 macro_rules! log_info {
179 ($logger: expr, $($arg:tt)*) => (
180 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
181 log_internal!($logger, $crate::util::logger::Level::Info, $($arg)*);
185 macro_rules! log_debug {
186 ($logger: expr, $($arg:tt)*) => (
187 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
188 log_internal!($logger, $crate::util::logger::Level::Debug, $($arg)*);
192 macro_rules! log_trace {
193 ($logger: expr, $($arg:tt)*) => (
194 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
195 log_internal!($logger, $crate::util::logger::Level::Trace, $($arg)*);