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 h in self.0.hops.iter() {
83 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)?;
88 macro_rules! log_route {
90 ::util::macro_logger::DebugRoute(&$obj)
94 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
95 impl<'a> std::fmt::Display for DebugTx<'a> {
96 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
97 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
98 if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 &&
99 (self.0.input[0].sequence >> 8*3) as u8 == 0x80 {
100 write!(f, "commitment tx")?;
101 } else if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 {
102 write!(f, "closing tx")?;
103 } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) &&
104 self.0.input[0].witness.len() == 5 {
105 write!(f, "HTLC-timeout tx")?;
106 } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) &&
107 self.0.input[0].witness.len() == 5 {
108 write!(f, "HTLC-success tx")?;
110 for inp in &self.0.input {
111 if !inp.witness.is_empty() {
112 if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) { write!(f, "preimage-")?; break }
113 else if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) { write!(f, "timeout-")?; break }
119 write!(f, "INVALID TRANSACTION")?;
125 macro_rules! log_tx {
127 ::util::macro_logger::DebugTx(&$obj)
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!()));
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)*);
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)*);
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)*);
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)*);
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)*);