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