logger_rust/
set_log.rs

1pub use crate::config::{LogVariables, LogVariablesImpl, LogLevel, LOG_ROTATOR_CONFIG, LOG_MUTEX};
2pub use crate::{log_error, LOG_PATH};
3pub use crate::log_rotator::{
4    LogPath,
5    LogConfig,
6    LogRotatorConfig,
7
8};
9use std::{
10    path::PathBuf,
11    process,
12    thread,
13    time::Duration,
14};
15
16pub fn set_log_level(level: LogLevel) {
17//! Sets the log level to the provided value.
18//!
19//! # Examples
20//!
21//! ```
22//! use logger_rust::*;
23//! 
24//! fn main() {
25//! // Set the log level to File
26//!   set_log_level(LogLevel::File);
27//! }
28//! ```
29//! 
30    let log_variables = LogVariablesImpl;
31    let mut log_level = log_variables.log_level().lock().unwrap();
32    *log_level = level;
33}
34
35pub fn set_log_path(config: LogConfig) {
36    //! Sets the log path and, optionally, the log rotator configuration.
37    //!
38    //! This function takes a `LogConfig` argument that specifies the log path and, optionally,
39    //! the log rotator configuration. If a log rotator configuration is provided, the log rotator
40    //! will automatically rotate the logs when necessary based on the provided configuration.
41    //!
42    //! # Arguments
43    //!
44    //! * `config` - The log configuration. Can be either a `LogConfig::Path` variant that specifies
45    //!   the log path or a `LogConfig::Rotator` variant that specifies the log path and the log
46    //!   rotator configuration.
47    //!
48    //! # Examples
49    //! ### Set the log path without a log rotator:
50    //! ```
51    //! use logger_rust::*;
52    //! use std::time::Duration;
53    //! use std::path::PathBuf;
54    //!
55    //! set_log_level(LogLevel::File);
56    //! set_log_path(LogConfig::Path(LogPath::from("C:/Users/qruie/Documents")));
57    //! ```
58    //! 
59    //! ### Set the log path with a log rotator
60    //! ```
61    //! use logger_rust::*;
62    //! use std::time::Duration;
63    //! use std::path::PathBuf;
64    //! 
65    //! set_log_level(LogLevel::File);
66    //! set_log_path(LogConfig::Rotator(LogRotatorConfig::new(
67    //!     PathBuf::from("C:/Users/qruie/Documents"),
68    //!     5 * 1024 * 1024,
69    //!     Duration::from_secs(2),
70    //! )));
71    //! ```
72    let log_variables = LogVariablesImpl;
73    let log_level = log_variables.log_level().lock().unwrap();
74    if *log_level != LogLevel::File && *log_level != LogLevel::Both {
75        panic!("
76        Cannot call set_log_path when log level is no set to `LogLevel::File` or `LogLevel::Both`
77        Please, specify the `LogLevel::Console` or `LogLevel::Path` if you want to use `set_log_path`
78        `set_log_level(LogLevel::File);` // OR `LogLevel::Both` <──────────────────────────────┘
79        ---------------------------------------\n");
80    }
81    let handle = thread::spawn(move || {
82        let path = match config {
83            LogConfig::Path(LogPath::Path(path)) => path,
84            LogConfig::Rotator(rotator_config) => {
85                let log_path = rotator_config.log_path.clone();
86                let mut log_rotator_config = LOG_ROTATOR_CONFIG.lock().unwrap();
87                *log_rotator_config = Some(rotator_config);
88                log_path
89            }
90        };
91        match (
92            path.exists(),
93            path.is_dir(),
94            path.metadata().map(|m| m.permissions().readonly()),
95        ) {
96            (false, _, _) => {
97                log_error!("Path is not correct: {}", path.display());
98                thread::sleep(Duration::from_secs(10));
99                process::exit(1);
100            }
101            (_, false, _) => {
102                log_error!("Path is not a directory: {}", path.display());
103                thread::sleep(Duration::from_secs(10));
104                process::exit(1);
105            }
106            (_, _, Err(e)) => {
107                log_error!("Failed to get metadata for path {}: {}", path.display(), e);
108                thread::sleep(Duration::from_secs(10));
109                process::exit(1);
110            }
111            (_, _, Ok(false)) => {
112                log_error!("Not enough permissions to access the path: '{}'", path.display());
113                thread::sleep(Duration::from_secs(10));
114                process::exit(1);
115            }
116            _ => {}
117        }
118        let mut log_path = LOG_PATH.lock().unwrap();
119        *log_path = PathBuf::from(path);
120    });
121    handle.join().unwrap();
122}