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