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 std::str::FromStr;
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
21 /// An enum representing the available verbosity levels of the logger.
26 /// Designates very low priority, often extremely verbose, information
28 /// Designates lower priority information
30 /// Designates useful information
32 /// Designates hazardous situations
34 /// Designates very serious errors
37 use lightning::util::logger::Level as nativeLevel;
40 pub(crate) fn to_native(&self) -> nativeLevel {
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,
50 pub(crate) fn into_native(self) -> nativeLevel {
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,
60 pub(crate) fn from_native(native: &nativeLevel) -> Self {
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,
70 pub(crate) fn native_into(native: nativeLevel) -> Self {
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,
80 /// Creates a copy of the Level
82 pub extern "C" fn Level_clone(orig: &Level) -> Level {
85 /// Checks if two Levels contain equal inner contents.
86 /// This ignores pointers and is_owned flags and looks at the values in fields.
88 pub extern "C" fn Level_eq(a: &Level, b: &Level) -> bool {
89 if &a.to_native() == &b.to_native() { true } else { false }
91 /// Checks if two Levels contain equal inner contents.
93 pub extern "C" fn Level_hash(o: &Level) -> u64 {
94 // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core
96 let mut hasher = core::hash::SipHasher::new();
97 std::hash::Hash::hash(&o.to_native(), &mut hasher);
98 std::hash::Hasher::finish(&hasher)
100 /// Returns the most verbose logging level.
103 pub extern "C" fn Level_max() -> crate::lightning::util::logger::Level {
104 let mut ret = lightning::util::logger::Level::max();
105 crate::lightning::util::logger::Level::native_into(ret)
108 /// A trait encapsulating the operations required of a logger
111 /// An opaque pointer which is passed to your function implementations as an argument.
112 /// This has no meaning in the LDK, and can be NULL or any other value.
113 pub this_arg: *mut c_void,
114 /// Logs the `Record`
115 pub log: extern "C" fn (this_arg: *const c_void, record: *const std::os::raw::c_char),
116 /// Frees any resources associated with this object given its this_arg pointer.
117 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
118 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
120 unsafe impl Send for Logger {}
121 unsafe impl Sync for Logger {}
123 use lightning::util::logger::Logger as rustLogger;
124 impl rustLogger for Logger {
125 fn log(&self, mut record: &lightning::util::logger::Record) {
126 let mut local_record = std::ffi::CString::new(format!("{}", record.args)).unwrap();
127 (self.log)(self.this_arg, local_record.as_ptr())
131 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
132 // directly as a Deref trait in higher-level structs:
133 impl std::ops::Deref for Logger {
135 fn deref(&self) -> &Self {
139 /// Calls the free function if one is set
141 pub extern "C" fn Logger_free(this_ptr: Logger) { }
142 impl Drop for Logger {
144 if let Some(f) = self.free {