1 // Pruned copy of crate rust log, without global logger
2 // https://github.com/rust-lang-nursery/log #7a60286
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Log traits live here, which are called throughout the library to provide useful information for
11 //! debugging purposes.
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.
20 static LOG_LEVEL_NAMES: [&'static str; 5] = ["TRACE", "DEBUG", "INFO", "WARN", "ERROR"];
22 /// An enum representing the available verbosity levels of the logger.
23 #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
25 /// Designates very low priority, often extremely verbose, information
27 /// Designates lower priority information
29 /// Designates useful information
31 /// Designates hazardous situations
33 /// Designates very serious errors
37 impl PartialOrd for Level {
39 fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
44 fn lt(&self, other: &Level) -> bool {
45 (*self as usize) < *other as usize
49 fn le(&self, other: &Level) -> bool {
50 *self as usize <= *other as usize
54 fn gt(&self, other: &Level) -> bool {
55 *self as usize > *other as usize
59 fn ge(&self, other: &Level) -> bool {
60 *self as usize >= *other as usize
66 fn cmp(&self, other: &Level) -> cmp::Ordering {
67 (*self as usize).cmp(&(*other as usize))
71 impl fmt::Display for Level {
72 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
73 fmt.pad(LOG_LEVEL_NAMES[*self as usize])
78 /// Returns the most verbose logging level.
80 pub fn max() -> Level {
85 /// A Record, unit of logging output with Metadata to enable filtering
86 /// Module_path, file, line to inform on log's source
87 /// (C-not exported) - we convert to a const char* instead
88 #[derive(Clone,Debug)]
89 pub struct Record<'a> {
90 /// The verbosity level of the message.
93 pub args: fmt::Arguments<'a>,
94 /// The module path of the message.
95 pub module_path: &'a str,
96 /// The source file containing the message.
98 /// The line containing the message.
102 impl<'a> Record<'a> {
103 /// Returns a new Record.
104 /// (C-not exported) as fmt can't be used in C
106 pub fn new(level: Level, args: fmt::Arguments<'a>, module_path: &'a str, file: &'a str, line: u32) -> Record<'a> {
117 /// A trait encapsulating the operations required of a logger
119 /// Logs the `Record`
120 fn log(&self, record: &Record);
123 /// Wrapper for logging byte slices in hex format.
125 pub struct DebugBytes<'a>(pub &'a [u8]);
126 impl<'a> core::fmt::Display for DebugBytes<'a> {
127 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
129 write!(f, "{:02x}", i)?;
137 use util::logger::{Logger, Level};
138 use util::test_utils::TestLogger;
142 fn test_level_show() {
143 assert_eq!("INFO", Level::Info.to_string());
144 assert_eq!("ERROR", Level::Error.to_string());
145 assert_ne!("WARN", Level::Error.to_string());
153 fn new(logger: Arc<Logger>) -> WrapperLog {
159 fn call_macros(&self) {
160 log_error!(self.logger, "This is an error");
161 log_warn!(self.logger, "This is a warning");
162 log_info!(self.logger, "This is an info");
163 log_debug!(self.logger, "This is a debug");
164 log_trace!(self.logger, "This is a trace");
169 fn test_logging_macros() {
170 let mut logger = TestLogger::new();
171 logger.enable(Level::Trace);
172 let logger : Arc<Logger> = Arc::new(logger);
173 let wrapper = WrapperLog::new(Arc::clone(&logger));
174 wrapper.call_macros();
178 fn test_log_ordering() {
179 assert!(Level::Error > Level::Warn);
180 assert!(Level::Error >= Level::Warn);
181 assert!(Level::Error >= Level::Error);
182 assert!(Level::Warn > Level::Info);
183 assert!(Level::Warn >= Level::Info);
184 assert!(Level::Warn >= Level::Warn);
185 assert!(Level::Info > Level::Debug);
186 assert!(Level::Info >= Level::Debug);
187 assert!(Level::Info >= Level::Info);
188 assert!(Level::Debug > Level::Trace);
189 assert!(Level::Debug >= Level::Trace);
190 assert!(Level::Debug >= Level::Debug);
191 assert!(Level::Trace >= Level::Trace);
193 assert!(Level::Error <= Level::Error);
194 assert!(Level::Warn < Level::Error);
195 assert!(Level::Warn <= Level::Error);
196 assert!(Level::Warn <= Level::Warn);
197 assert!(Level::Info < Level::Warn);
198 assert!(Level::Info <= Level::Warn);
199 assert!(Level::Info <= Level::Info);
200 assert!(Level::Debug < Level::Info);
201 assert!(Level::Debug <= Level::Info);
202 assert!(Level::Debug <= Level::Debug);
203 assert!(Level::Trace < Level::Debug);
204 assert!(Level::Trace <= Level::Debug);
205 assert!(Level::Trace <= Level::Trace);