From 38b626070ac68c59743988c50b1c19a4a31acbd6 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Wed, 9 Dec 2015 17:51:31 -0500 Subject: [PATCH] A simple start at a logging facility. --- src/entry.rs | 15 +++++++++ src/lib.rs | 38 ++++++++++++++++++++++ src/library_info.rs | 13 ++++++++ src/location.rs | 21 ++++++++++++ src/log_level.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++ src/logger.rs | 34 ++++++++++++++++++++ src/macros.rs | 19 +++++++++++ tests/lib.rs | 14 ++++++++ 8 files changed, 232 insertions(+) create mode 100644 src/entry.rs create mode 100644 src/lib.rs create mode 100644 src/library_info.rs create mode 100644 src/location.rs create mode 100644 src/log_level.rs create mode 100644 src/logger.rs create mode 100644 src/macros.rs create mode 100644 tests/lib.rs diff --git a/src/entry.rs b/src/entry.rs new file mode 100644 index 0000000..cc21f6c --- /dev/null +++ b/src/entry.rs @@ -0,0 +1,15 @@ +use super::location::Location; + + +pub struct Entry +{ + pub location: Location +} + +impl Entry +{ + pub fn new(loc: Location) -> Entry + { + Entry {location: loc} + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..828f7df --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,38 @@ +/// +/// +/// + + +// Define the modules that are a +// part of this library. +mod macros; +pub mod library_info; +pub mod log_level; +pub mod location; +pub mod entry; +pub mod logger; + +// Pull certain module items to the +// fore front of the crate. +pub use self::logger::Logger; +pub use self::log_level::LogLevel; + + +// Define the Macros this library provides and uses. +#[macro_export] +macro_rules! write_to_log +{ + () => + { + println!("{2:?} {0:?} {1:?}", file!(), line!(), module_path!()); + }; +} + +#[macro_export] +macro_rules! message +{ + () => + { + write_to_log!(); + }; +} diff --git a/src/library_info.rs b/src/library_info.rs new file mode 100644 index 0000000..d6daa6e --- /dev/null +++ b/src/library_info.rs @@ -0,0 +1,13 @@ +/// The full, displayable, name of the program. +pub const NAME: &'static str = "Scribe"; + +/// A simple name for this program that can be used +/// more easily than the displayable name. +pub const SIMPLE_NAME: &'static str = "Scribe"; + +/// The version of the library. +pub const VERSION: &'static str = env!("CARGO_PKG_VERSION"); + +/// The directory name to use for this program. +/// This is most useful for dealing with logging. +pub const DIRECTORY_NAME: &'static str = "scribe"; diff --git a/src/location.rs b/src/location.rs new file mode 100644 index 0000000..2eb3ed2 --- /dev/null +++ b/src/location.rs @@ -0,0 +1,21 @@ +#[derive(Clone, Copy)] +pub struct Location +{ + /// + pub module_path: &'static str, + + /// + pub file: &'static str, + + /// + pub line: u32 +} + +impl Location +{ + pub fn new(mod_loc: &'static str, file_loc: &'static str, line_loc: u32) + -> Location + { + Location {module_path: mod_loc, file: file_loc, line: line_loc} + } +} diff --git a/src/log_level.rs b/src/log_level.rs new file mode 100644 index 0000000..9c79cfc --- /dev/null +++ b/src/log_level.rs @@ -0,0 +1,78 @@ +use std::fmt; +use std::fmt::Display; + + +#[derive(Clone, Copy, Eq, Debug)] +pub enum LogLevel +{ + /// Nothing will be written to the logs. + OFF, + + /// Only errors will be written to the logs. + /// + /// This designates a serious, program + /// stopping, error. + ERROR, + + /// Warnings and errors will be written to + /// the logs. + /// + /// Designates a problem, but it is not + /// something that will stop the program + /// from running. + WARN, + + /// Messages, warnings, and errors will be + /// written to the logs. + /// + /// Designates useful information + MESSAGE, + + /// Debug statements, messages, warnings, + /// and errors will be written to the logs. + DEBUG, + + /// + TRACE +} + +impl LogLevel +{ + pub fn from_u8(lvl: u8) -> Option + { + match lvl + { + 0 => Some(LogLevel::OFF), + 1 => Some(LogLevel::ERROR), + 2 => Some(LogLevel::WARN), + 3 => Some(LogLevel::MESSAGE), + 4 => Some(LogLevel::DEBUG), + 5 => Some(LogLevel::TRACE), + _ => None + } + } +} + +impl PartialEq for LogLevel +{ + fn eq(&self, other: &LogLevel) -> bool + { + *self as usize == *other as usize + } +} + +impl Display for LogLevel +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result + { + match *self + { + LogLevel::OFF => write!(f, "Off"), + LogLevel::ERROR => write!(f, "Error"), + LogLevel::WARN => write!(f, "Warn"), + LogLevel::MESSAGE => write!(f, "Message"), + LogLevel::DEBUG => write!(f, "Debug"), + LogLevel::TRACE => write!(f, "Trace") + } + } +} diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 0000000..40b682b --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,34 @@ +use super::log_level::LogLevel; + + +pub struct Logger +{ + log_level: LogLevel +} + + +impl Logger +{ + pub fn new(lvl: LogLevel) -> Logger + { + Logger{log_level: lvl} + } + + pub fn get_log_level(&self) -> LogLevel + { + match self.log_level + { + LogLevel::OFF => LogLevel::OFF, + LogLevel::ERROR => LogLevel::ERROR, + LogLevel::WARN => LogLevel::WARN, + LogLevel::MESSAGE => LogLevel::MESSAGE, + LogLevel::DEBUG => LogLevel::DEBUG, + LogLevel::TRACE => LogLevel::TRACE + } + } + + pub fn set_log_level(&mut self, lvl: LogLevel) + { + self.log_level = lvl + } +} diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..d733f46 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,19 @@ +//#[docs(hidden)] +#[macro_export] +macro_rules! log +{ + ($log_level: expr, $($args: tt)*) => + ({ + let log_entry: Entry; + let desired_log_level: LogLevel; + //let current_log_level: LogLevel; + + log_entry = Entry::new(Location::new(module_path!(), file!(), line!())); + desired_log_level = $log_level; + + println!("{0}: {1}(line: {2})", + log_entry.location.module_path, + log_entry.location.file, + log_entry.location.line); + }) +} diff --git a/tests/lib.rs b/tests/lib.rs new file mode 100644 index 0000000..020c34f --- /dev/null +++ b/tests/lib.rs @@ -0,0 +1,14 @@ +#[macro_use] +extern crate scribe; + +use scribe::LogLevel; +use scribe::location::Location; +use scribe::entry::Entry; + + +#[test] +pub fn test_log_call() +{ + //log!(LogLevel::ERROR, "Test"); + write_to_log!() +}