From 9415d265b32868a12acb442c3c227b44e3987343 Mon Sep 17 00:00:00 2001 From: dousamichal0807 Date: Wed, 13 Apr 2022 19:01:47 +0200 Subject: [PATCH] Split the `Logger` trait, `LogLevel` and structs implementing `Logger` trait. --- src/auto.rs | 24 +++++++ src/level.rs | 68 +++++++++++++++++++ src/lib.rs | 117 ++++++--------------------------- src/loggers.rs | 5 ++ src/{ => loggers}/composite.rs | 16 +++-- 5 files changed, 127 insertions(+), 103 deletions(-) create mode 100644 src/auto.rs create mode 100644 src/level.rs create mode 100644 src/loggers.rs rename src/{ => loggers}/composite.rs (76%) diff --git a/src/auto.rs b/src/auto.rs new file mode 100644 index 0000000..e713a65 --- /dev/null +++ b/src/auto.rs @@ -0,0 +1,24 @@ +use crate::LogLevel; + +use std::io; + +/// Trait for implementing a logging utility. +/// +/// This trait has only one method, that is [`log`]. This method is called whenever +/// some operation, that may be important, happens. +/// +/// Note that [`Logger`] trait does not specify how given message is logged nor +/// where it should be logged to. +pub trait Logger { + /// Method for logging a message. + /// + /// # Parameters + /// + /// - `log_level`: [`LogLevel`] of given message + /// - `message`: message text to be logged + /// + /// # Return value + /// + /// [`std::io::Result`]`<()>` indicating, whether the message was successfully logged + fn log(&mut self, log_level: LogLevel, message: &str) -> io::Result<()>; +} \ No newline at end of file diff --git a/src/level.rs b/src/level.rs new file mode 100644 index 0000000..2cfc43b --- /dev/null +++ b/src/level.rs @@ -0,0 +1,68 @@ +use std::cmp::Ordering; +use std::fmt; +use std::fmt::Display; +use std::fmt::Formatter; + +/// Represents how important a log message is. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum LogLevel { + /// [`LogLevel`] for debugging purposes. + Debug, + + /// [`LogLevel`] for informational messages. + Info, + + /// [`LogLevel`] for messages that warns the user of application about potential risks. + Warning, + + /// [`LogLevel`] for messages that informs the user that something went wrong and the error is + /// cannot be corrected, but the application can continue running. + Error, + + /// [`LogLevel`] for messages that informs the user that something went wrong and the error is + /// unrecoverable and the application cannot continue. + Fatal, +} + +impl LogLevel { + /// Describes the [`LogLevel`] as a number ([`u8`]). + /// + /// # Return value + /// + /// [`LogLevel`] represented as an [`u8`] + pub fn numeric_level(&self) -> u8 { + match self { + Self::Debug => 0, + Self::Info => 1, + Self::Warning => 2, + Self::Error => 3, + Self::Fatal => 4, + } + } +} + +impl Display for LogLevel { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + writeln!(f, "{}", + match self { + Self::Debug => "DEBUG", + Self::Info => "INFO", + Self::Warning => "WARN", + Self::Error => "ERROR", + Self::Fatal => "FATAL", + } + ) + } +} + +impl PartialOrd for LogLevel { + fn partial_cmp(&self, other: &Self) -> Option { + Option::Some(self.cmp(other)) + } +} + +impl Ord for LogLevel { + fn cmp(&self, other: &Self) -> Ordering { + self.numeric_level().cmp(&other.numeric_level()) + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index e5d8dd4..08005e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,96 +1,21 @@ -pub mod composite; -pub mod text; - -pub use self::composite::CompositeLogger; -pub use self::text::TextLogger; - -use std::cmp::Ordering; -use std::fmt; -use std::fmt::Display; -use std::fmt::Formatter; -use std::io; - -/// Trait for implementing a logging utility. -/// -/// This trait has only one method, that is [`log`]. This method is called whenever -/// some operation, that may be important, happens. -/// -/// Note that [`Logger`] trait does not specify how given message is logged nor -/// where it should be logged to. -pub trait Logger { - /// Method for logging a message. - /// - /// # Parameters - /// - /// - `log_level`: [`LogLevel`] of given message - /// - `message`: message text to be logged - /// - /// # Return value - /// - /// [`std::io::Result`]`<()>` indicating, whether the message was successfully logged - fn log(&mut self, log_level: LogLevel, message: &str) -> io::Result<()>; -} - -/// Represents how important a log message is. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum LogLevel { - /// [`LogLevel`] for debugging purposes. - Debug, - - /// [`LogLevel`] for informational messages. - Info, - - /// [`LogLevel`] for messages that warns the user of application about potential risks. - Warning, - - /// [`LogLevel`] for messages that informs the user that something went wrong and the error is - /// cannot be corrected, but the application can continue running. - Error, - - /// [`LogLevel`] for messages that informs the user that something went wrong and the error is - /// unrecoverable and the application cannot continue. - Fatal, -} - -impl LogLevel { - /// Describes the [`LogLevel`] as a number ([`u8`]). - /// - /// # Return value - /// - /// [`LogLevel`] represented as an [`u8`] - pub fn numeric_level(&self) -> u8 { - match self { - Self::Debug => 0, - Self::Info => 1, - Self::Warning => 2, - Self::Error => 3, - Self::Fatal => 4, - } - } -} - -impl Display for LogLevel { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - writeln!(f, "{}", - match self { - Self::Debug => "DEBUG", - Self::Info => "INFO", - Self::Warning => "WARN", - Self::Error => "ERROR", - Self::Fatal => "FATAL", - } - ) - } -} - -impl PartialOrd for LogLevel { - fn partial_cmp(&self, other: &Self) -> Option { - Option::Some(self.cmp(other)) - } -} - -impl Ord for LogLevel { - fn cmp(&self, other: &Self) -> Ordering { - self.numeric_level().cmp(&other.numeric_level()) - } -} \ No newline at end of file +//! A simple crate for logging. +//! +//! # The [`Logger`] trait +//! +//! All loggers implement [`Logger`] trait. See [`Logger`] trait for more +//! information. +//! +//! # Featured logger types +//! +//! - [`CompositeLogger`](crate::loggers::CompositeLogger) +//! - [`TextLogger`](crate::loggers::TextLogger) +#![forbid(unsafe_code)] +#![forbid(unused_crate_dependencies)] +#![forbid(unused_extern_crates)] + +pub mod auto; +pub mod level; +pub mod loggers; + +pub use crate::auto::Logger; +pub use crate::level::LogLevel; \ No newline at end of file diff --git a/src/loggers.rs b/src/loggers.rs new file mode 100644 index 0000000..42fc2c7 --- /dev/null +++ b/src/loggers.rs @@ -0,0 +1,5 @@ +pub mod composite; +pub mod text; + +pub use self::composite::CompositeLogger; +pub use self::text::TextLogger; \ No newline at end of file diff --git a/src/composite.rs b/src/loggers/composite.rs similarity index 76% rename from src/composite.rs rename to src/loggers/composite.rs index c12f31f..a7bb107 100644 --- a/src/composite.rs +++ b/src/loggers/composite.rs @@ -1,6 +1,6 @@ -use std::borrow::Borrow; -use std::borrow::BorrowMut; use std::io; +use std::ops::Deref; +use std::ops::DerefMut; use crate::Logger; use crate::LogLevel; @@ -9,7 +9,7 @@ type LoggerVec = Vec>; /// Represents a [`Logger`] that consists of many [`Logger`]s. /// -/// [`CompositeLogger`] is used through its [`Borrow`] and [`BorrowMut`] +/// [`CompositeLogger`] is used through its [`Deref`] and [`DerefMut`] /// implementations which yield a borrow to the inner [`Vec`] of [`Logger`]s. pub struct CompositeLogger (LoggerVec); @@ -35,14 +35,16 @@ impl Logger for CompositeLogger { } } -impl Borrow for CompositeLogger { - fn borrow(&self) -> &LoggerVec { +impl Deref for CompositeLogger { + type Target = LoggerVec; + + fn deref(&self) -> &LoggerVec { &self.0 } } -impl BorrowMut for CompositeLogger { - fn borrow_mut(&mut self) -> &mut LoggerVec { +impl DerefMut for CompositeLogger { + fn deref_mut(&mut self) -> &mut LoggerVec { &mut self.0 } } \ No newline at end of file