1 use chain::transaction::OutPoint;
3 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
4 use bitcoin::blockdata::transaction::Transaction;
5 use secp256k1::key::PublicKey;
8 use ln::chan_utils::HTLCType;
12 pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey);
13 impl<'a> std::fmt::Display for DebugPubKey<'a> {
14 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
15 for i in self.0.serialize().iter() {
16 write!(f, "{:02x}", i)?;
21 macro_rules! log_pubkey {
23 ::util::macro_logger::DebugPubKey(&$obj)
27 pub(crate) struct DebugBytes<'a>(pub &'a [u8]);
28 impl<'a> std::fmt::Display for DebugBytes<'a> {
29 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
31 write!(f, "{:02x}", i)?;
36 macro_rules! log_bytes {
38 ::util::macro_logger::DebugBytes(&$obj)
42 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Sha256dHash, pub u16);
43 impl<'a> std::fmt::Display for DebugFundingChannelId<'a> {
44 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
45 for i in OutPoint::new(self.0.clone(), self.1).to_channel_id().iter() {
46 write!(f, "{:02x}", i)?;
51 macro_rules! log_funding_channel_id {
52 ($funding_txid: expr, $funding_txo: expr) => {
53 ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
57 pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a Option<(OutPoint, T)>);
58 impl<'a, T> std::fmt::Display for DebugFundingInfo<'a, T> {
59 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
60 match self.0.as_ref() {
61 Some(&(ref funding_output, _)) => DebugBytes(&funding_output.to_channel_id()[..]).fmt(f),
62 None => write!(f, "without funding output set"),
66 macro_rules! log_funding_info {
67 ($key_storage: expr) => {
69 Storage::Local { ref funding_info, .. } => {
70 ::util::macro_logger::DebugFundingInfo(&funding_info)
72 Storage::Watchtower { .. } => {
73 ::util::macro_logger::DebugFundingInfo(&None)
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 macro_rules! log_internal {
135 ($self: ident, $lvl:expr, $($arg:tt)+) => (
136 &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
140 macro_rules! log_error {
141 ($self: ident, $($arg:tt)*) => (
142 #[cfg(not(any(feature = "max_level_off")))]
143 log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
147 macro_rules! log_warn {
148 ($self: ident, $($arg:tt)*) => (
149 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
150 log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
154 macro_rules! log_info {
155 ($self: ident, $($arg:tt)*) => (
156 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
157 log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
161 macro_rules! log_debug {
162 ($self: ident, $($arg:tt)*) => (
163 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
164 log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
168 macro_rules! log_trace {
169 ($self: ident, $($arg:tt)*) => (
170 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
171 log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);