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.
This commit is contained in:
Jason Travis Smith 2016-01-10 04:24:31 -05:00
parent 38b626070a
commit 200be2c0db
9 changed files with 97 additions and 147 deletions

15
examples/logging.rs Normal file
View File

@ -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.");
}

View File

@ -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}
}
}

View File

@ -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!();
};
}

View File

@ -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";

View File

@ -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}
}
}

View File

@ -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<LogLevel>
{
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")
}
}
}

View File

@ -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
}
}

View File

@ -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)*);
}
}

View File

@ -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!()
}