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);
125 use util::logger::{Logger, Level};
126 use util::test_utils::TestLogger;
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());
141 fn new(logger: Arc<Logger>) -> WrapperLog {
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");
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();
166 fn test_log_ordering() {
167 assert!(Level::Error > Level::Warn);
168 assert!(Level::Error >= Level::Warn);
169 assert!(Level::Error >= Level::Error);
170 assert!(Level::Warn > Level::Info);
171 assert!(Level::Warn >= Level::Info);
172 assert!(Level::Warn >= Level::Warn);
173 assert!(Level::Info > Level::Debug);
174 assert!(Level::Info >= Level::Debug);
175 assert!(Level::Info >= Level::Info);
176 assert!(Level::Debug > Level::Trace);
177 assert!(Level::Debug >= Level::Trace);
178 assert!(Level::Debug >= Level::Debug);
179 assert!(Level::Trace >= Level::Trace);
181 assert!(Level::Error <= Level::Error);
182 assert!(Level::Warn < Level::Error);
183 assert!(Level::Warn <= Level::Error);
184 assert!(Level::Warn <= Level::Warn);
185 assert!(Level::Info < Level::Warn);
186 assert!(Level::Info <= Level::Warn);
187 assert!(Level::Info <= Level::Info);
188 assert!(Level::Debug < Level::Info);
189 assert!(Level::Debug <= Level::Info);
190 assert!(Level::Debug <= Level::Debug);
191 assert!(Level::Trace < Level::Debug);
192 assert!(Level::Trace <= Level::Debug);
193 assert!(Level::Trace <= Level::Trace);