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::chain::keysinterface::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;
18 use crate::util::logger::DebugBytes;
20 /// Logs a pubkey in hex format.
22 macro_rules! log_pubkey {
24 $crate::util::logger::DebugPubKey(&$obj)
28 /// Logs a byte slice in hex format.
30 macro_rules! log_bytes {
32 $crate::util::logger::DebugBytes(&$obj)
36 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Txid, pub u16);
37 impl<'a> core::fmt::Display for DebugFundingChannelId<'a> {
38 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
39 for i in (OutPoint { txid: self.0.clone(), index: self.1 }).to_channel_id().iter() {
40 write!(f, "{:02x}", i)?;
45 macro_rules! log_funding_channel_id {
46 ($funding_txid: expr, $funding_txo: expr) => {
47 $crate::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
51 pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a (OutPoint, T));
52 impl<'a, T> core::fmt::Display for DebugFundingInfo<'a, T> {
53 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
54 DebugBytes(&(self.0).0.to_channel_id()[..]).fmt(f)
57 macro_rules! log_funding_info {
58 ($key_storage: expr) => {
59 $crate::util::macro_logger::DebugFundingInfo(&$key_storage.get_funding_txo())
63 pub(crate) struct DebugRoute<'a>(pub &'a Route);
64 impl<'a> core::fmt::Display for DebugRoute<'a> {
65 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
66 for (idx, p) in self.0.paths.iter().enumerate() {
67 writeln!(f, "path {}:", idx)?;
69 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)?;
75 macro_rules! log_route {
77 $crate::util::macro_logger::DebugRoute(&$obj)
81 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
82 impl<'a> core::fmt::Display for DebugTx<'a> {
83 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
84 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
85 let first_input = &self.0.input[0];
86 let witness_script_len = first_input.witness.last().unwrap_or(&[]).len();
87 if self.0.input.len() == 1 && witness_script_len == 71 &&
88 (first_input.sequence.0 >> 8*3) as u8 == 0x80 {
89 write!(f, "commitment tx ")?;
90 } else if self.0.input.len() == 1 && witness_script_len == 71 {
91 write!(f, "closing tx ")?;
92 } else if self.0.input.len() == 1 && HTLCClaim::from_witness(&first_input.witness) == Some(HTLCClaim::OfferedTimeout) {
93 write!(f, "HTLC-timeout tx ")?;
94 } else if self.0.input.len() == 1 && HTLCClaim::from_witness(&first_input.witness) == Some(HTLCClaim::AcceptedPreimage) {
95 write!(f, "HTLC-success tx ")?;
97 let mut num_preimage = 0;
98 let mut num_timeout = 0;
99 let mut num_revoked = 0;
100 for inp in &self.0.input {
101 let htlc_claim = HTLCClaim::from_witness(&inp.witness);
103 Some(HTLCClaim::AcceptedPreimage)|Some(HTLCClaim::OfferedPreimage) => num_preimage += 1,
104 Some(HTLCClaim::AcceptedTimeout)|Some(HTLCClaim::OfferedTimeout) => num_timeout += 1,
105 Some(HTLCClaim::Revocation) => num_revoked += 1,
109 if num_preimage > 0 || num_timeout > 0 || num_revoked > 0 {
110 write!(f, "HTLC claim tx ({} preimage, {} timeout, {} revoked)",
111 num_preimage, num_timeout, num_revoked)?;
115 debug_assert!(false, "We should never generate unknown transaction types");
116 write!(f, "unknown tx type ").unwrap();
118 write!(f, "with txid {}", self.0.txid())?;
123 macro_rules! log_tx {
125 $crate::util::macro_logger::DebugTx(&$obj)
129 pub(crate) struct DebugSpendable<'a>(pub &'a SpendableOutputDescriptor);
130 impl<'a> core::fmt::Display for DebugSpendable<'a> {
131 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
133 &SpendableOutputDescriptor::StaticOutput { ref outpoint, .. } => {
134 write!(f, "StaticOutput {}:{} marked for spending", outpoint.txid, outpoint.index)?;
136 &SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) => {
137 write!(f, "DelayedPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
139 &SpendableOutputDescriptor::StaticPaymentOutput(ref descriptor) => {
140 write!(f, "StaticPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
147 macro_rules! log_spendable {
149 $crate::util::macro_logger::DebugSpendable(&$obj)
153 /// Create a new Record and log it. You probably don't want to use this macro directly,
154 /// but it needs to be exported so `log_trace` etc can use it in external crates.
157 macro_rules! log_internal {
158 ($logger: expr, $lvl:expr, $($arg:tt)+) => (
159 $logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()))
163 /// Logs an entry at the given level.
166 macro_rules! log_given_level {
167 ($logger: expr, $lvl:expr, $($arg:tt)+) => (
169 #[cfg(not(any(feature = "max_level_off")))]
170 $crate::util::logger::Level::Error => log_internal!($logger, $lvl, $($arg)*),
171 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
172 $crate::util::logger::Level::Warn => log_internal!($logger, $lvl, $($arg)*),
173 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
174 $crate::util::logger::Level::Info => log_internal!($logger, $lvl, $($arg)*),
175 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
176 $crate::util::logger::Level::Debug => log_internal!($logger, $lvl, $($arg)*),
177 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
178 $crate::util::logger::Level::Trace => log_internal!($logger, $lvl, $($arg)*),
179 #[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")))]
180 $crate::util::logger::Level::Gossip => log_internal!($logger, $lvl, $($arg)*),
182 #[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"))]
184 // The level is disabled at compile-time
190 /// Log at the `ERROR` level.
192 macro_rules! log_error {
193 ($logger: expr, $($arg:tt)*) => (
194 log_given_level!($logger, $crate::util::logger::Level::Error, $($arg)*);
198 /// Log at the `WARN` level.
200 macro_rules! log_warn {
201 ($logger: expr, $($arg:tt)*) => (
202 log_given_level!($logger, $crate::util::logger::Level::Warn, $($arg)*);
206 /// Log at the `INFO` level.
208 macro_rules! log_info {
209 ($logger: expr, $($arg:tt)*) => (
210 log_given_level!($logger, $crate::util::logger::Level::Info, $($arg)*);
214 /// Log at the `DEBUG` level.
216 macro_rules! log_debug {
217 ($logger: expr, $($arg:tt)*) => (
218 log_given_level!($logger, $crate::util::logger::Level::Debug, $($arg)*);
222 /// Log at the `TRACE` level.
224 macro_rules! log_trace {
225 ($logger: expr, $($arg:tt)*) => (
226 log_given_level!($logger, $crate::util::logger::Level::Trace, $($arg)*)
230 /// Log at the `GOSSIP` level.
232 macro_rules! log_gossip {
233 ($logger: expr, $($arg:tt)*) => (
234 log_given_level!($logger, $crate::util::logger::Level::Gossip, $($arg)*);