logger_rust/log_rotator/
mod.rs

1/// ## Log rotation
2/// From version 1.0.39, you can create a `log rotator` instance which allows you to split logs by their size and duration.
3/// - log_path: path to log directory;
4/// - max_size (`u64`): maximum size of log file;
5/// - max_life (`std::time::Duration`): maximum lifetime of log file;
6/// 
7/// Here's an example:
8/// ```rust
9/// use logger_rust::*; // importing 
10/// use std::{
11///     path::PathBuf,
12///     time::Duration
13/// };
14/// 
15/// fn main() {
16///     //set_log_level(LogLevel::File);
17///     //set_log_path(LogConfig::Rotator(LogRotatorConfig::new(
18///         //PathBuf::from("C:/Users/JK/Desktop"),
19///         //5 * 15 * 15, <-- im not able to pass the testdoc cuz of that lol
20///         //Duration::from_secs(2),
21///     //)));
22/// }
23/// ```
24
25use std::{
26    time::Duration,
27    path::PathBuf,
28};
29pub enum LogConfig {
30    /// # LogConfig enum
31    /// Defines the Path variable as std::PathBuf for path
32    /// And a Rotator variable linked to the LogRotatorConfig implementor
33    Path(LogPath),
34    Rotator(LogRotatorConfig),
35}
36
37pub enum LogPath {
38    /// # LogPath enum
39    /// Defines the Path variable as std::PathBuf for path
40    /// What should i even write here and why?
41    Path(PathBuf),
42}
43
44pub struct LogRotatorConfig {
45    /// # LogRotatorConfig struct
46    /// Defines the public variables for LogRotator instanse
47    /// `log_path` = string
48    /// `max-size` = u64 (e.g 5 * 1024 * 1024)
49    /// `max_time` = time::Duration
50    pub log_path: PathBuf,
51    pub max_size: u64,
52    pub max_time: Duration,
53}
54
55impl LogRotatorConfig {
56    pub fn new(log_path: PathBuf, max_size: u64, max_time: Duration) -> Self {
57        //! # LogRotatorConfig::New
58        //! The inializer for configuration of log rotator:
59        //! ```rust
60        //! use logger_rust::*;
61        //! use std::time::Duration;
62        //! use std::path::PathBuf;
63        //! 
64        //! fn main() {
65        //!     set_log_level(LogLevel::File);
66        //!     set_log_path(LogConfig::Rotator(LogRotatorConfig::new(
67        //!         PathBuf::from("C:/Users/qruie/Documents"), // Logging directory
68        //!         5 * 1024 * 1024, // 5MB
69        //!         Duration::from_secs(2), // Duration for log splits
70        //!     )));
71        //! }
72        //! ```
73        Self {
74            log_path,
75            max_size,
76            max_time,
77        }
78    }
79}
80
81impl From<&str> for LogPath {
82/// `From` trait implementation for `LogPath` that allows it to be constructed from a string slice.
83///
84/// # Examples
85///
86/// ```
87/// use logger_rust::*;
88///
89/// fn main() {
90///     set_log_level(LogLevel::File);
91///     set_log_path(LogConfig::Path(LogPath::from("C:/Users/qruie/Documents")));
92///     // ..
93/// }
94/// ```
95    fn from(s: &str) -> Self {
96        Self::Path(PathBuf::from(s))
97    }
98}
99
100impl From<PathBuf> for LogPath {
101/// `From` trait implementation for `LogPath` that allows it to be constructed from a `PathBuf`.
102///
103/// # Examples
104///
105/// ```
106/// use logger_rust::*;
107/// use std::path::PathBuf;
108///
109/// fn main() {
110///     set_log_level(LogLevel::File);
111///     set_log_path(LogConfig::Path(LogPath::from("C:/Users/qruie/Documents")));
112///     // ..
113/// }
114/// ```
115    fn from(path: PathBuf) -> Self {
116        Self::Path(path)
117    }
118}