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