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) {
17let 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 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}