Rebuild against current RL main
[ldk-c-bindings] / lightning-c-bindings / src / util / logger.rs
1 //! Log traits live here, which are called throughout the library to provide useful information for
2 //! debugging purposes.
3 //!
4 //! There is currently 2 ways to filter log messages. First one, by using compilation features, e.g \"max_level_off\".
5 //! The second one, client-side by implementing check against Record Level field.
6 //! Each module may have its own Logger or share one.
7
8 use std::ffi::c_void;
9 use bitcoin::hashes::Hash;
10 use crate::c_types::*;
11
12 /// An enum representing the available verbosity levels of the logger.
13 #[must_use]
14 #[derive(Clone)]
15 #[repr(C)]
16 pub enum Level {
17         ///Designates logger being silent
18         Off,
19         /// Designates very serious errors
20         Error,
21         /// Designates hazardous situations
22         Warn,
23         /// Designates useful information
24         Info,
25         /// Designates lower priority information
26         Debug,
27         /// Designates very low priority, often extremely verbose, information
28         Trace,
29 }
30 use lightning::util::logger::Level as nativeLevel;
31 impl Level {
32         #[allow(unused)]
33         pub(crate) fn to_native(&self) -> nativeLevel {
34                 match self {
35                         Level::Off => nativeLevel::Off,
36                         Level::Error => nativeLevel::Error,
37                         Level::Warn => nativeLevel::Warn,
38                         Level::Info => nativeLevel::Info,
39                         Level::Debug => nativeLevel::Debug,
40                         Level::Trace => nativeLevel::Trace,
41                 }
42         }
43         #[allow(unused)]
44         pub(crate) fn into_native(self) -> nativeLevel {
45                 match self {
46                         Level::Off => nativeLevel::Off,
47                         Level::Error => nativeLevel::Error,
48                         Level::Warn => nativeLevel::Warn,
49                         Level::Info => nativeLevel::Info,
50                         Level::Debug => nativeLevel::Debug,
51                         Level::Trace => nativeLevel::Trace,
52                 }
53         }
54         #[allow(unused)]
55         pub(crate) fn from_native(native: &nativeLevel) -> Self {
56                 match native {
57                         nativeLevel::Off => Level::Off,
58                         nativeLevel::Error => Level::Error,
59                         nativeLevel::Warn => Level::Warn,
60                         nativeLevel::Info => Level::Info,
61                         nativeLevel::Debug => Level::Debug,
62                         nativeLevel::Trace => Level::Trace,
63                 }
64         }
65         #[allow(unused)]
66         pub(crate) fn native_into(native: nativeLevel) -> Self {
67                 match native {
68                         nativeLevel::Off => Level::Off,
69                         nativeLevel::Error => Level::Error,
70                         nativeLevel::Warn => Level::Warn,
71                         nativeLevel::Info => Level::Info,
72                         nativeLevel::Debug => Level::Debug,
73                         nativeLevel::Trace => Level::Trace,
74                 }
75         }
76 }
77 /// Creates a copy of the Level
78 #[no_mangle]
79 pub extern "C" fn Level_clone(orig: &Level) -> Level {
80         orig.clone()
81 }
82 /// Returns the most verbose logging level.
83 #[must_use]
84 #[no_mangle]
85 pub extern "C" fn Level_max() -> crate::util::logger::Level {
86         let mut ret = lightning::util::logger::Level::max();
87         crate::util::logger::Level::native_into(ret)
88 }
89
90 /// A trait encapsulating the operations required of a logger
91 #[repr(C)]
92 pub struct Logger {
93         /// An opaque pointer which is passed to your function implementations as an argument.
94         /// This has no meaning in the LDK, and can be NULL or any other value.
95         pub this_arg: *mut c_void,
96         /// Logs the `Record`
97         pub log: extern "C" fn (this_arg: *const c_void, record: *const std::os::raw::c_char),
98 /// Frees any resources associated with this object given its this_arg pointer.
99 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
100         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
101 }
102 unsafe impl Sync for Logger {}
103 unsafe impl Send for Logger {}
104
105 use lightning::util::logger::Logger as rustLogger;
106 impl rustLogger for Logger {
107         fn log(&self, record: &lightning::util::logger::Record) {
108                 let mut local_record = std::ffi::CString::new(format!("{}", record.args)).unwrap();
109                 (self.log)(self.this_arg, local_record.as_ptr())
110         }
111 }
112
113 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
114 // directly as a Deref trait in higher-level structs:
115 impl std::ops::Deref for Logger {
116         type Target = Self;
117         fn deref(&self) -> &Self {
118                 self
119         }
120 }
121 /// Calls the free function if one is set
122 #[no_mangle]
123 pub extern "C" fn Logger_free(this_ptr: Logger) { }
124 impl Drop for Logger {
125         fn drop(&mut self) {
126                 if let Some(f) = self.free {
127                         f(self.this_arg);
128                 }
129         }
130 }