8a1fbb15d54340e37dccf5b0f6592a6020659681
[ldk-c-bindings] / lightning-c-bindings / src / util / logger.rs
1 //! Log traits live here, which are called throughout the library to provide useful information for
2 //! debugging purposes.
3 //!
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.
7
8 use std::ffi::c_void;
9 use bitcoin::hashes::Hash;
10 use crate::c_types::*;
11
12 /// An enum representing the available verbosity levels of the logger.
13 #[must_use]
14 #[derive(Clone)]
15 #[repr(C)]
16 pub enum Level {
17         ///Designates logger being silent
18         Off,
19         /// Designates very serious errors
20         Error,
21         /// Designates hazardous situations
22         Warn,
23         /// Designates useful information
24         Info,
25         /// Designates lower priority information
26         Debug,
27         /// Designates very low priority, often extremely verbose, information
28         Trace,
29 }
30 use lightning::util::logger::Level as nativeLevel;
31 impl Level {
32         #[allow(unused)]
33         pub(crate) fn to_native(&self) -> nativeLevel {
34                 match self {
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,
41                 }
42         }
43         #[allow(unused)]
44         pub(crate) fn into_native(self) -> nativeLevel {
45                 match self {
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,
52                 }
53         }
54         #[allow(unused)]
55         pub(crate) fn from_native(native: &nativeLevel) -> Self {
56                 match native {
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,
63                 }
64         }
65         #[allow(unused)]
66         pub(crate) fn native_into(native: nativeLevel) -> Self {
67                 match native {
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,
74                 }
75         }
76 }
77 #[no_mangle]
78 pub extern "C" fn Level_clone(orig: &Level) -> Level {
79         orig.clone()
80 }
81 /// Returns the most verbose logging level.
82 #[must_use]
83 #[no_mangle]
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)
87 }
88
89 /// A trait encapsulating the operations required of a logger
90 #[repr(C)]
91 pub struct Logger {
92         pub this_arg: *mut c_void,
93         /// Logs the `Record`
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)>,
96 }
97 unsafe impl Sync for Logger {}
98 unsafe impl Send for Logger {}
99
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())
105         }
106 }
107
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 {
111         type Target = Self;
112         fn deref(&self) -> &Self {
113                 self
114         }
115 }
116 /// Calls the free function if one is set
117 #[no_mangle]
118 pub extern "C" fn Logger_free(this_ptr: Logger) { }
119 impl Drop for Logger {
120         fn drop(&mut self) {
121                 if let Some(f) = self.free {
122                         f(self.this_arg);
123                 }
124         }
125 }