ChannelManager+Router++ Logger Arc --> Deref
[rust-lightning] / lightning / src / util / logger.rs
1 // Pruned copy of crate rust log, without global logger
2 // https://github.com/rust-lang-nursery/log #7a60286
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 //! Log traits live here, which are called throughout the library to provide useful information for
11 //! debugging purposes.
12 //!
13 //! There is currently 2 ways to filter log messages. First one, by using compilation features, e.g "max_level_off".
14 //! The second one, client-side by implementing check against Record Level field.
15 //! Each module may have its own Logger or share one.
16
17 use std::cmp;
18 use std::fmt;
19
20 static LOG_LEVEL_NAMES: [&'static str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
21
22 /// An enum representing the available verbosity levels of the logger.
23 #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
24 pub enum Level {
25         ///Designates logger being silent
26         Off,
27         /// Designates very serious errors
28         Error,
29         /// Designates hazardous situations
30         Warn,
31         /// Designates useful information
32         Info,
33         /// Designates lower priority information
34         Debug,
35         /// Designates very low priority, often extremely verbose, information
36         Trace,
37 }
38
39 impl PartialOrd for Level {
40         #[inline]
41         fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
42                 Some(self.cmp(other))
43         }
44
45         #[inline]
46         fn lt(&self, other: &Level) -> bool {
47                 (*self as usize) < *other as usize
48         }
49
50         #[inline]
51         fn le(&self, other: &Level) -> bool {
52                 *self as usize <= *other as usize
53         }
54
55         #[inline]
56         fn gt(&self, other: &Level) -> bool {
57                 *self as usize > *other as usize
58         }
59
60         #[inline]
61         fn ge(&self, other: &Level) -> bool {
62                 *self as usize >= *other as usize
63         }
64 }
65
66 impl Ord for Level {
67         #[inline]
68         fn cmp(&self, other: &Level) -> cmp::Ordering {
69                 (*self as usize).cmp(&(*other as usize))
70         }
71 }
72
73 impl fmt::Display for Level {
74         fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
75                 fmt.pad(LOG_LEVEL_NAMES[*self as usize])
76         }
77 }
78
79 impl Level {
80         /// Returns the most verbose logging level.
81         #[inline]
82         pub fn max() -> Level {
83                 Level::Trace
84         }
85 }
86
87 /// A Record, unit of logging output with Metadata to enable filtering
88 /// Module_path, file, line to inform on log's source
89 #[derive(Clone,Debug)]
90 pub struct Record<'a> {
91         /// The verbosity level of the message.
92         pub level: Level,
93         /// The message body.
94         pub args: fmt::Arguments<'a>,
95         /// The module path of the message.
96         pub module_path: &'a str,
97         /// The source file containing the message.
98         pub file: &'a str,
99         /// The line containing the message.
100         pub line: u32,
101 }
102
103 impl<'a> Record<'a> {
104         /// Returns a new Record.
105         #[inline]
106         pub fn new(level: Level, args: fmt::Arguments<'a>, module_path: &'a str, file: &'a str, line: u32) -> Record<'a> {
107                 Record {
108                         level,
109                         args,
110                         module_path,
111                         file,
112                         line
113                 }
114         }
115 }
116
117 /// A trait encapsulating the operations required of a logger
118 pub trait Logger: Sync + Send {
119         /// Logs the `Record`
120         fn log(&self, record: &Record);
121 }
122
123 #[cfg(test)]
124 mod tests {
125         use util::logger::{Logger, Level};
126         use util::test_utils::TestLogger;
127         use std::sync::Arc;
128
129         #[test]
130         fn test_level_show() {
131                 assert_eq!("INFO", Level::Info.to_string());
132                 assert_eq!("ERROR", Level::Error.to_string());
133                 assert_ne!("WARN", Level::Error.to_string());
134         }
135
136         struct WrapperLog {
137                 logger: Arc<Logger>
138         }
139
140         impl WrapperLog {
141                 fn new(logger: Arc<Logger>) -> WrapperLog {
142                         WrapperLog {
143                                 logger,
144                         }
145                 }
146
147                 fn call_macros(&self) {
148                         log_error!(self.logger, "This is an error");
149                         log_warn!(self.logger, "This is a warning");
150                         log_info!(self.logger, "This is an info");
151                         log_debug!(self.logger, "This is a debug");
152                         log_trace!(self.logger, "This is a trace");
153                 }
154         }
155
156         #[test]
157         fn test_logging_macros() {
158                 let mut logger = TestLogger::new();
159                 logger.enable(Level::Trace);
160                 let logger : Arc<Logger> = Arc::new(logger);
161                 let wrapper = WrapperLog::new(Arc::clone(&logger));
162                 wrapper.call_macros();
163         }
164 }