hyperlane_log/log/
impl.rs

1use super::{
2    constant::*,
3    r#trait::{LogDataTrait, LogFuncTrait},
4    r#type::{Log, LogListArcLock},
5    utils::*,
6};
7use crate::{ArcLogFunc, ListLog};
8use file_operation::*;
9use http_type::*;
10use hyperlane_time::*;
11use once_cell::sync::Lazy;
12use std::{
13    sync::{Arc, RwLock},
14    vec,
15};
16
17static LOG_ERROR_QUEUE: Lazy<LogListArcLock> = Lazy::new(|| Arc::new(RwLock::new(Vec::new())));
18static LOG_INFO_QUEUE: Lazy<LogListArcLock> = Lazy::new(|| Arc::new(RwLock::new(Vec::new())));
19static LOG_DEBUG_QUEUE: Lazy<LogListArcLock> = Lazy::new(|| Arc::new(RwLock::new(Vec::new())));
20
21impl Default for Log {
22    #[inline]
23    fn default() -> Self {
24        Self {
25            path: DEFAULT_LOG_DIR.to_owned(),
26            file_size: DEFAULT_LOG_FILE_SIZE,
27            interval_millis: DEFAULT_LOG_INTERVAL_MILLIS,
28        }
29    }
30}
31
32impl Log {
33    #[inline]
34    pub fn new<T>(path: T, file_size: usize, interval_millis: usize) -> Self
35    where
36        T: Into<String>,
37    {
38        Self {
39            path: path.into(),
40            file_size,
41            interval_millis,
42        }
43    }
44
45    #[inline]
46    fn write(list: &mut Vec<(String, ArcLogFunc)>, path: &str) {
47        for (log_string, func) in list.iter() {
48            let out: String = func(log_string);
49            let _ = append_to_file(path, &out.as_bytes());
50        }
51    }
52
53    #[inline]
54    fn add_data<T, L>(log_queue: &LogListArcLock, data: T, func: L)
55    where
56        T: LogDataTrait,
57        L: LogFuncTrait,
58    {
59        let data_string: String = data.into();
60        {
61            if let Ok(mut queue) = log_queue.write() {
62                queue.push((data_string, Arc::new(func)));
63            }
64        }
65    }
66
67    #[inline]
68    fn get_file_name(&self, idx: usize) -> String {
69        format!(
70            "{}{}{}{}{}{}",
71            ROOT_PATH,
72            current_date(),
73            POINT,
74            idx,
75            POINT,
76            LOG_EXTION
77        )
78    }
79
80    #[inline]
81    fn get_file_dir_name(&self) -> String {
82        format!("{}{}", ROOT_PATH, current_date())
83    }
84
85    #[inline]
86    fn get_log_path(&self, system_dir: &str) -> String {
87        let base_path: &String = self.get_path();
88        let mut combined_path: String = base_path.trim_end_matches(ROOT_PATH).to_string();
89        if !system_dir.starts_with(ROOT_PATH) {
90            combined_path.push_str(ROOT_PATH);
91        }
92        combined_path.push_str(
93            system_dir
94                .trim_start_matches(ROOT_PATH)
95                .trim_end_matches(ROOT_PATH),
96        );
97        combined_path.push_str(&self.get_file_dir_name());
98        let idx: usize = get_second_element_from_filename(&combined_path);
99        let mut combined_path_clone: String = combined_path.clone();
100        combined_path.push_str(&self.get_file_name(idx));
101        let file_size: usize = get_file_size(&combined_path).unwrap_or_default() as usize;
102        if file_size <= self.file_size {
103            return combined_path;
104        }
105        combined_path_clone.push_str(&self.get_file_name(idx + 1));
106        combined_path_clone
107    }
108
109    #[inline]
110    pub(super) fn write_error(&self) {
111        let mut list: ListLog = if let Ok(mut error) = LOG_ERROR_QUEUE.write() {
112            let tmp_error: ListLog = error.drain(..).collect::<Vec<_>>();
113            tmp_error
114        } else {
115            vec![]
116        };
117        Self::write(&mut list, &self.get_log_path(ERROR_DIR));
118    }
119
120    #[inline]
121    pub(super) fn write_info(&self) {
122        let mut list: ListLog = if let Ok(mut info) = LOG_INFO_QUEUE.write() {
123            let tmp_info: ListLog = info.drain(..).collect::<Vec<_>>();
124            tmp_info
125        } else {
126            vec![]
127        };
128        Self::write(&mut list, &self.get_log_path(INFO_DIR));
129    }
130
131    #[inline]
132    pub(super) fn write_debug(&self) {
133        let mut list: ListLog = if let Ok(mut debug) = LOG_DEBUG_QUEUE.write() {
134            let tmp_debug: ListLog = debug.drain(..).collect::<Vec<_>>();
135            tmp_debug
136        } else {
137            vec![]
138        };
139        Self::write(&mut list, &self.get_log_path(DEBUG_DIR));
140    }
141
142    #[inline]
143    pub fn error<T, L>(&self, data: T, func: L)
144    where
145        T: LogDataTrait,
146        L: LogFuncTrait,
147    {
148        Self::add_data(&LOG_ERROR_QUEUE, data, func);
149    }
150
151    #[inline]
152    pub fn info<T, L>(&self, data: T, func: L)
153    where
154        T: LogDataTrait,
155        L: LogFuncTrait,
156    {
157        Self::add_data(&LOG_INFO_QUEUE, data, func);
158    }
159
160    #[inline]
161    pub fn debug<T, L>(&self, data: T, func: L)
162    where
163        T: LogDataTrait,
164        L: LogFuncTrait,
165    {
166        Self::add_data(&LOG_DEBUG_QUEUE, data, func);
167    }
168}