1 // Pruned copy of crate rust log, without global logger
2 // https://github.com/rust-lang-nursery/log #7a60286
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.
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; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
22 /// An enum representing the available verbosity levels of the logger.
23 #[derive(Copy, Clone, Eq, Debug, Hash)]
25 ///Designates logger being silent
27 /// Designates very serious errors
29 /// Designates hazardous situations
31 /// Designates useful information
33 /// Designates lower priority information
35 /// Designates very low priority, often extremely verbose, information
39 impl PartialEq for Level {
41 fn eq(&self, other: &Level) -> bool {
42 *self as usize == *other as usize
46 impl PartialOrd for Level {
48 fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
53 fn lt(&self, other: &Level) -> bool {
54 (*self as usize) < *other as usize
58 fn le(&self, other: &Level) -> bool {
59 *self as usize <= *other as usize
63 fn gt(&self, other: &Level) -> bool {
64 *self as usize > *other as usize
68 fn ge(&self, other: &Level) -> bool {
69 *self as usize >= *other as usize
75 fn cmp(&self, other: &Level) -> cmp::Ordering {
76 (*self as usize).cmp(&(*other as usize))
80 impl fmt::Display for Level {
81 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
82 fmt.pad(LOG_LEVEL_NAMES[*self as usize])
87 /// Returns the most verbose logging level.
89 pub fn max() -> Level {
94 /// A Record, unit of logging output with Metadata to enable filtering
95 /// Module_path, file, line to inform on log's source
96 #[derive(Clone,Debug)]
97 pub struct Record<'a> {
98 /// The verbosity level of the message.
100 /// The message body.
101 pub args: fmt::Arguments<'a>,
102 /// The module path of the message.
103 pub module_path: &'a str,
104 /// The source file containing the message.
106 /// The line containing the message.
110 impl<'a> Record<'a> {
111 /// Returns a new Record.
113 pub fn new(level: Level, args: fmt::Arguments<'a>, module_path: &'a str, file: &'a str, line: u32) -> Record<'a> {
124 /// A trait encapsulating the operations required of a logger
125 pub trait Logger: Sync + Send {
126 /// Logs the `Record`
127 fn log(&self, record: &Record);
132 use util::logger::{Logger, Level};
133 use util::test_utils::TestLogger;
134 use std::sync::{Arc};
137 fn test_level_show() {
138 assert_eq!("INFO", Level::Info.to_string());
139 assert_eq!("ERROR", Level::Error.to_string());
140 assert_ne!("WARN", Level::Error.to_string());
148 fn new(logger: Arc<Logger>) -> WrapperLog {
154 fn call_macros(&self) {
155 log_error!(self, "This is an error");
156 log_warn!(self, "This is a warning");
157 log_info!(self, "This is an info");
158 log_debug!(self, "This is a debug");
159 log_trace!(self, "This is a trace");
164 fn test_logging_macros() {
165 let mut logger = TestLogger::new();
166 logger.enable(Level::Trace);
167 let logger : Arc<Logger> = Arc::new(logger);
168 let wrapper = WrapperLog::new(Arc::clone(&logger));
169 wrapper.call_macros();