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 logger being silent
28 /// Designates very serious errors
30 /// Designates hazardous situations
32 /// Designates useful information
34 /// Designates lower priority information
36 /// Designates very low priority, often extremely verbose, information
39 use lightning::util::logger::Level as nativeLevel;
42 pub(crate) fn to_native(&self) -> nativeLevel {
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,
53 pub(crate) fn into_native(self) -> nativeLevel {
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,
64 pub(crate) fn from_native(native: &nativeLevel) -> Self {
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,
75 pub(crate) fn native_into(native: nativeLevel) -> Self {
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,
86 /// Creates a copy of the Level
88 pub extern "C" fn Level_clone(orig: &Level) -> Level {
91 /// Returns the most verbose logging level.
94 pub extern "C" fn Level_max() -> crate::lightning::util::logger::Level {
95 let mut ret = lightning::util::logger::Level::max();
96 crate::lightning::util::logger::Level::native_into(ret)
99 /// A trait encapsulating the operations required of a logger
102 /// An opaque pointer which is passed to your function implementations as an argument.
103 /// This has no meaning in the LDK, and can be NULL or any other value.
104 pub this_arg: *mut c_void,
105 /// Logs the `Record`
106 pub log: extern "C" fn (this_arg: *const c_void, record: *const std::os::raw::c_char),
107 /// Frees any resources associated with this object given its this_arg pointer.
108 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
109 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
111 unsafe impl Sync for Logger {}
112 unsafe impl Send for Logger {}
114 use lightning::util::logger::Logger as rustLogger;
115 impl rustLogger for Logger {
116 fn log(&self, record: &lightning::util::logger::Record) {
117 let mut local_record = std::ffi::CString::new(format!("{}", record.args)).unwrap();
118 (self.log)(self.this_arg, local_record.as_ptr())
122 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
123 // directly as a Deref trait in higher-level structs:
124 impl std::ops::Deref for Logger {
126 fn deref(&self) -> &Self {
130 /// Calls the free function if one is set
132 pub extern "C" fn Logger_free(this_ptr: Logger) { }
133 impl Drop for Logger {
135 if let Some(f) = self.free {