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 crate::chain::transaction::OutPoint;
11 use crate::sign::SpendableOutputDescriptor;
13 use bitcoin::hash_types::Txid;
14 use bitcoin::blockdata::transaction::Transaction;
16 use crate::routing::router::Route;
17 use crate::ln::chan_utils::HTLCClaim;
19 macro_rules! log_iter {
21 $crate::util::logger::DebugIter($obj)
25 /// Logs a pubkey in hex format.
27 macro_rules! log_pubkey {
29 $crate::util::logger::DebugPubKey(&$obj)
33 /// Logs a byte slice in hex format.
35 macro_rules! log_bytes {
37 $crate::util::logger::DebugBytes(&$obj)
41 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Txid, pub u16);
42 impl<'a> core::fmt::Display for DebugFundingChannelId<'a> {
43 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
44 (OutPoint { txid: self.0.clone(), index: self.1 }).to_channel_id().fmt(f)
47 macro_rules! log_funding_channel_id {
48 ($funding_txid: expr, $funding_txo: expr) => {
49 $crate::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
53 pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a (OutPoint, T));
54 impl<'a, T> core::fmt::Display for DebugFundingInfo<'a, T> {
55 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
56 (self.0).0.to_channel_id().fmt(f)
59 macro_rules! log_funding_info {
60 ($key_storage: expr) => {
61 $crate::util::macro_logger::DebugFundingInfo(&$key_storage.get_funding_txo())
65 pub(crate) struct DebugRoute<'a>(pub &'a Route);
66 impl<'a> core::fmt::Display for DebugRoute<'a> {
67 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
68 for (idx, p) in self.0.paths.iter().enumerate() {
69 writeln!(f, "path {}:", idx)?;
70 for h in p.hops.iter() {
71 writeln!(f, " node_id: {}, short_channel_id: {}, fee_msat: {}, cltv_expiry_delta: {}", log_pubkey!(h.pubkey), h.short_channel_id, h.fee_msat, h.cltv_expiry_delta)?;
73 writeln!(f, " blinded_tail: {:?}", p.blinded_tail)?;
78 macro_rules! log_route {
80 $crate::util::macro_logger::DebugRoute(&$obj)
84 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
85 impl<'a> core::fmt::Display for DebugTx<'a> {
86 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
87 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
88 let first_input = &self.0.input[0];
89 let witness_script_len = first_input.witness.last().unwrap_or(&[]).len();
90 if self.0.input.len() == 1 && witness_script_len == 71 &&
91 (first_input.sequence.0 >> 8*3) as u8 == 0x80 {
92 write!(f, "commitment tx ")?;
93 } else if self.0.input.len() == 1 && witness_script_len == 71 {
94 write!(f, "closing tx ")?;
95 } else if self.0.input.len() == 1 && HTLCClaim::from_witness(&first_input.witness) == Some(HTLCClaim::OfferedTimeout) {
96 write!(f, "HTLC-timeout tx ")?;
97 } else if self.0.input.len() == 1 && HTLCClaim::from_witness(&first_input.witness) == Some(HTLCClaim::AcceptedPreimage) {
98 write!(f, "HTLC-success tx ")?;
100 let mut num_preimage = 0;
101 let mut num_timeout = 0;
102 let mut num_revoked = 0;
103 for inp in &self.0.input {
104 let htlc_claim = HTLCClaim::from_witness(&inp.witness);
106 Some(HTLCClaim::AcceptedPreimage)|Some(HTLCClaim::OfferedPreimage) => num_preimage += 1,
107 Some(HTLCClaim::AcceptedTimeout)|Some(HTLCClaim::OfferedTimeout) => num_timeout += 1,
108 Some(HTLCClaim::Revocation) => num_revoked += 1,
112 if num_preimage > 0 || num_timeout > 0 || num_revoked > 0 {
113 write!(f, "HTLC claim tx ({} preimage, {} timeout, {} revoked) ",
114 num_preimage, num_timeout, num_revoked)?;
118 debug_assert!(false, "We should never generate unknown transaction types");
119 write!(f, "unknown tx type ").unwrap();
121 write!(f, "with txid {}", self.0.txid())?;
126 macro_rules! log_tx {
128 $crate::util::macro_logger::DebugTx(&$obj)
132 pub(crate) struct DebugSpendable<'a>(pub &'a SpendableOutputDescriptor);
133 impl<'a> core::fmt::Display for DebugSpendable<'a> {
134 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
136 &SpendableOutputDescriptor::StaticOutput { ref outpoint, .. } => {
137 write!(f, "StaticOutput {}:{} marked for spending", outpoint.txid, outpoint.index)?;
139 &SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) => {
140 write!(f, "DelayedPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
142 &SpendableOutputDescriptor::StaticPaymentOutput(ref descriptor) => {
143 write!(f, "StaticPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
150 macro_rules! log_spendable {
152 $crate::util::macro_logger::DebugSpendable(&$obj)
156 /// Create a new Record and log it. You probably don't want to use this macro directly,
157 /// but it needs to be exported so `log_trace` etc can use it in external crates.
160 macro_rules! log_internal {
161 ($logger: expr, $lvl:expr, $($arg:tt)+) => (
162 $logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()))
166 /// Logs an entry at the given level.
169 macro_rules! log_given_level {
170 ($logger: expr, $lvl:expr, $($arg:tt)+) => (
172 #[cfg(not(any(feature = "max_level_off")))]
173 $crate::util::logger::Level::Error => $crate::log_internal!($logger, $lvl, $($arg)*),
174 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
175 $crate::util::logger::Level::Warn => $crate::log_internal!($logger, $lvl, $($arg)*),
176 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
177 $crate::util::logger::Level::Info => $crate::log_internal!($logger, $lvl, $($arg)*),
178 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
179 $crate::util::logger::Level::Debug => $crate::log_internal!($logger, $lvl, $($arg)*),
180 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
181 $crate::util::logger::Level::Trace => $crate::log_internal!($logger, $lvl, $($arg)*),
182 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug", feature = "max_level_trace")))]
183 $crate::util::logger::Level::Gossip => $crate::log_internal!($logger, $lvl, $($arg)*),
185 #[cfg(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug", feature = "max_level_trace"))]
187 // The level is disabled at compile-time
193 /// Log at the `ERROR` level.
195 macro_rules! log_error {
196 ($logger: expr, $($arg:tt)*) => (
197 $crate::log_given_level!($logger, $crate::util::logger::Level::Error, $($arg)*);
201 /// Log at the `WARN` level.
203 macro_rules! log_warn {
204 ($logger: expr, $($arg:tt)*) => (
205 $crate::log_given_level!($logger, $crate::util::logger::Level::Warn, $($arg)*);
209 /// Log at the `INFO` level.
211 macro_rules! log_info {
212 ($logger: expr, $($arg:tt)*) => (
213 $crate::log_given_level!($logger, $crate::util::logger::Level::Info, $($arg)*);
217 /// Log at the `DEBUG` level.
219 macro_rules! log_debug {
220 ($logger: expr, $($arg:tt)*) => (
221 $crate::log_given_level!($logger, $crate::util::logger::Level::Debug, $($arg)*);
225 /// Log at the `TRACE` level.
227 macro_rules! log_trace {
228 ($logger: expr, $($arg:tt)*) => (
229 $crate::log_given_level!($logger, $crate::util::logger::Level::Trace, $($arg)*)
233 /// Log at the `GOSSIP` level.
235 macro_rules! log_gossip {
236 ($logger: expr, $($arg:tt)*) => (
237 $crate::log_given_level!($logger, $crate::util::logger::Level::Gossip, $($arg)*);