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 core::convert::Infallible;
19 use bitcoin::hashes::Hash;
20 use crate::c_types::*;
22 /// An enum representing the available verbosity levels of the logger.
27 /// Designates very low priority, often extremely verbose, information
29 /// Designates lower priority information
31 /// Designates useful information
33 /// Designates hazardous situations
35 /// Designates very serious errors
38 use lightning::util::logger::Level as nativeLevel;
41 pub(crate) fn to_native(&self) -> nativeLevel {
43 Level::Trace => nativeLevel::Trace,
44 Level::Debug => nativeLevel::Debug,
45 Level::Info => nativeLevel::Info,
46 Level::Warn => nativeLevel::Warn,
47 Level::Error => nativeLevel::Error,
51 pub(crate) fn into_native(self) -> nativeLevel {
53 Level::Trace => nativeLevel::Trace,
54 Level::Debug => nativeLevel::Debug,
55 Level::Info => nativeLevel::Info,
56 Level::Warn => nativeLevel::Warn,
57 Level::Error => nativeLevel::Error,
61 pub(crate) fn from_native(native: &nativeLevel) -> Self {
63 nativeLevel::Trace => Level::Trace,
64 nativeLevel::Debug => Level::Debug,
65 nativeLevel::Info => Level::Info,
66 nativeLevel::Warn => Level::Warn,
67 nativeLevel::Error => Level::Error,
71 pub(crate) fn native_into(native: nativeLevel) -> Self {
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 /// Creates a copy of the Level
83 pub extern "C" fn Level_clone(orig: &Level) -> Level {
87 /// Utility method to constructs a new Trace-variant Level
88 pub extern "C" fn Level_trace() -> Level {
91 /// Utility method to constructs a new Debug-variant Level
92 pub extern "C" fn Level_debug() -> Level {
95 /// Utility method to constructs a new Info-variant Level
96 pub extern "C" fn Level_info() -> Level {
99 /// Utility method to constructs a new Warn-variant Level
100 pub extern "C" fn Level_warn() -> Level {
103 /// Utility method to constructs a new Error-variant Level
104 pub extern "C" fn Level_error() -> Level {
106 /// Checks if two Levels contain equal inner contents.
107 /// This ignores pointers and is_owned flags and looks at the values in fields.
109 pub extern "C" fn Level_eq(a: &Level, b: &Level) -> bool {
110 if &a.to_native() == &b.to_native() { true } else { false }
112 /// Checks if two Levels contain equal inner contents.
114 pub extern "C" fn Level_hash(o: &Level) -> u64 {
115 // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core
117 let mut hasher = core::hash::SipHasher::new();
118 std::hash::Hash::hash(&o.to_native(), &mut hasher);
119 std::hash::Hasher::finish(&hasher)
121 /// Returns the most verbose logging level.
124 pub extern "C" fn Level_max() -> crate::lightning::util::logger::Level {
125 let mut ret = lightning::util::logger::Level::max();
126 crate::lightning::util::logger::Level::native_into(ret)
129 /// A trait encapsulating the operations required of a logger
132 /// An opaque pointer which is passed to your function implementations as an argument.
133 /// This has no meaning in the LDK, and can be NULL or any other value.
134 pub this_arg: *mut c_void,
135 /// Logs the `Record`
136 pub log: extern "C" fn (this_arg: *const c_void, record: *const std::os::raw::c_char),
137 /// Frees any resources associated with this object given its this_arg pointer.
138 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
139 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
141 unsafe impl Send for Logger {}
142 unsafe impl Sync for Logger {}
144 pub(crate) extern "C" fn Logger_clone_fields(orig: &Logger) -> Logger {
146 this_arg: orig.this_arg,
147 log: Clone::clone(&orig.log),
148 free: Clone::clone(&orig.free),
152 use lightning::util::logger::Logger as rustLogger;
153 impl rustLogger for Logger {
154 fn log(&self, mut record: &lightning::util::logger::Record) {
155 let mut local_record = std::ffi::CString::new(format!("{}", record.args)).unwrap();
156 (self.log)(self.this_arg, local_record.as_ptr())
160 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
161 // directly as a Deref trait in higher-level structs:
162 impl std::ops::Deref for Logger {
164 fn deref(&self) -> &Self {
168 /// Calls the free function if one is set
170 pub extern "C" fn Logger_free(this_ptr: Logger) { }
171 impl Drop for Logger {
173 if let Some(f) = self.free {