From eed1bc5415fa5a068bb715ce5ddda70617d22f7b Mon Sep 17 00:00:00 2001 From: Sebastien Rousseau Date: Sat, 11 May 2024 17:22:30 +0100 Subject: [PATCH] refactor(hsh): :heavy_minus_sign: remove loggers functions --- src/lib.rs | 3 - src/loggers.rs | 243 ------------------------------------------ src/macros.rs | 36 ------- tests/test_loggers.rs | 129 ---------------------- 4 files changed, 411 deletions(-) delete mode 100644 src/loggers.rs delete mode 100644 tests/test_loggers.rs diff --git a/src/lib.rs b/src/lib.rs index 0174850..03fe6e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,9 +108,6 @@ /// The `algorithms` module contains the password hashing algorithms. pub mod algorithms; -/// The `loggers` module contains the loggers for the library. -pub mod loggers; - /// The `macros` module contains functions for generating macros. pub mod macros; diff --git a/src/loggers.rs b/src/loggers.rs deleted file mode 100644 index 78be83b..0000000 --- a/src/loggers.rs +++ /dev/null @@ -1,243 +0,0 @@ -// Copyright © 2023 Hash (HSH) library. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 OR MIT - -// Standard library imports for formatting and I/O operations. -use std::{ - fmt, - fs::OpenOptions, - io::{self, Write as IoWrite}, -}; - -/// Enum representing the different log formats that can be used. -/// -/// This enum allows the developer to specify the format in which log messages should be displayed. -/// -#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, PartialOrd)] -pub enum LogFormat { - /// The log format is set to the Common Log Format (CLF) - CLF, - /// The log format is set to the JSON format - JSON, - /// The log format is set to the Common Event Format (CEF) - CEF, - /// The log format is set to the Extended Log Format (ELF) - ELF, - /// The log format is set to the W3C Extended Log File Format - W3C, - /// The log format is set to the Graylog Extended Log Format (GELF) - GELF, -} - -/// Implements Display trait for `LogFormat` enum. -/// -/// This allows easy conversion of the log format enums to strings. -impl fmt::Display for LogFormat { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - writeln!(f, "{self:?}") - } -} - -/// An enumeration of the different levels that a log message can have. -/// Each variant of the enumeration represents a different level of -/// importance. -/// -/// # Arguments -/// -/// * `ALL` - The log level is set to all. -/// * `DEBUG` - The log level is set to debug. -/// * `DISABLED` - The log level is set to disabled. -/// * `ERROR` - The log level is set to error. -/// * `FATAL` - The log level is set to fatal. -/// * `INFO` - The log level is set to info. -/// * `NONE` - The log level is set to none. -/// * `TRACE` - The log level is set to trace. -/// * `VERBOSE` - The log level is set to verbose. -/// * `WARNING` - The log level is set to warning. -/// -#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, PartialOrd)] -pub enum LogLevel { - /// The log level is set to all. - ALL, - /// The log level is set to debug. - DEBUG, - /// The log level is set to disabled. - DISABLED, - /// The log level is set to error. - ERROR, - /// The log level is set to fatal. - FATAL, - /// The log level is set to info. - INFO, - /// The log level is set to none. - NONE, - /// The log level is set to trace. - TRACE, - /// The log level is set to verbose. - VERBOSE, - /// The log level is set to warning. - WARNING, -} -/// Display trait implementation for `LogLevel`. -/// -/// This converts the enum to a string representation. -impl fmt::Display for LogLevel { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{self:?}") - } -} - -/// Struct representing a log message. -/// -/// Contains all the elements that make up a complete log message. -#[derive(Debug, PartialEq, Eq, Hash, Clone)] -pub struct Log { - /// A string that holds a session ID. The session ID is a unique - /// identifier for the current session. A random GUID (Globally - /// Unique Identifier) is generated by default. - pub session_id: String, - /// A string that holds the timestamp in ISO 8601 format. - pub time: String, - /// A string that holds the level (INFO, WARN, ERROR, etc.). - pub level: LogLevel, - /// A string that holds the component name. - pub component: String, - /// A string that holds the description of the log message. - pub description: String, - /// A string that holds the log format. - pub format: LogFormat, -} - -impl Log { - /// Logs a message to the console using a pre-allocated buffer to - /// reduce memory allocation and flush the output buffer to ensure - /// that the message is written immediately. - /// - /// # Errors - /// - /// This function will panic if an error occurs when writing to the - /// pre-allocated buffer or flushing the output buffer. - pub fn log(&self) -> io::Result<()> { - // Open the file in append mode. If the file does not exist, create it. - let mut file = OpenOptions::new() - .write(true) - .truncate(true) - .open("xtasks.log")?; - match self.format { - LogFormat::CLF => { - writeln!( - file, - "SessionID={}\tTimestamp={}\tDescription={}\tLevel={}\tComponent={}\tFormat={}", - self.session_id, - self.time, - self.description, - self.level, - self.component, - self.format - ) - } - LogFormat::JSON => { - writeln!( - file, - r#"{{"session_id": "{}", "timestamp": "{}", "description": "{}", "level": "{}", "component": "{}", "format": "{}"}}"#, - self.session_id, - self.time, - self.description, - self.level, - self.component, - self.format - ) - } - LogFormat::CEF => { - writeln!( - file, - r#"[CEF] - - 1 - xtasks - Application - {} - Log - {} - {} - {} - localhost - localhost - - - - - - - - - - - - - - "#, - self.time, - self.level, - self.description, - self.session_id - ) - } - _ => Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Unsupported log format", - )), - }?; - file.flush()?; - Ok(()) - } - - /// Creates a new `Log` instance. - /// - /// Initializes a new `Log` struct with the provided details. - /// - /// # Returns - /// - /// Returns a new instance of the `Log` struct. - pub fn new( - session_id: &str, - time: &str, - level: LogLevel, - component: &str, - description: &str, - format: LogFormat, - ) -> Self { - Self { - session_id: session_id.to_string(), - time: time.to_string(), - level, - component: component.to_string(), - description: description.to_string(), - format, - } - } -} - -/// Provides default values for `Log`. -/// -/// This implementation provides a quick way to generate a `Log` instance with default values. -impl Default for Log { - fn default() -> Self { - Self { - session_id: String::default(), - time: String::default(), - level: LogLevel::INFO, // Default log level - component: String::default(), - description: String::default(), - format: LogFormat::CLF, // Default log format - } - } -} -#[cfg(test)] -/// Tests for the `log_info!` macro. -mod tests { - use crate::macro_log_info; - - #[test] - fn test_log_info() { - macro_log_info!( - LogLevel::INFO, - "component", - "description", - LogFormat::CLF - ); - } -} diff --git a/src/macros.rs b/src/macros.rs index afb6b81..9a9f470 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -434,42 +434,6 @@ macro_rules! hash_length { }; } -/// Custom logging macro for various log levels and formats. -/// -/// # Parameters -/// -/// * `$level`: The log level of the message. -/// * `$component`: The component where the log is coming from. -/// * `$description`: A description of the log message. -/// * `$format`: The format of the log message. -/// -#[macro_export] -macro_rules! macro_log_info { - ($level:expr, $component:expr, $description:expr, $format:expr) => {{ - use dtt::DateTime; - use vrd::random::Random; - use $crate::loggers::{Log, LogFormat, LogLevel}; - - // Get the current date and time in ISO 8601 format. - let date = DateTime::new(); - let iso = date.iso_8601; - - // Create a new random number generator - let mut rng = Random::default(); - let session_id = rng.rand().to_string(); - - let log = Log::new( - &session_id, - &iso, - $level, - $component, - $description, - $format, - ); - let _ = log.log(); - }}; -} - /// Macros related to executing shell commands. /// /// Executes a shell command, logs the start and completion of the operation, and handles any errors that occur. diff --git a/tests/test_loggers.rs b/tests/test_loggers.rs deleted file mode 100644 index e553b56..0000000 --- a/tests/test_loggers.rs +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright © 2023 Hash (HSH) library. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 OR MIT - -#[cfg(test)] -mod tests { - - // fn test_macro_log( - // level: LogLevel, - // component: &str, - // description: &str, - // format: LogFormat, - // ) { - // let log = - // macro_log_info!(level, component, description, format); - // assert_eq!(log.level, level); - // assert_eq!(log.component, component); - // assert_eq!(log.description, description); - // assert_eq!(log.format, format); - // } - - // #[test] - // fn test_macros() { - // test_macro_log( - // LogLevel::ALL, - // "component", - // "description", - // LogFormat::CLF, - // ); - // test_macro_log( - // LogLevel::DEBUG, - // "component", - // "description", - // LogFormat::CLF, - // ); - // test_macro_log( - // LogLevel::DISABLED, - // "component", - // "description", - // LogFormat::CLF, - // ); - // test_macro_log( - // LogLevel::ERROR, - // "component", - // "description", - // LogFormat::CLF, - // ); - // test_macro_log( - // LogLevel::FATAL, - // "component", - // "description", - // LogFormat::CLF, - // ); - // test_macro_log( - // LogLevel::INFO, - // "component", - // "description", - // LogFormat::CLF, - // ); - // test_macro_log( - // LogLevel::NONE, - // "component", - // "description", - // LogFormat::CLF, - // ); - // test_macro_log( - // LogLevel::TRACE, - // "component", - // "description", - // LogFormat::CLF, - // ); - // test_macro_log( - // LogLevel::VERBOSE, - // "component", - // "description", - // LogFormat::CLF, - // ); - // test_macro_log( - // LogLevel::WARNING, - // "component", - // "description", - // LogFormat::CLF, - // ); - // } - - use hsh::loggers::{Log, LogFormat, LogLevel}; - - #[test] - fn test_log_level_display() { - let level = LogLevel::INFO; - assert_eq!(format!("{level}"), "INFO"); - } - - #[test] - fn test_log_format_display() { - let format = LogFormat::JSON; - assert_eq!(format!("{format}"), "JSON\n"); - } - - #[test] - fn test_log_new() { - let log = Log::new( - "session123", - "2023-02-28T12:34:56Z", - LogLevel::WARNING, - "auth", - "Invalid credentials", - LogFormat::CLF, - ); - - assert_eq!(log.session_id, "session123"); - assert_eq!(log.time, "2023-02-28T12:34:56Z"); - assert_eq!(log.level, LogLevel::WARNING); - assert_eq!(log.component, "auth"); - assert_eq!(log.description, "Invalid credentials"); - assert_eq!(log.format, LogFormat::CLF); - } - - #[test] - fn test_log_default() { - let log = Log::default(); - - assert!(log.session_id.is_empty()); - assert!(log.time.is_empty()); - assert_eq!(log.level, LogLevel::INFO); - assert!(log.component.is_empty()); - assert!(log.description.is_empty()); - assert_eq!(log.format, LogFormat::CLF); - } -}