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; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
22 /// An enum representing the available verbosity levels of the logger.
23 #[derive(Copy, Clone, PartialEq, 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 PartialOrd for Level {
41 fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
46 fn lt(&self, other: &Level) -> bool {
47 (*self as usize) < *other as usize
51 fn le(&self, other: &Level) -> bool {
52 *self as usize <= *other as usize
56 fn gt(&self, other: &Level) -> bool {
57 *self as usize > *other as usize
61 fn ge(&self, other: &Level) -> bool {
62 *self as usize >= *other as usize
68 fn cmp(&self, other: &Level) -> cmp::Ordering {
69 (*self as usize).cmp(&(*other as usize))
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])
80 /// Returns the most verbose logging level.
82 pub fn max() -> Level {
87 /// A Record, unit of logging output with Metadata to enable filtering
88 /// Module_path, file, line to inform on log's source
89 /// (C-not exported) - we convert to a const char* instead
90 #[derive(Clone,Debug)]
91 pub struct Record<'a> {
92 /// The verbosity level of the message.
95 pub args: fmt::Arguments<'a>,
96 /// The module path of the message.
97 pub module_path: &'a str,
98 /// The source file containing the message.
100 /// The line containing the message.
104 impl<'a> Record<'a> {
105 /// Returns a new Record.
106 /// (C-not exported) as fmt can't be used in C
108 pub fn new(level: Level, args: fmt::Arguments<'a>, module_path: &'a str, file: &'a str, line: u32) -> Record<'a> {
119 /// A trait encapsulating the operations required of a logger
121 /// Logs the `Record`
122 fn log(&self, record: &Record);
127 use util::logger::{Logger, Level};
128 use util::test_utils::TestLogger;
132 fn test_level_show() {
133 assert_eq!("INFO", Level::Info.to_string());
134 assert_eq!("ERROR", Level::Error.to_string());
135 assert_ne!("WARN", Level::Error.to_string());
143 fn new(logger: Arc<Logger>) -> WrapperLog {
149 fn call_macros(&self) {
150 log_error!(self.logger, "This is an error");
151 log_warn!(self.logger, "This is a warning");
152 log_info!(self.logger, "This is an info");
153 log_debug!(self.logger, "This is a debug");
154 log_trace!(self.logger, "This is a trace");
159 fn test_logging_macros() {
160 let mut logger = TestLogger::new();
161 logger.enable(Level::Trace);
162 let logger : Arc<Logger> = Arc::new(logger);
163 let wrapper = WrapperLog::new(Arc::clone(&logger));
164 wrapper.call_macros();