1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! Log traits live here, which are called throughout the library to provide useful information for
10 //! debugging purposes.
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.
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};
25 /// An enum representing the available verbosity levels of the logger.
30 /// Designates extremely verbose information, including gossip-induced messages
32 /// Designates very low priority, often extremely verbose, information
34 /// Designates lower priority information
36 /// Designates useful information
38 /// Designates hazardous situations
40 /// Designates very serious errors
43 use lightning::util::logger::Level as LevelImport;
44 pub(crate) type nativeLevel = LevelImport;
48 pub(crate) fn to_native(&self) -> nativeLevel {
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,
59 pub(crate) fn into_native(self) -> nativeLevel {
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,
70 pub(crate) fn from_native(native: &nativeLevel) -> Self {
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,
81 pub(crate) fn native_into(native: nativeLevel) -> Self {
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,
92 /// Creates a copy of the Level
94 pub extern "C" fn Level_clone(orig: &Level) -> Level {
98 /// Utility method to constructs a new Gossip-variant Level
99 pub extern "C" fn Level_gossip() -> Level {
102 /// Utility method to constructs a new Trace-variant Level
103 pub extern "C" fn Level_trace() -> Level {
106 /// Utility method to constructs a new Debug-variant Level
107 pub extern "C" fn Level_debug() -> Level {
110 /// Utility method to constructs a new Info-variant Level
111 pub extern "C" fn Level_info() -> Level {
114 /// Utility method to constructs a new Warn-variant Level
115 pub extern "C" fn Level_warn() -> Level {
118 /// Utility method to constructs a new Error-variant Level
119 pub extern "C" fn Level_error() -> Level {
121 /// Checks if two Levels contain equal inner contents.
122 /// This ignores pointers and is_owned flags and looks at the values in fields.
124 pub extern "C" fn Level_eq(a: &Level, b: &Level) -> bool {
125 if &a.to_native() == &b.to_native() { true } else { false }
127 /// Generates a non-cryptographic 64-bit hash of the Level.
129 pub extern "C" fn Level_hash(o: &Level) -> u64 {
130 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
132 let mut hasher = core::hash::SipHasher::new();
133 core::hash::Hash::hash(&o.to_native(), &mut hasher);
134 core::hash::Hasher::finish(&hasher)
136 /// Returns the most verbose logging level.
139 pub extern "C" fn Level_max() -> crate::lightning::util::logger::Level {
140 let mut ret = lightning::util::logger::Level::max();
141 crate::lightning::util::logger::Level::native_into(ret)
145 use lightning::util::logger::Record as nativeRecordImport;
146 pub(crate) type nativeRecord = nativeRecordImport<'static>;
148 /// A Record, unit of logging output with Metadata to enable filtering
149 /// Module_path, file, line to inform on log's source
153 /// A pointer to the opaque Rust object.
155 /// Nearly everywhere, inner must be non-null, however in places where
156 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
157 pub inner: *mut nativeRecord,
158 /// Indicates that this is the only struct which contains the same pointer.
160 /// Rust functions which take ownership of an object provided via an argument require
161 /// this to be true and invalidate the object pointed to by inner.
165 impl Drop for Record {
167 if self.is_owned && !<*mut nativeRecord>::is_null(self.inner) {
168 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
172 /// Frees any resources used by the Record, if is_owned is set and inner is non-NULL.
174 pub extern "C" fn Record_free(this_obj: Record) { }
176 /// Used only if an object of this type is returned as a trait impl by a method
177 pub(crate) extern "C" fn Record_free_void(this_ptr: *mut c_void) {
178 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRecord) };
182 pub(crate) fn get_native_ref(&self) -> &'static nativeRecord {
183 unsafe { &*ObjOps::untweak_ptr(self.inner) }
185 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRecord {
186 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
188 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
189 pub(crate) fn take_inner(mut self) -> *mut nativeRecord {
190 assert!(self.is_owned);
191 let ret = ObjOps::untweak_ptr(self.inner);
192 self.inner = core::ptr::null_mut();
196 /// The verbosity level of the message.
198 pub extern "C" fn Record_get_level(this_ptr: &Record) -> crate::lightning::util::logger::Level {
199 let mut inner_val = &mut this_ptr.get_native_mut_ref().level;
200 crate::lightning::util::logger::Level::from_native(inner_val)
202 /// The verbosity level of the message.
204 pub extern "C" fn Record_set_level(this_ptr: &mut Record, mut val: crate::lightning::util::logger::Level) {
205 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.level = val.into_native();
207 /// The message body.
209 pub extern "C" fn Record_get_args(this_ptr: &Record) -> crate::c_types::Str {
210 let mut inner_val = &mut this_ptr.get_native_mut_ref().args;
211 inner_val.as_str().into()
213 /// The message body.
215 pub extern "C" fn Record_set_args(this_ptr: &mut Record, mut val: crate::c_types::Str) {
216 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.args = val.into_string();
218 /// The module path of the message.
220 pub extern "C" fn Record_get_module_path(this_ptr: &Record) -> crate::c_types::Str {
221 let mut inner_val = &mut this_ptr.get_native_mut_ref().module_path;
224 /// The module path of the message.
226 pub extern "C" fn Record_set_module_path(this_ptr: &mut Record, mut val: crate::c_types::Str) {
227 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.module_path = val.into_str();
229 /// The source file containing the message.
231 pub extern "C" fn Record_get_file(this_ptr: &Record) -> crate::c_types::Str {
232 let mut inner_val = &mut this_ptr.get_native_mut_ref().file;
235 /// The source file containing the message.
237 pub extern "C" fn Record_set_file(this_ptr: &mut Record, mut val: crate::c_types::Str) {
238 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.file = val.into_str();
240 /// The line containing the message.
242 pub extern "C" fn Record_get_line(this_ptr: &Record) -> u32 {
243 let mut inner_val = &mut this_ptr.get_native_mut_ref().line;
246 /// The line containing the message.
248 pub extern "C" fn Record_set_line(this_ptr: &mut Record, mut val: u32) {
249 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.line = val;
251 impl Clone for Record {
252 fn clone(&self) -> Self {
254 inner: if <*mut nativeRecord>::is_null(self.inner) { core::ptr::null_mut() } else {
255 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
261 /// Used only if an object of this type is returned as a trait impl by a method
262 pub(crate) extern "C" fn Record_clone_void(this_ptr: *const c_void) -> *mut c_void {
263 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRecord)).clone() })) as *mut c_void
266 /// Creates a copy of the Record
267 pub extern "C" fn Record_clone(orig: &Record) -> Record {
270 /// A trait encapsulating the operations required of a logger
273 /// An opaque pointer which is passed to your function implementations as an argument.
274 /// This has no meaning in the LDK, and can be NULL or any other value.
275 pub this_arg: *mut c_void,
276 /// Logs the `Record`
277 pub log: extern "C" fn (this_arg: *const c_void, record: &crate::lightning::util::logger::Record),
278 /// Frees any resources associated with this object given its this_arg pointer.
279 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
280 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
282 unsafe impl Send for Logger {}
283 unsafe impl Sync for Logger {}
284 pub(crate) fn Logger_clone_fields(orig: &Logger) -> Logger {
286 this_arg: orig.this_arg,
287 log: Clone::clone(&orig.log),
288 free: Clone::clone(&orig.free),
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 })
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 {
303 fn deref(&self) -> &Self {
307 impl core::ops::DerefMut for Logger {
308 fn deref_mut(&mut self) -> &mut Self {
312 /// Calls the free function if one is set
314 pub extern "C" fn Logger_free(this_ptr: Logger) { }
315 impl Drop for Logger {
317 if let Some(f) = self.free {