1use crate::*;
2
3pub(crate) fn get_second_element_from_filename(dir_path: &str) -> usize {
4 let mut res_idx: usize = DEFAULT_LOG_FILE_START_IDX;
5 if let Ok(entries) = read_dir(dir_path) {
6 for entry in entries.filter_map(Result::ok) {
7 let file_name: String = entry.file_name().to_string_lossy().to_string();
8 let parts: Vec<&str> = file_name.split(POINT).collect();
9 if parts.len() > 1 {
10 if let Ok(second_element) = parts[1].parse::<usize>() {
11 res_idx = second_element.max(res_idx);
12 }
13 }
14 }
15 }
16 res_idx.max(DEFAULT_LOG_FILE_START_IDX)
17}
18
19pub(crate) fn get_file_name(idx: usize) -> String {
20 format!(
21 "{}{}{}{}{}{}",
22 ROOT_PATH,
23 date(),
24 POINT,
25 idx,
26 POINT,
27 LOG_EXTENSION
28 )
29}
30
31pub(crate) fn get_file_dir_name() -> String {
32 format!("{}{}", ROOT_PATH, date())
33}
34
35pub(crate) fn get_log_path(system_dir: &str, base_path: &str, limit_file_size: &usize) -> String {
36 let mut combined_path: String = base_path.trim_end_matches(ROOT_PATH).to_string();
37 if !system_dir.starts_with(ROOT_PATH) {
38 combined_path.push_str(ROOT_PATH);
39 }
40 combined_path.push_str(
41 system_dir
42 .trim_start_matches(ROOT_PATH)
43 .trim_end_matches(ROOT_PATH),
44 );
45 combined_path.push_str(&get_file_dir_name());
46 let idx: usize = get_second_element_from_filename(&combined_path);
47 let mut combined_path_clone: String = combined_path.clone();
48 combined_path.push_str(&get_file_name(idx));
49 let file_size: usize = get_file_size(&combined_path).unwrap_or_default() as usize;
50 if &file_size <= limit_file_size {
51 return combined_path;
52 }
53 combined_path_clone.push_str(&get_file_name(idx + 1));
54 combined_path_clone
55}
56
57pub fn common_log<T: ToString>(data: T) -> String {
58 let mut log_string: String = String::new();
59 for line in data.to_string().lines() {
60 let line_string: String = format!("{}: {}{}", time(), line, BR);
61 log_string.push_str(&line_string);
62 }
63 log_string
64}
65
66pub fn log_handler<T: ToString>(log_data: T) -> String {
67 common_log(log_data)
68}