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