hyperlane_log/log/
fn.rs

1use crate::*;
2
3/// Gets the maximum index number from log filenames in a directory.
4///
5/// # Arguments
6///
7/// - `&str` - Directory path to scan for log files.
8///
9/// # Returns
10///
11/// - `usize` - The highest index number found in filenames.
12pub(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
28/// Generates a log filename with given index.
29///
30/// # Arguments
31///
32/// - `usize` - Index number for the log file.
33///
34/// # Returns
35///
36/// - `String` - Formatted log filename.
37pub(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
49/// Generates directory name for current date's logs.
50///
51/// # Returns
52///
53/// - `String` - Directory name based on current date.
54pub(crate) fn get_file_dir_name() -> String {
55    format!("{}{}", ROOT_PATH, date())
56}
57
58/// Constructs appropriate log file path considering size limits.
59///
60/// # Arguments
61///
62/// - `&str` - System directory path.
63/// - `&str` - Base path for logs.
64/// - `&usize` - Maximum allowed file size in bytes.
65///
66/// # Returns
67///
68/// - `String` - Full path to appropriate log file.
69pub(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
91/// Formats log data with timestamp for each line.
92///
93/// # Arguments
94///
95/// - `T: ToString` - Data to be logged.
96///
97/// # Returns
98///
99/// - `String` - Formatted log string with timestamps.
100pub fn common_log<T: ToString>(data: T) -> String {
101    let mut log_string: String = String::new();
102    for line in data.to_string().lines() {
103        let line_string: String = format!("{}: {}{}", time(), line, BR);
104        log_string.push_str(&line_string);
105    }
106    log_string
107}
108
109/// Handles log data formatting.
110///
111/// # Arguments
112///
113/// - `T: ToString` - Data to be logged.
114///
115/// # Returns
116///
117/// - `String` - Formatted log string.
118pub fn log_handler<T: ToString>(log_data: T) -> String {
119    common_log(log_data)
120}