[fuzz] Make router_target a bit easier for fuzzers to explore
[rust-lightning] / 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 /// Returns the most verbose logging level.
78 #[must_use]
79 #[no_mangle]
80 pub extern "C" fn Level_max() -> crate::util::logger::Level {
81         let mut ret = lightning::util::logger::Level::max();
82         crate::util::logger::Level::native_into(ret)
83 }
84
85 /// A trait encapsulating the operations required of a logger
86 #[repr(C)]
87 pub struct Logger {
88         pub this_arg: *mut c_void,
89         /// Logs the `Record`
90         pub log: extern "C" fn (this_arg: *const c_void, record: *const std::os::raw::c_char),
91         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
92 }
93 unsafe impl Sync for Logger {}
94 unsafe impl Send for Logger {}
95
96 use lightning::util::logger::Logger as rustLogger;
97 impl rustLogger for Logger {
98         fn log(&self, record: &lightning::util::logger::Record) {
99                 let mut local_record = std::ffi::CString::new(format!("{}", record.args)).unwrap();
100                 (self.log)(self.this_arg, local_record.as_ptr())
101         }
102 }
103
104 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
105 // directly as a Deref trait in higher-level structs:
106 impl std::ops::Deref for Logger {
107         type Target = Self;
108         fn deref(&self) -> &Self {
109                 self
110         }
111 }
112 /// Calls the free function if one is set
113 #[no_mangle]
114 pub extern "C" fn Logger_free(this_ptr: Logger) { }
115 impl Drop for Logger {
116         fn drop(&mut self) {
117                 if let Some(f) = self.free {
118                         f(self.this_arg);
119                 }
120         }
121 }