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