Merge pull request #53 from orenfromberg/patch-1
[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::str::FromStr;
17 use std::ffi::c_void;
18 use core::convert::Infallible;
19 use bitcoin::hashes::Hash;
20 use crate::c_types::*;
21
22 /// An enum representing the available verbosity levels of the logger.
23 #[must_use]
24 #[derive(Clone)]
25 #[repr(C)]
26 pub enum Level {
27         /// Designates extremely verbose information, including gossip-induced messages
28         Gossip,
29         /// Designates very low priority, often extremely verbose, information
30         Trace,
31         /// Designates lower priority information
32         Debug,
33         /// Designates useful information
34         Info,
35         /// Designates hazardous situations
36         Warn,
37         /// Designates very serious errors
38         Error,
39 }
40 use lightning::util::logger::Level as nativeLevel;
41 impl Level {
42         #[allow(unused)]
43         pub(crate) fn to_native(&self) -> nativeLevel {
44                 match self {
45                         Level::Gossip => nativeLevel::Gossip,
46                         Level::Trace => nativeLevel::Trace,
47                         Level::Debug => nativeLevel::Debug,
48                         Level::Info => nativeLevel::Info,
49                         Level::Warn => nativeLevel::Warn,
50                         Level::Error => nativeLevel::Error,
51                 }
52         }
53         #[allow(unused)]
54         pub(crate) fn into_native(self) -> nativeLevel {
55                 match self {
56                         Level::Gossip => nativeLevel::Gossip,
57                         Level::Trace => nativeLevel::Trace,
58                         Level::Debug => nativeLevel::Debug,
59                         Level::Info => nativeLevel::Info,
60                         Level::Warn => nativeLevel::Warn,
61                         Level::Error => nativeLevel::Error,
62                 }
63         }
64         #[allow(unused)]
65         pub(crate) fn from_native(native: &nativeLevel) -> Self {
66                 match native {
67                         nativeLevel::Gossip => Level::Gossip,
68                         nativeLevel::Trace => Level::Trace,
69                         nativeLevel::Debug => Level::Debug,
70                         nativeLevel::Info => Level::Info,
71                         nativeLevel::Warn => Level::Warn,
72                         nativeLevel::Error => Level::Error,
73                 }
74         }
75         #[allow(unused)]
76         pub(crate) fn native_into(native: nativeLevel) -> Self {
77                 match native {
78                         nativeLevel::Gossip => Level::Gossip,
79                         nativeLevel::Trace => Level::Trace,
80                         nativeLevel::Debug => Level::Debug,
81                         nativeLevel::Info => Level::Info,
82                         nativeLevel::Warn => Level::Warn,
83                         nativeLevel::Error => Level::Error,
84                 }
85         }
86 }
87 /// Creates a copy of the Level
88 #[no_mangle]
89 pub extern "C" fn Level_clone(orig: &Level) -> Level {
90         orig.clone()
91 }
92 #[no_mangle]
93 /// Utility method to constructs a new Gossip-variant Level
94 pub extern "C" fn Level_gossip() -> Level {
95         Level::Gossip}
96 #[no_mangle]
97 /// Utility method to constructs a new Trace-variant Level
98 pub extern "C" fn Level_trace() -> Level {
99         Level::Trace}
100 #[no_mangle]
101 /// Utility method to constructs a new Debug-variant Level
102 pub extern "C" fn Level_debug() -> Level {
103         Level::Debug}
104 #[no_mangle]
105 /// Utility method to constructs a new Info-variant Level
106 pub extern "C" fn Level_info() -> Level {
107         Level::Info}
108 #[no_mangle]
109 /// Utility method to constructs a new Warn-variant Level
110 pub extern "C" fn Level_warn() -> Level {
111         Level::Warn}
112 #[no_mangle]
113 /// Utility method to constructs a new Error-variant Level
114 pub extern "C" fn Level_error() -> Level {
115         Level::Error}
116 /// Checks if two Levels contain equal inner contents.
117 /// This ignores pointers and is_owned flags and looks at the values in fields.
118 #[no_mangle]
119 pub extern "C" fn Level_eq(a: &Level, b: &Level) -> bool {
120         if &a.to_native() == &b.to_native() { true } else { false }
121 }
122 /// Checks if two Levels contain equal inner contents.
123 #[no_mangle]
124 pub extern "C" fn Level_hash(o: &Level) -> u64 {
125         // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core
126         #[allow(deprecated)]
127         let mut hasher = core::hash::SipHasher::new();
128         std::hash::Hash::hash(&o.to_native(), &mut hasher);
129         std::hash::Hasher::finish(&hasher)
130 }
131 /// Returns the most verbose logging level.
132 #[must_use]
133 #[no_mangle]
134 pub extern "C" fn Level_max() -> crate::lightning::util::logger::Level {
135         let mut ret = lightning::util::logger::Level::max();
136         crate::lightning::util::logger::Level::native_into(ret)
137 }
138
139
140 use lightning::util::logger::Record as nativeRecordImport;
141 pub(crate) type nativeRecord = nativeRecordImport<'static>;
142
143 /// A Record, unit of logging output with Metadata to enable filtering
144 /// Module_path, file, line to inform on log's source
145 #[must_use]
146 #[repr(C)]
147 pub struct Record {
148         /// A pointer to the opaque Rust object.
149
150         /// Nearly everywhere, inner must be non-null, however in places where
151         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
152         pub inner: *mut nativeRecord,
153         /// Indicates that this is the only struct which contains the same pointer.
154
155         /// Rust functions which take ownership of an object provided via an argument require
156         /// this to be true and invalidate the object pointed to by inner.
157         pub is_owned: bool,
158 }
159
160 impl Drop for Record {
161         fn drop(&mut self) {
162                 if self.is_owned && !<*mut nativeRecord>::is_null(self.inner) {
163                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
164                 }
165         }
166 }
167 /// Frees any resources used by the Record, if is_owned is set and inner is non-NULL.
168 #[no_mangle]
169 pub extern "C" fn Record_free(this_obj: Record) { }
170 #[allow(unused)]
171 /// Used only if an object of this type is returned as a trait impl by a method
172 pub(crate) extern "C" fn Record_free_void(this_ptr: *mut c_void) {
173         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRecord); }
174 }
175 #[allow(unused)]
176 impl Record {
177         pub(crate) fn get_native_ref(&self) -> &'static nativeRecord {
178                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
179         }
180         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRecord {
181                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
182         }
183         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
184         pub(crate) fn take_inner(mut self) -> *mut nativeRecord {
185                 assert!(self.is_owned);
186                 let ret = ObjOps::untweak_ptr(self.inner);
187                 self.inner = std::ptr::null_mut();
188                 ret
189         }
190 }
191 /// The verbosity level of the message.
192 #[no_mangle]
193 pub extern "C" fn Record_get_level(this_ptr: &Record) -> crate::lightning::util::logger::Level {
194         let mut inner_val = &mut this_ptr.get_native_mut_ref().level;
195         crate::lightning::util::logger::Level::from_native(inner_val)
196 }
197 /// The verbosity level of the message.
198 #[no_mangle]
199 pub extern "C" fn Record_set_level(this_ptr: &mut Record, mut val: crate::lightning::util::logger::Level) {
200         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.level = val.into_native();
201 }
202 /// The message body.
203 #[no_mangle]
204 pub extern "C" fn Record_get_args(this_ptr: &Record) -> crate::c_types::Str {
205         let mut inner_val = &mut this_ptr.get_native_mut_ref().args;
206         inner_val.as_str().into()
207 }
208 /// The message body.
209 #[no_mangle]
210 pub extern "C" fn Record_set_args(this_ptr: &mut Record, mut val: crate::c_types::Str) {
211         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.args = val.into_string();
212 }
213 /// The module path of the message.
214 #[no_mangle]
215 pub extern "C" fn Record_get_module_path(this_ptr: &Record) -> crate::c_types::Str {
216         let mut inner_val = &mut this_ptr.get_native_mut_ref().module_path;
217         inner_val.into()
218 }
219 /// The module path of the message.
220 #[no_mangle]
221 pub extern "C" fn Record_set_module_path(this_ptr: &mut Record, mut val: crate::c_types::Str) {
222         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.module_path = val.into_str();
223 }
224 /// The source file containing the message.
225 #[no_mangle]
226 pub extern "C" fn Record_get_file(this_ptr: &Record) -> crate::c_types::Str {
227         let mut inner_val = &mut this_ptr.get_native_mut_ref().file;
228         inner_val.into()
229 }
230 /// The source file containing the message.
231 #[no_mangle]
232 pub extern "C" fn Record_set_file(this_ptr: &mut Record, mut val: crate::c_types::Str) {
233         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.file = val.into_str();
234 }
235 /// The line containing the message.
236 #[no_mangle]
237 pub extern "C" fn Record_get_line(this_ptr: &Record) -> u32 {
238         let mut inner_val = &mut this_ptr.get_native_mut_ref().line;
239         *inner_val
240 }
241 /// The line containing the message.
242 #[no_mangle]
243 pub extern "C" fn Record_set_line(this_ptr: &mut Record, mut val: u32) {
244         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.line = val;
245 }
246 impl Clone for Record {
247         fn clone(&self) -> Self {
248                 Self {
249                         inner: if <*mut nativeRecord>::is_null(self.inner) { std::ptr::null_mut() } else {
250                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
251                         is_owned: true,
252                 }
253         }
254 }
255 #[allow(unused)]
256 /// Used only if an object of this type is returned as a trait impl by a method
257 pub(crate) extern "C" fn Record_clone_void(this_ptr: *const c_void) -> *mut c_void {
258         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRecord)).clone() })) as *mut c_void
259 }
260 #[no_mangle]
261 /// Creates a copy of the Record
262 pub extern "C" fn Record_clone(orig: &Record) -> Record {
263         orig.clone()
264 }
265 /// A trait encapsulating the operations required of a logger
266 #[repr(C)]
267 pub struct Logger {
268         /// An opaque pointer which is passed to your function implementations as an argument.
269         /// This has no meaning in the LDK, and can be NULL or any other value.
270         pub this_arg: *mut c_void,
271         /// Logs the `Record`
272         pub log: extern "C" fn (this_arg: *const c_void, record: &crate::lightning::util::logger::Record),
273         /// Frees any resources associated with this object given its this_arg pointer.
274         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
275         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
276 }
277 unsafe impl Send for Logger {}
278 unsafe impl Sync for Logger {}
279 #[no_mangle]
280 pub(crate) extern "C" fn Logger_clone_fields(orig: &Logger) -> Logger {
281         Logger {
282                 this_arg: orig.this_arg,
283                 log: Clone::clone(&orig.log),
284                 free: Clone::clone(&orig.free),
285         }
286 }
287
288 use lightning::util::logger::Logger as rustLogger;
289 impl rustLogger for Logger {
290         fn log(&self, mut record: &lightning::util::logger::Record) {
291                 (self.log)(self.this_arg, &crate::lightning::util::logger::Record { inner: unsafe { ObjOps::nonnull_ptr_to_inner((record as *const lightning::util::logger::Record<'_, >) as *mut _) }, is_owned: false })
292         }
293 }
294
295 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
296 // directly as a Deref trait in higher-level structs:
297 impl std::ops::Deref for Logger {
298         type Target = Self;
299         fn deref(&self) -> &Self {
300                 self
301         }
302 }
303 /// Calls the free function if one is set
304 #[no_mangle]
305 pub extern "C" fn Logger_free(this_ptr: Logger) { }
306 impl Drop for Logger {
307         fn drop(&mut self) {
308                 if let Some(f) = self.free {
309                         f(self.this_arg);
310                 }
311         }
312 }