1f68542773e462bd9623f24d27562d0d11339caa
[rust-lightning] / lightning / src / util / macro_logger.rs
1 use chain::transaction::OutPoint;
2
3 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
4 use secp256k1::key::PublicKey;
5
6 use ln::router::Route;
7
8 use std;
9
10 pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey);
11 impl<'a> std::fmt::Display for DebugPubKey<'a> {
12         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
13                 for i in self.0.serialize().iter() {
14                         write!(f, "{:02x}", i)?;
15                 }
16                 Ok(())
17         }
18 }
19 macro_rules! log_pubkey {
20         ($obj: expr) => {
21                 ::util::macro_logger::DebugPubKey(&$obj)
22         }
23 }
24
25 pub(crate) struct DebugBytes<'a>(pub &'a [u8]);
26 impl<'a> std::fmt::Display for DebugBytes<'a> {
27         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
28                 for i in self.0 {
29                         write!(f, "{:02x}", i)?;
30                 }
31                 Ok(())
32         }
33 }
34 macro_rules! log_bytes {
35         ($obj: expr) => {
36                 ::util::macro_logger::DebugBytes(&$obj)
37         }
38 }
39
40 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Sha256dHash, pub u16);
41 impl<'a> std::fmt::Display for DebugFundingChannelId<'a> {
42         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
43                 for i in OutPoint::new(self.0.clone(), self.1).to_channel_id().iter() {
44                         write!(f, "{:02x}", i)?;
45                 }
46                 Ok(())
47         }
48 }
49 macro_rules! log_funding_channel_id {
50         ($funding_txid: expr, $funding_txo: expr) => {
51                 ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
52         }
53 }
54
55 pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a Option<(OutPoint, T)>);
56 impl<'a, T> std::fmt::Display for DebugFundingInfo<'a, T> {
57         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
58                 match self.0.as_ref() {
59                         Some(&(ref funding_output, _)) => DebugBytes(&funding_output.to_channel_id()[..]).fmt(f),
60                         None => write!(f, "without funding output set"),
61                 }
62         }
63 }
64 macro_rules! log_funding_info {
65         ($key_storage: expr) => {
66                 match $key_storage {
67                         Storage::Local { ref funding_info, .. } => {
68                                 ::util::macro_logger::DebugFundingInfo(&funding_info)
69                         },
70                         Storage::Watchtower { .. } => {
71                                 ::util::macro_logger::DebugFundingInfo(&None)
72                         }
73                 }
74         }
75 }
76
77 pub(crate) struct DebugRoute<'a>(pub &'a Route);
78 impl<'a> std::fmt::Display for DebugRoute<'a> {
79         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
80                 for h in self.0.hops.iter() {
81                         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)?;
82                 }
83                 Ok(())
84         }
85 }
86 macro_rules! log_route {
87         ($obj: expr) => {
88                 ::util::macro_logger::DebugRoute(&$obj)
89         }
90 }
91
92 macro_rules! log_internal {
93         ($self: ident, $lvl:expr, $($arg:tt)+) => (
94                 &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
95         );
96 }
97
98 macro_rules! log_error {
99         ($self: ident, $($arg:tt)*) => (
100                 #[cfg(not(any(feature = "max_level_off")))]
101                 log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
102         )
103 }
104
105 macro_rules! log_warn {
106         ($self: ident, $($arg:tt)*) => (
107                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
108                 log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
109         )
110 }
111
112 macro_rules! log_info {
113         ($self: ident, $($arg:tt)*) => (
114                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
115                 log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
116         )
117 }
118
119 macro_rules! log_debug {
120         ($self: ident, $($arg:tt)*) => (
121                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
122                 log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
123         )
124 }
125
126 macro_rules! log_trace {
127         ($self: ident, $($arg:tt)*) => (
128                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
129                 log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);
130         )
131 }