From 200be2c0db94cc72c95d6b9519bb229bf8619ecd Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Sun, 10 Jan 2016 04:24:31 -0500 Subject: [PATCH] Turned this into a basic and simple logging system for now. Currently the library will handle using the log macros to directly call println!(). Later this can be set to actually log the errors. --- examples/logging.rs | 15 +++++++++ src/entry.rs | 15 --------- src/lib.rs | 30 ++---------------- src/library_info.rs | 13 -------- src/location.rs | 21 ------------- src/log_level.rs | 31 ++++++++---------- src/logger.rs | 34 -------------------- src/macros.rs | 77 ++++++++++++++++++++++++++++++++++++++------- tests/lib.rs | 8 +---- 9 files changed, 97 insertions(+), 147 deletions(-) create mode 100644 examples/logging.rs delete mode 100644 src/entry.rs delete mode 100644 src/library_info.rs delete mode 100644 src/location.rs delete mode 100644 src/logger.rs diff --git a/examples/logging.rs b/examples/logging.rs new file mode 100644 index 0000000..0f56bd9 --- /dev/null +++ b/examples/logging.rs @@ -0,0 +1,15 @@ +#[macro_use] +extern crate scribe; + + +use scribe::*; + + + +pub fn main() +{ + error!("There was an error!"); + warn!("Danger Will Robinson, danger!"); + info!("Jason is awesome."); + debug!("This seemed to work alright."); +} diff --git a/src/entry.rs b/src/entry.rs deleted file mode 100644 index cc21f6c..0000000 --- a/src/entry.rs +++ /dev/null @@ -1,15 +0,0 @@ -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 index 828f7df..fb79352 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,34 +5,8 @@ // Define the modules that are a // part of this library. +mod log_level; 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 deleted file mode 100644 index d6daa6e..0000000 --- a/src/library_info.rs +++ /dev/null @@ -1,13 +0,0 @@ -/// 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 deleted file mode 100644 index 2eb3ed2..0000000 --- a/src/location.rs +++ /dev/null @@ -1,21 +0,0 @@ -#[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 index 9c79cfc..4e5168b 100644 --- a/src/log_level.rs +++ b/src/log_level.rs @@ -2,12 +2,11 @@ use std::fmt; use std::fmt::Display; + +#[repr(u8)] #[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 @@ -22,32 +21,30 @@ pub enum LogLevel /// from running. WARN, - /// Messages, warnings, and errors will be + /// Info, warnings, and errors will be /// written to the logs. /// /// Designates useful information - MESSAGE, + INFO, - /// Debug statements, messages, warnings, + /// Debug statements, info, warnings, /// and errors will be written to the logs. + /// + /// Designates debugging information. 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), + 0 => Some(LogLevel::ERROR), + 1 => Some(LogLevel::WARN), + 2 => Some(LogLevel::INFO), + 3 => Some(LogLevel::DEBUG), _ => None } } @@ -67,12 +64,10 @@ impl Display for LogLevel { match *self { - LogLevel::OFF => write!(f, "Off"), LogLevel::ERROR => write!(f, "Error"), LogLevel::WARN => write!(f, "Warn"), - LogLevel::MESSAGE => write!(f, "Message"), + LogLevel::INFO => write!(f, "Info"), LogLevel::DEBUG => write!(f, "Debug"), - LogLevel::TRACE => write!(f, "Trace") } } } diff --git a/src/logger.rs b/src/logger.rs deleted file mode 100644 index 40b682b..0000000 --- a/src/logger.rs +++ /dev/null @@ -1,34 +0,0 @@ -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 index d733f46..f890d27 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,19 +1,74 @@ -//#[docs(hidden)] +#[doc(hidden)] #[macro_export] macro_rules! log { - ($log_level: expr, $($args: tt)*) => + ($log_level: expr, $($arg: tt)+) => ({ - let log_entry: Entry; - let desired_log_level: LogLevel; - //let current_log_level: LogLevel; + let file_path: &std::path::Path; + let mut file_name: &str; - log_entry = Entry::new(Location::new(module_path!(), file!(), line!())); - desired_log_level = $log_level; + file_name = file!(); + file_path = std::path::Path::new(file!()); + match file_path.file_name() + { + Some(name) => + { + match name.to_str() + { + Some(string) => + { + file_name = string; + } - println!("{0}: {1}(line: {2})", - log_entry.location.module_path, - log_entry.location.file, - log_entry.location.line); + None => + { + } + } + } + + None => + { + } + } + + println!("{0}: {1}::{2}::{3} -- {4}", $log_level, + module_path!(), file_name, line!(), + format_args!($($arg)+)); }) } + +#[macro_export] +macro_rules! error +{ + ($($arg: tt)*) => + { + log!(LogLevel::ERROR, $($arg)*); + } +} + +#[macro_export] +macro_rules! warn +{ + ($($arg: tt)*) => + { + log!(LogLevel::WARN, $($arg)*); + } +} + +#[macro_export] +macro_rules! info +{ + ($($arg: tt)*) => + { + log!(LogLevel::INFO, $($arg)*); + } +} + +#[macro_export] +macro_rules! debug +{ + ($($arg: tt)*) => + { + log!(LogLevel::DEBUG, $($arg)*); + } +} diff --git a/tests/lib.rs b/tests/lib.rs index 020c34f..de4d224 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,14 +1,8 @@ #[macro_use] extern crate scribe; -use scribe::LogLevel; -use scribe::location::Location; -use scribe::entry::Entry; - #[test] -pub fn test_log_call() +pub fn test_blank() { - //log!(LogLevel::ERROR, "Test"); - write_to_log!() }