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::ln::ChannelId;
11 use crate::sign::SpendableOutputDescriptor;
13 use bitcoin::blockdata::transaction::Transaction;
15 use crate::routing::router::Route;
16 use crate::ln::chan_utils::HTLCClaim;
18 macro_rules! log_iter {
20 $crate::util::logger::DebugIter($obj)
24 /// Logs a pubkey in hex format.
26 macro_rules! log_pubkey {
28 $crate::util::logger::DebugPubKey(&$obj)
32 /// Logs a byte slice in hex format.
34 macro_rules! log_bytes {
36 $crate::util::logger::DebugBytes(&$obj)
40 pub(crate) struct DebugFundingInfo<'a>(pub &'a ChannelId);
41 impl<'a> core::fmt::Display for DebugFundingInfo<'a> {
42 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
46 macro_rules! log_funding_info {
47 ($key_storage: expr) => {
48 $crate::util::macro_logger::DebugFundingInfo(
49 &$key_storage.channel_id()
54 pub(crate) struct DebugRoute<'a>(pub &'a Route);
55 impl<'a> core::fmt::Display for DebugRoute<'a> {
56 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
57 for (idx, p) in self.0.paths.iter().enumerate() {
58 writeln!(f, "path {}:", idx)?;
59 for h in p.hops.iter() {
60 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)?;
62 writeln!(f, " blinded_tail: {:?}", p.blinded_tail)?;
67 macro_rules! log_route {
69 $crate::util::macro_logger::DebugRoute(&$obj)
73 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
74 impl<'a> core::fmt::Display for DebugTx<'a> {
75 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
76 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
77 let first_input = &self.0.input[0];
78 let witness_script_len = first_input.witness.last().unwrap_or(&[]).len();
79 if self.0.input.len() == 1 && witness_script_len == 71 &&
80 (first_input.sequence.0 >> 8*3) as u8 == 0x80 {
81 write!(f, "commitment tx ")?;
82 } else if self.0.input.len() == 1 && witness_script_len == 71 {
83 write!(f, "closing tx ")?;
84 } else if self.0.input.len() == 1 && HTLCClaim::from_witness(&first_input.witness) == Some(HTLCClaim::OfferedTimeout) {
85 write!(f, "HTLC-timeout tx ")?;
86 } else if self.0.input.len() == 1 && HTLCClaim::from_witness(&first_input.witness) == Some(HTLCClaim::AcceptedPreimage) {
87 write!(f, "HTLC-success tx ")?;
89 let mut num_preimage = 0;
90 let mut num_timeout = 0;
91 let mut num_revoked = 0;
92 for inp in &self.0.input {
93 let htlc_claim = HTLCClaim::from_witness(&inp.witness);
95 Some(HTLCClaim::AcceptedPreimage)|Some(HTLCClaim::OfferedPreimage) => num_preimage += 1,
96 Some(HTLCClaim::AcceptedTimeout)|Some(HTLCClaim::OfferedTimeout) => num_timeout += 1,
97 Some(HTLCClaim::Revocation) => num_revoked += 1,
101 if num_preimage > 0 || num_timeout > 0 || num_revoked > 0 {
102 write!(f, "HTLC claim tx ({} preimage, {} timeout, {} revoked) ",
103 num_preimage, num_timeout, num_revoked)?;
107 debug_assert!(false, "We should never generate unknown transaction types");
108 write!(f, "unknown tx type ").unwrap();
110 write!(f, "with txid {}", self.0.txid())?;
115 macro_rules! log_tx {
117 $crate::util::macro_logger::DebugTx(&$obj)
121 pub(crate) struct DebugSpendable<'a>(pub &'a SpendableOutputDescriptor);
122 impl<'a> core::fmt::Display for DebugSpendable<'a> {
123 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
125 &SpendableOutputDescriptor::StaticOutput { ref outpoint, .. } => {
126 write!(f, "StaticOutput {}:{} marked for spending", outpoint.txid, outpoint.index)?;
128 &SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) => {
129 write!(f, "DelayedPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
131 &SpendableOutputDescriptor::StaticPaymentOutput(ref descriptor) => {
132 write!(f, "StaticPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
139 macro_rules! log_spendable {
141 $crate::util::macro_logger::DebugSpendable(&$obj)
145 /// Create a new Record and log it. You probably don't want to use this macro directly,
146 /// but it needs to be exported so `log_trace` etc can use it in external crates.
149 macro_rules! log_internal {
150 ($logger: expr, $lvl:expr, $($arg:tt)+) => (
151 $logger.log($crate::util::logger::Record::new($lvl, None, None, format_args!($($arg)+), module_path!(), file!(), line!()))
155 /// Logs an entry at the given level.
158 macro_rules! log_given_level {
159 ($logger: expr, $lvl:expr, $($arg:tt)+) => (
161 #[cfg(not(any(feature = "max_level_off")))]
162 $crate::util::logger::Level::Error => $crate::log_internal!($logger, $lvl, $($arg)*),
163 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
164 $crate::util::logger::Level::Warn => $crate::log_internal!($logger, $lvl, $($arg)*),
165 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
166 $crate::util::logger::Level::Info => $crate::log_internal!($logger, $lvl, $($arg)*),
167 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
168 $crate::util::logger::Level::Debug => $crate::log_internal!($logger, $lvl, $($arg)*),
169 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
170 $crate::util::logger::Level::Trace => $crate::log_internal!($logger, $lvl, $($arg)*),
171 #[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")))]
172 $crate::util::logger::Level::Gossip => $crate::log_internal!($logger, $lvl, $($arg)*),
174 #[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"))]
176 // The level is disabled at compile-time
182 /// Log at the `ERROR` level.
184 macro_rules! log_error {
185 ($logger: expr, $($arg:tt)*) => (
186 $crate::log_given_level!($logger, $crate::util::logger::Level::Error, $($arg)*);
190 /// Log at the `WARN` level.
192 macro_rules! log_warn {
193 ($logger: expr, $($arg:tt)*) => (
194 $crate::log_given_level!($logger, $crate::util::logger::Level::Warn, $($arg)*);
198 /// Log at the `INFO` level.
200 macro_rules! log_info {
201 ($logger: expr, $($arg:tt)*) => (
202 $crate::log_given_level!($logger, $crate::util::logger::Level::Info, $($arg)*);
206 /// Log at the `DEBUG` level.
208 macro_rules! log_debug {
209 ($logger: expr, $($arg:tt)*) => (
210 $crate::log_given_level!($logger, $crate::util::logger::Level::Debug, $($arg)*);
214 /// Log at the `TRACE` level.
216 macro_rules! log_trace {
217 ($logger: expr, $($arg:tt)*) => (
218 $crate::log_given_level!($logger, $crate::util::logger::Level::Trace, $($arg)*)
222 /// Log at the `GOSSIP` level.
224 macro_rules! log_gossip {
225 ($logger: expr, $($arg:tt)*) => (
226 $crate::log_given_level!($logger, $crate::util::logger::Level::Gossip, $($arg)*);