From f09642876e61f34bae0e57471c3d89134824e8d8 Mon Sep 17 00:00:00 2001 From: adalpane Date: Fri, 2 Aug 2024 11:59:20 +0200 Subject: [PATCH] feature gate tokio dependency --- CHANGELOG.md | 7 +++++++ Cargo.toml | 10 +++++++--- README.md | 13 ++++++++++-- src/lib.rs | 16 ++++++++++++--- src/processor.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++ src/speak_easy.rs | 46 ++---------------------------------------- 6 files changed, 91 insertions(+), 52 deletions(-) create mode 100644 src/processor.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index ba1fe7c..14eb8b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.1.3 - 2024-08-02 + +### New features +- Feature gate tokio dependency + +## 0.1.2 - 2024-07-08 + ## 0.1.1 - 2024-05-17 ### New features diff --git a/Cargo.toml b/Cargo.toml index 9ec17f7..a77b65b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "speak-easy" -version = "0.1.2" +version = "0.1.3" authors = ["stefanodecillis"] edition = "2021" -description = "Logging functionalities with different levels and rotation options built on top of tokio-rs tracing." +description = "Logging functionalities with different levels and rotation options built on top of tracing and compatible with of tokio-rs." license = "MIT" readme = "README.md" repository = "https://github.com/stefanodecillis/speak-easy" @@ -15,9 +15,13 @@ categories = [ ] keywords = ["logging", "tracing", "async"] +[features] +default = ["tokio_async"] +tokio_async = ["tokio"] + [dependencies] -tokio = { version = "1.37.0", features = ["time", "rt"] } +tokio = { version = "1.37.0", features = ["time", "rt"], optional = true } tracing = "0.1.37" tracing-subscriber = "0.3.18" tracing-appender = "0.2" diff --git a/README.md b/README.md index 38c7bab..19145a1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ # Speak-Easy -Speak-Easy is a Rust library that provides logging functionalities with different levels and rotation options built on top of tokio-rs tracing. +Speak-Easy is a Rust library that provides logging functionalities with different levels and rotation options built on top of tracing and compatible with tokio-rs. ## Features @@ -21,10 +21,19 @@ First, add the following to your `Cargo.toml`: ```toml [dependencies] -speak-easy = "0.1.1" +speak-easy = { version = "0.1" } tokio = { features = ["macros", "rt-multi-thread"], version = "1.37.0" } ``` +**Note** + +If you want to use Speak-Easy without tokio, you must disable default features: + +```toml +[dependencies] +speak-easy = { version = "0.1", default-features = false } +``` + Then, use the library in your code like this: diff --git a/src/lib.rs b/src/lib.rs index 1480f3e..ba960ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ //! # speak-easy //! -//! Logging functionalities with different levels and rotation options built on top of tokio-rs tracing. +//! Logging functionalities with different levels and rotation options built on top of tracing and compatible with of tokio-rs //! //! ## Features //!| Feature | Status | @@ -32,11 +32,20 @@ //! //! ```toml //! [dependencies] -//! speak-easy = "0.1.1" +//! speak-easy = { version = "0.1" } //! tokio = { features = ["macros", "rt-multi-thread"], version = "1.37.0" } //! ``` +//! +//! **Note** +//! +//! If you want to use Speak-Easy without tokio, you must disable default features: +//! +//! ```toml +//! [dependencies] +//! speak-easy = { version = "0.1", default-features = false } +//! ``` //! -//! Remeber that the `tokio` dependency is required for the `tracing` crate. Your main function should look like this: +//! Your main function should look like this: //! //! ```rust //! #[tokio::main] @@ -100,6 +109,7 @@ //! mod formatter; +mod processor; pub use tracing::{debug, error, info, trace, warn, Level}; pub mod speak_easy; diff --git a/src/processor.rs b/src/processor.rs new file mode 100644 index 0000000..66f9234 --- /dev/null +++ b/src/processor.rs @@ -0,0 +1,51 @@ +use std::time::Duration; +use std::fs; +use std::io; + + +fn keep_last_logs(directory_path: &str, prefix: &str, holds_num: &usize) -> io::Result<()> { + // Read the directory + let mut entries = fs::read_dir(directory_path)? + .map(|res| res.map(|e| e.path())) + .collect::, io::Error>>()?; + + // Filter the entries to only include files that start with the prefix + entries.retain(|path| { + path.file_name() + .and_then(|name| name.to_str()) + .map(|name| name.starts_with(prefix)) + .unwrap_or(false) + }); + + // Sort the entries by modified date in descending order + entries.sort_by_key(|path| fs::metadata(path).and_then(|meta| meta.modified()).unwrap()); + entries.reverse(); + + // Remove all but the last five entries + for path in entries.into_iter().skip(*holds_num) { + fs::remove_file(path)?; + } + + Ok(()) +} + + +#[cfg(feature="tokio_async")] +pub fn spawn_processor(directory_path: String, prefix: String, cleanup_interval: u64, keep_last: usize) { + tokio::spawn(async move { + loop { + let _ = keep_last_logs(&directory_path, &prefix, &keep_last); + tokio::time::sleep(Duration::from_secs(cleanup_interval)).await; + } + }); +} + +#[cfg(not(feature="tokio_async"))] +pub fn spawn_processor(directory_path: String, prefix: String, cleanup_interval: u64, keep_last: usize) { + std::thread::spawn(move || { + loop { + let _ = keep_last_logs(&directory_path, &prefix, &keep_last); + std::thread::sleep(Duration::from_secs(cleanup_interval)); + } + }); +} \ No newline at end of file diff --git a/src/speak_easy.rs b/src/speak_easy.rs index e837a87..f0852ec 100644 --- a/src/speak_easy.rs +++ b/src/speak_easy.rs @@ -13,8 +13,7 @@ //! use crate::{ - formatter::LogsFileFormatter, - {Rotation, SpeakConfig}, + formatter::LogsFileFormatter, processor::spawn_processor, Rotation, SpeakConfig }; use tracing::Level; use tracing_subscriber::{ @@ -24,11 +23,6 @@ use tracing_subscriber::{ reload, }; -use std::fs; -use std::io; -use std::sync::Arc; -use tokio::time::{sleep, Duration}; - pub struct SpeakEasy {} impl SpeakEasy { @@ -73,17 +67,7 @@ impl SpeakEasy { } if speak_easy_config.clone().cleanup { - let directory_path = Arc::new(speak_easy_config.directory_path.clone()); - let prefix = Arc::new(speak_easy_config.prefix.clone()); - let cleanup_interval = Arc::new(speak_easy_config.cleanup_interval); - let holds_num = Arc::new(speak_easy_config.keep_last); - - tokio::spawn(async move { - loop { - let _ = SpeakEasy::keep_last_logs(&directory_path, &prefix, &holds_num); - sleep(Duration::from_secs(*cleanup_interval)).await; - } - }); + spawn_processor(speak_easy_config.directory_path.to_string(), speak_easy_config.prefix.to_string(), speak_easy_config.cleanup_interval, speak_easy_config.keep_last) } } None => { @@ -93,30 +77,4 @@ impl SpeakEasy { } }; } - - fn keep_last_logs(directory_path: &str, prefix: &str, holds_num: &usize) -> io::Result<()> { - // Read the directory - let mut entries = fs::read_dir(directory_path)? - .map(|res| res.map(|e| e.path())) - .collect::, io::Error>>()?; - - // Filter the entries to only include files that start with the prefix - entries.retain(|path| { - path.file_name() - .and_then(|name| name.to_str()) - .map(|name| name.starts_with(prefix)) - .unwrap_or(false) - }); - - // Sort the entries by modified date in descending order - entries.sort_by_key(|path| fs::metadata(path).and_then(|meta| meta.modified()).unwrap()); - entries.reverse(); - - // Remove all but the last five entries - for path in entries.into_iter().skip(*holds_num) { - fs::remove_file(path)?; - } - - Ok(()) - } }