commit e099c9108c6091e73e47907dc062895b48e30f04 Author: rascul Date: Fri Dec 22 13:40:13 2017 -0600 init diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7ae709f --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ + +root = True + +[*] +indent_style = tab +indent_size = 4 +trim_trailing_whitespace = True +insert_final_newline = True + +[*.yml] +indent_style = space diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6aa1064 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target/ +**/*.rs.bk +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3595535 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "rag" +version = "0.0.1" +authors = ["rascul "] +description = "Simple logger" +license = "MIT" +repository = "https://github.com/rascul/rag" +documentation = "https://docs.rs/rag" +readme = "README.md" +keywords = ["log"] + +[badges] +travis-ci = { repository = "/rascul/rag" } + +[dependencies] +chrono = "0.4" +colored = "1.6" +log = "0.3" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5f27cf2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Ray Schulz + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1dc02d8 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +logger implementation diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..72d0334 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,32 @@ +use std::fmt::{Display, Formatter, Result as FmtResult}; + +use log::SetLoggerError; + +pub type RagResult = Result; + +#[derive(Debug)] +pub enum RagError { + SetLogger(SetLoggerError), + Empty, +} + +impl From<()> for RagError { + fn from(_: ()) -> RagError { + RagError::Empty + } +} + +impl From for RagError { + fn from(e: SetLoggerError) -> RagError { + RagError::SetLogger(e) + } +} + +impl Display for RagError { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + match *self { + RagError::SetLogger(ref e) => Display::fmt(e, f), + RagError::Empty => Display::fmt("Empty", f), + } + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..a01bd96 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,56 @@ +/// Logger implementation for colors in the terminal and in the future +/// logging to a file and to a remote log server + +extern crate chrono; +extern crate colored; +extern crate log; + +use colored::*; +use log::{Log, LogLevel, LogLevelFilter, LogMetadata, LogRecord, set_logger}; + +#[cfg(test)] +mod tests; +mod error; + +use error::RagResult; + +struct Logger; + +impl Log for Logger { + fn enabled(&self, metadata: &LogMetadata) -> bool { + metadata.level() <= LogLevel::Info + } + + fn log(&self, record: &LogRecord) { + if self.enabled(record.metadata()) { + let now = chrono::Local::now(); + let ts = now.format("%Y-%m-%dT%H:%M:%S%.3f%z").to_string(); + + let (msg, _level) = match record.level() { + LogLevel::Error => { + (format!("{} {} {}", ts.white().bold(), "ERR".red(), record.args()), 3) + } + LogLevel::Warn => { + (format!("{} {} {}", ts.white().bold(), "WRN".purple(), record.args()), 4) + } + LogLevel::Info => { + (format!("{} {} {}", ts.white().bold(), "INF".cyan(), record.args()), 6) + } + LogLevel::Debug => { + (format!("{} {} {}", ts.white().bold(), "DBG".yellow(), record.args()), 7) + } + LogLevel::Trace => { + (format!("{} {} {}", ts.white().bold(), "TRC".green(), record.args()), 0) + } + }; + println!("{}", msg); + } + } +} + +pub fn init() -> RagResult<()> { + Ok(set_logger(|max_log_level| { + max_log_level.set(LogLevelFilter::Info); + Box::new(Logger) + })?) +} diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..decff80 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,8 @@ +use super::*; + +#[test] +fn initialize() { + if init().is_err() { + panic!(); + } +}