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.
21 static LOG_LEVEL_NAMES: [&'static str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
23 /// An enum representing the available verbosity levels of the logger.
24 #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
26 ///Designates logger being silent
28 /// Designates very serious errors
30 /// Designates hazardous situations
32 /// Designates useful information
34 /// Designates lower priority information
36 /// Designates very low priority, often extremely verbose, information
40 impl PartialOrd for Level {
42 fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
47 fn lt(&self, other: &Level) -> bool {
48 (*self as usize) < *other as usize
52 fn le(&self, other: &Level) -> bool {
53 *self as usize <= *other as usize
57 fn gt(&self, other: &Level) -> bool {
58 *self as usize > *other as usize
62 fn ge(&self, other: &Level) -> bool {
63 *self as usize >= *other as usize
69 fn cmp(&self, other: &Level) -> cmp::Ordering {
70 (*self as usize).cmp(&(*other as usize))
74 impl fmt::Display for Level {
75 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
76 fmt.pad(LOG_LEVEL_NAMES[*self as usize])
81 /// Returns the most verbose logging level.
83 pub fn max() -> Level {
88 /// A Record, unit of logging output with Metadata to enable filtering
89 /// Module_path, file, line to inform on log's source
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.
107 pub fn new(level: Level, args: fmt::Arguments<'a>, module_path: &'a str, file: &'a str, line: u32) -> Record<'a> {
118 /// A trait encapsulating the operations required of a logger
119 pub trait Logger: Sync + Send {
120 /// Logs the `Record`
121 fn log(&self, record: &Record);
124 pub(crate) struct LogHolder<'a> { pub(crate) logger: &'a Arc<Logger> }
128 use util::logger::{Logger, Level};
129 use util::test_utils::TestLogger;
130 use std::sync::{Arc};
133 fn test_level_show() {
134 assert_eq!("INFO", Level::Info.to_string());
135 assert_eq!("ERROR", Level::Error.to_string());
136 assert_ne!("WARN", Level::Error.to_string());
144 fn new(logger: Arc<Logger>) -> WrapperLog {
150 fn call_macros(&self) {
151 log_error!(self, "This is an error");
152 log_warn!(self, "This is a warning");
153 log_info!(self, "This is an info");
154 log_debug!(self, "This is a debug");
155 log_trace!(self, "This is a trace");
160 fn test_logging_macros() {
161 let mut logger = TestLogger::new();
162 logger.enable(Level::Trace);
163 let logger : Arc<Logger> = Arc::new(logger);
164 let wrapper = WrapperLog::new(Arc::clone(&logger));
165 wrapper.call_macros();