Regenerate 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 std::str::FromStr;
17 use std::ffi::c_void;
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
20
21 /// An enum representing the available verbosity levels of the logger.
22 #[must_use]
23 #[derive(Clone)]
24 #[repr(C)]
25 pub enum Level {
26         ///Designates logger being silent
27         Off,
28         /// Designates very serious errors
29         Error,
30         /// Designates hazardous situations
31         Warn,
32         /// Designates useful information
33         Info,
34         /// Designates lower priority information
35         Debug,
36         /// Designates very low priority, often extremely verbose, information
37         Trace,
38 }
39 use lightning::util::logger::Level as nativeLevel;
40 impl Level {
41         #[allow(unused)]
42         pub(crate) fn to_native(&self) -> nativeLevel {
43                 match self {
44                         Level::Off => nativeLevel::Off,
45                         Level::Error => nativeLevel::Error,
46                         Level::Warn => nativeLevel::Warn,
47                         Level::Info => nativeLevel::Info,
48                         Level::Debug => nativeLevel::Debug,
49                         Level::Trace => nativeLevel::Trace,
50                 }
51         }
52         #[allow(unused)]
53         pub(crate) fn into_native(self) -> nativeLevel {
54                 match self {
55                         Level::Off => nativeLevel::Off,
56                         Level::Error => nativeLevel::Error,
57                         Level::Warn => nativeLevel::Warn,
58                         Level::Info => nativeLevel::Info,
59                         Level::Debug => nativeLevel::Debug,
60                         Level::Trace => nativeLevel::Trace,
61                 }
62         }
63         #[allow(unused)]
64         pub(crate) fn from_native(native: &nativeLevel) -> Self {
65                 match native {
66                         nativeLevel::Off => Level::Off,
67                         nativeLevel::Error => Level::Error,
68                         nativeLevel::Warn => Level::Warn,
69                         nativeLevel::Info => Level::Info,
70                         nativeLevel::Debug => Level::Debug,
71                         nativeLevel::Trace => Level::Trace,
72                 }
73         }
74         #[allow(unused)]
75         pub(crate) fn native_into(native: nativeLevel) -> Self {
76                 match native {
77                         nativeLevel::Off => Level::Off,
78                         nativeLevel::Error => Level::Error,
79                         nativeLevel::Warn => Level::Warn,
80                         nativeLevel::Info => Level::Info,
81                         nativeLevel::Debug => Level::Debug,
82                         nativeLevel::Trace => Level::Trace,
83                 }
84         }
85 }
86 /// Creates a copy of the Level
87 #[no_mangle]
88 pub extern "C" fn Level_clone(orig: &Level) -> Level {
89         orig.clone()
90 }
91 /// Checks if two Levels contain equal inner contents.
92 /// This ignores pointers and is_owned flags and looks at the values in fields.
93 #[no_mangle]
94 pub extern "C" fn Level_eq(a: &Level, b: &Level) -> bool {
95         if &a.to_native() == &b.to_native() { true } else { false }
96 }
97 /// Checks if two Levels contain equal inner contents.
98 #[no_mangle]
99 pub extern "C" fn Level_hash(o: &Level) -> u64 {
100         // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core
101         #[allow(deprecated)]
102         let mut hasher = core::hash::SipHasher::new();
103         std::hash::Hash::hash(&o.to_native(), &mut hasher);
104         std::hash::Hasher::finish(&hasher)
105 }
106 /// Returns the most verbose logging level.
107 #[must_use]
108 #[no_mangle]
109 pub extern "C" fn Level_max() -> crate::lightning::util::logger::Level {
110         let mut ret = lightning::util::logger::Level::max();
111         crate::lightning::util::logger::Level::native_into(ret)
112 }
113
114 /// A trait encapsulating the operations required of a logger
115 #[repr(C)]
116 pub struct Logger {
117         /// An opaque pointer which is passed to your function implementations as an argument.
118         /// This has no meaning in the LDK, and can be NULL or any other value.
119         pub this_arg: *mut c_void,
120         /// Logs the `Record`
121         pub log: extern "C" fn (this_arg: *const c_void, record: *const std::os::raw::c_char),
122         /// Frees any resources associated with this object given its this_arg pointer.
123         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
124         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
125 }
126 unsafe impl Send for Logger {}
127 unsafe impl Sync for Logger {}
128
129 use lightning::util::logger::Logger as rustLogger;
130 impl rustLogger for Logger {
131         fn log(&self, mut record: &lightning::util::logger::Record) {
132                 let mut local_record = std::ffi::CString::new(format!("{}", record.args)).unwrap();
133                 (self.log)(self.this_arg, local_record.as_ptr())
134         }
135 }
136
137 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
138 // directly as a Deref trait in higher-level structs:
139 impl std::ops::Deref for Logger {
140         type Target = Self;
141         fn deref(&self) -> &Self {
142                 self
143         }
144 }
145 /// Calls the free function if one is set
146 #[no_mangle]
147 pub extern "C" fn Logger_free(this_ptr: Logger) { }
148 impl Drop for Logger {
149         fn drop(&mut self) {
150                 if let Some(f) = self.free {
151                         f(self.this_arg);
152                 }
153         }
154 }