From a9ce90231cf39d126f9a5e611356a74c76299296 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sat, 2 Apr 2022 00:22:29 +0100 Subject: [PATCH] Add crate with simple logger code --- Cargo.toml | 23 +++++++++ src/lib.rs | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a370b53 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "rall" +version = "0.1.0" +edition = "2021" +authors = ["Goudham Suresh "] +description = "Really...? Another Logging Library? Yes! Incredibly Intuitive & Simple" +license = "MIT" +readme = "README.md" +repository = "https://github.com/sgoudham/rall" +keywords = ["logger", "logging", "simple", "coloured", "color"] +categories = ["development-tools::debugging"] + +[dependencies] +embed-doc-image = "0.1.4" +termcolor = "1.1.3" + +[features] +doc-images = [] + +[package.metadata.docs.rs] +# docs.rs uses a nightly compiler, so by instructing it to use our `doc-images` feature we +# ensure that it will render any images that we may have in inner attribute documentation. +features = ["doc-images"] \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..d6e0072 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,142 @@ +//! # Really...? Another Logging Library? +//! +//! **Yes!** +//! +//! ## Description +//! +//! _rall_ is an incredibly simple and intuitive logger, consider this crate a _failure_ if you +//! can't get setup within **30 seconds!** +//! +//! ## Feature Set +//! +//! - [x] Logging Levels +//! - [x] Coloured Output +//! - [ ] Options for Datetime, Current Function, Line Number, Custom Colours, etc. +//! - [ ] Custom Formatting +//! - [ ] File support +//! +//! And much more to come... soon™! +//! +//! ## Quick Start +//! +//! ```rust +//! use rall::SimpleLogger; +//! +//! // Create Default SimpleLogger +//! let mut simple_logger = SimpleLogger::default(); +//! +//! // Log Out To Standard Output +//! logger.log(Level::TRACE, "My Best Friend Hazel :D"); +//! logger.log(Level::DEBUG, "My Best Friend Hazel :D"); +//! logger.log(Level::INFO, "My Best Friend Hazel :D"); +//! logger.log(Level::WARN, "My Best Friend Hazel :D"); +//! logger.log(Level::ERROR, "My Best Friend Hazel :D"); +//! logger.log(Level::CRITICAL, "My Best Friend Hazel :D"); +//! ``` +//! +#![cfg_attr(feature = "doc-images", +cfg_attr(all(), +doc = ::embed_doc_image::embed_image!("windows_logs", "images/windows_logs.png")))] +//! +//! ### Windows Output +//! +//! ![Example Logs][windows_logs] +//! +//! ### Unix Output +//! +//! +//! +//! ### Author Notes +//! +//! I'm still incredibly early in my Rust journey and so I wanted to get comfortable and try to pick +//! my own brain about exposing different API's in a Rust crate. I hope to achieve an intuitive and +//! easy to understand API design that users can instantly get started with. + +use std::fmt::{Display, Formatter}; +use std::io::Write; +use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; + +/// TODO +pub enum Level { + TRACE, + DEBUG, + INFO, + WARN, + ERROR, + CRITICAL, +} + +impl Display for Level { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Level::TRACE => write!(f, "[TRACE]"), + Level::DEBUG => write!(f, "[DEBUG]"), + Level::INFO => write!(f, "[INFO]"), + Level::WARN => write!(f, "[WARN]"), + Level::ERROR => write!(f, "[ERROR]"), + Level::CRITICAL => write!(f, "[FATAL]"), + } + } +} + +/// TODO +pub struct SimpleLogger { + standard_stream: StandardStream, +} + +impl Default for SimpleLogger { + fn default() -> Self { + Self { + standard_stream: StandardStream::stdout(ColorChoice::Always), + } + } +} + +/// TODO +impl SimpleLogger { + /// TODO + pub fn new(standard_stream: StandardStream) -> Self { + Self { standard_stream } + } + + /// TODO + pub fn log(&mut self, level: Level, str: &str) { + self.set_colour(&level); + writeln!(&mut self.standard_stream, "{} {}", level, str).unwrap(); + self.standard_stream.reset().unwrap(); + } + + /// TODO + fn set_colour(&mut self, logging_level: &Level) { + match logging_level { + Level::TRACE => self + .standard_stream + .set_color(ColorSpec::new().set_fg(Some(Color::Blue)).set_bold(true)) + .unwrap(), + Level::DEBUG => self + .standard_stream + .set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true)) + .unwrap(), + Level::INFO => self + .standard_stream + .set_color(ColorSpec::new().set_fg(None)) + .unwrap(), + Level::WARN => self + .standard_stream + .set_color( + ColorSpec::new() + .set_fg(Some(Color::Yellow)) + .set_dimmed(true), + ) + .unwrap(), + Level::ERROR => self + .standard_stream + .set_color(ColorSpec::new().set_fg(Some(Color::Red))) + .unwrap(), + Level::CRITICAL => self + .standard_stream + .set_color(ColorSpec::new().set_fg(Some(Color::Red)).set_intense(true)) + .unwrap(), + } + } +} \ No newline at end of file