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