A simple start at a logging facility.
This commit is contained in:
parent
3b1567d357
commit
38b626070a
15
src/entry.rs
Normal file
15
src/entry.rs
Normal file
@ -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}
|
||||||
|
}
|
||||||
|
}
|
38
src/lib.rs
Normal file
38
src/lib.rs
Normal file
@ -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!();
|
||||||
|
};
|
||||||
|
}
|
13
src/library_info.rs
Normal file
13
src/library_info.rs
Normal file
@ -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";
|
21
src/location.rs
Normal file
21
src/location.rs
Normal file
@ -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}
|
||||||
|
}
|
||||||
|
}
|
78
src/log_level.rs
Normal file
78
src/log_level.rs
Normal file
@ -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<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),
|
||||||
|
_ => 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
src/logger.rs
Normal file
34
src/logger.rs
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
19
src/macros.rs
Normal file
19
src/macros.rs
Normal file
@ -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);
|
||||||
|
})
|
||||||
|
}
|
14
tests/lib.rs
Normal file
14
tests/lib.rs
Normal file
@ -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!()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user