1 //! Log traits live here, which are called throughout the library to provide useful information for
2 //! debugging purposes.
4 //! There is currently 2 ways to filter log messages. First one, by using compilation features, e.g \"max_level_off\".
5 //! The second one, client-side by implementing check against Record Level field.
6 //! Each module may have its own Logger or share one.
9 use bitcoin::hashes::Hash;
10 use crate::c_types::*;
12 /// An enum representing the available verbosity levels of the logger.
17 ///Designates logger being silent
19 /// Designates very serious errors
21 /// Designates hazardous situations
23 /// Designates useful information
25 /// Designates lower priority information
27 /// Designates very low priority, often extremely verbose, information
30 use lightning::util::logger::Level as nativeLevel;
33 pub(crate) fn to_native(&self) -> nativeLevel {
35 Level::Off => nativeLevel::Off,
36 Level::Error => nativeLevel::Error,
37 Level::Warn => nativeLevel::Warn,
38 Level::Info => nativeLevel::Info,
39 Level::Debug => nativeLevel::Debug,
40 Level::Trace => nativeLevel::Trace,
44 pub(crate) fn into_native(self) -> nativeLevel {
46 Level::Off => nativeLevel::Off,
47 Level::Error => nativeLevel::Error,
48 Level::Warn => nativeLevel::Warn,
49 Level::Info => nativeLevel::Info,
50 Level::Debug => nativeLevel::Debug,
51 Level::Trace => nativeLevel::Trace,
55 pub(crate) fn from_native(native: &nativeLevel) -> Self {
57 nativeLevel::Off => Level::Off,
58 nativeLevel::Error => Level::Error,
59 nativeLevel::Warn => Level::Warn,
60 nativeLevel::Info => Level::Info,
61 nativeLevel::Debug => Level::Debug,
62 nativeLevel::Trace => Level::Trace,
66 pub(crate) fn native_into(native: nativeLevel) -> Self {
68 nativeLevel::Off => Level::Off,
69 nativeLevel::Error => Level::Error,
70 nativeLevel::Warn => Level::Warn,
71 nativeLevel::Info => Level::Info,
72 nativeLevel::Debug => Level::Debug,
73 nativeLevel::Trace => Level::Trace,
78 pub extern "C" fn Level_clone(orig: &Level) -> Level {
81 /// Returns the most verbose logging level.
84 pub extern "C" fn Level_max() -> crate::util::logger::Level {
85 let mut ret = lightning::util::logger::Level::max();
86 crate::util::logger::Level::native_into(ret)
89 /// A trait encapsulating the operations required of a logger
92 pub this_arg: *mut c_void,
94 pub log: extern "C" fn (this_arg: *const c_void, record: *const std::os::raw::c_char),
95 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
97 unsafe impl Sync for Logger {}
98 unsafe impl Send for Logger {}
100 use lightning::util::logger::Logger as rustLogger;
101 impl rustLogger for Logger {
102 fn log(&self, record: &lightning::util::logger::Record) {
103 let mut local_record = std::ffi::CString::new(format!("{}", record.args)).unwrap();
104 (self.log)(self.this_arg, local_record.as_ptr())
108 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
109 // directly as a Deref trait in higher-level structs:
110 impl std::ops::Deref for Logger {
112 fn deref(&self) -> &Self {
116 /// Calls the free function if one is set
118 pub extern "C" fn Logger_free(this_ptr: Logger) { }
119 impl Drop for Logger {
121 if let Some(f) = self.free {