19f0294f9c220a52942a48ee48f58238c0fe52ba
[rust-lightning] / src / util / macro_logger.rs
1 use chain::transaction::OutPoint;
2
3 use bitcoin::util::hash::Sha256dHash;
4 use secp256k1::key::PublicKey;
5
6 use std;
7
8 pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey);
9 impl<'a> std::fmt::Display for DebugPubKey<'a> {
10         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
11                 for i in self.0.serialize().iter() {
12                         write!(f, "{:02x}", i)?;
13                 }
14                 Ok(())
15         }
16 }
17 macro_rules! log_pubkey {
18         ($obj: expr) => {
19                 ::util::macro_logger::DebugPubKey(&$obj)
20         }
21 }
22
23 pub(crate) struct DebugBytes<'a>(pub &'a [u8; 32]);
24 impl<'a> std::fmt::Display for DebugBytes<'a> {
25         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
26                 for i in self.0 {
27                         write!(f, "{:02x}", i)?;
28                 }
29                 Ok(())
30         }
31 }
32 macro_rules! log_bytes {
33         ($obj: expr) => {
34                 ::util::macro_logger::DebugBytes(&$obj)
35         }
36 }
37
38 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Sha256dHash, pub u16);
39 impl<'a> std::fmt::Display for DebugFundingChannelId<'a> {
40         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
41                 for i in OutPoint::new(self.0.clone(), self.1).to_channel_id().iter() {
42                         write!(f, "{:02x}", i)?;
43                 }
44                 Ok(())
45         }
46 }
47 macro_rules! log_funding_channel_id {
48         ($funding_txid: expr, $funding_txo: expr) => {
49                 ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
50         }
51 }
52
53 macro_rules! log_internal {
54         ($self: ident, $lvl:expr, $($arg:tt)+) => (
55                 &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
56         );
57 }
58
59 macro_rules! log_error {
60         ($self: ident, $($arg:tt)*) => (
61                 #[cfg(not(any(feature = "max_level_off")))]
62                 log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
63         )
64 }
65
66 macro_rules! log_warn {
67         ($self: ident, $($arg:tt)*) => (
68                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
69                 log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
70         )
71 }
72
73 macro_rules! log_info {
74         ($self: ident, $($arg:tt)*) => (
75                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
76                 log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
77         )
78 }
79
80 macro_rules! log_debug {
81         ($self: ident, $($arg:tt)*) => (
82                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
83                 log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
84         )
85 }
86
87 macro_rules! log_trace {
88         ($self: ident, $($arg:tt)*) => (
89                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
90                 log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);
91         )
92 }