1use crate::*;
2
3pub(crate) fn get_second_element_from_filename(dir_path: &str) -> usize {
13 let mut res_idx: usize = DEFAULT_LOG_FILE_START_IDX;
14 if let Ok(entries) = read_dir(dir_path) {
15 for entry in entries.filter_map(Result::ok) {
16 let file_name: String = entry.file_name().to_string_lossy().to_string();
17 let parts: Vec<&str> = file_name.split(POINT).collect();
18 if parts.len() > 1 {
19 if let Ok(second_element) = parts[1].parse::<usize>() {
20 res_idx = second_element.max(res_idx);
21 }
22 }
23 }
24 }
25 res_idx.max(DEFAULT_LOG_FILE_START_IDX)
26}
27
28pub(crate) fn get_file_name(idx: usize) -> String {
38 format!(
39 "{}{}{}{}{}{}",
40 ROOT_PATH,
41 date(),
42 POINT,
43 idx,
44 POINT,
45 LOG_EXTENSION
46 )
47}
48
49pub(crate) fn get_file_dir_name() -> String {
55 format!("{}{}", ROOT_PATH, date())
56}
57
58pub(crate) fn get_log_path(system_dir: &str, base_path: &str, limit_file_size: &usize) -> String {
70 let mut combined_path: String = base_path.trim_end_matches(ROOT_PATH).to_string();
71 if !system_dir.starts_with(ROOT_PATH) {
72 combined_path.push_str(ROOT_PATH);
73 }
74 combined_path.push_str(
75 system_dir
76 .trim_start_matches(ROOT_PATH)
77 .trim_end_matches(ROOT_PATH),
78 );
79 combined_path.push_str(&get_file_dir_name());
80 let idx: usize = get_second_element_from_filename(&combined_path);
81 let mut combined_path_clone: String = combined_path.clone();
82 combined_path.push_str(&get_file_name(idx));
83 let file_size: usize = get_file_size(&combined_path).unwrap_or_default() as usize;
84 if &file_size <= limit_file_size {
85 return combined_path;
86 }
87 combined_path_clone.push_str(&get_file_name(idx + 1));
88 combined_path_clone
89}
90
91pub fn common_log<T: AsRef<str>>(data: T) -> String {
101 let mut log_string: String = String::new();
102 for line in data.as_ref().lines() {
103 let line_string: String = format!("{}: {}{}", time(), line, BR);
104 log_string.push_str(&line_string);
105 }
106 log_string
107}
108
109pub fn log_handler<T: AsRef<str>>(log_data: T) -> String {
119 common_log(log_data)
120}