hyperlane_log/log/impl.rs
1use crate::*;
2
3/// Blanket implementation for any function matching LogFuncTrait signature.
4///
5/// This allows any compatible closure or function to be used as a log formatter.
6impl<F, T> LogFuncTrait<T> for F
7where
8 F: Fn(T) -> String + Send + Sync,
9 T: AsRef<str>,
10{
11}
12
13/// Default implementation for Log configuration.
14impl Default for Log {
15 /// Creates default Log configuration.
16 ///
17 /// # Returns
18 ///
19 /// - `Self` - Default Log instance with default path and file size limit.
20 #[inline(always)]
21 fn default() -> Self {
22 Self {
23 path: DEFAULT_LOG_DIR.to_owned(),
24 limit_file_size: DEFAULT_LOG_FILE_SIZE,
25 }
26 }
27}
28
29impl Log {
30 /// Creates new Log configuration with specified parameters.
31 ///
32 /// # Arguments
33 ///
34 /// - `P: AsRef<str>` - The path for storing log files, which will be converted to string slice.
35 /// - `usize` - The maximum file size limit in bytes.
36 ///
37 /// # Returns
38 ///
39 /// - `Self` - A new Log instance with specified configuration.
40 #[inline(always)]
41 pub fn new<P: AsRef<str>>(path: P, limit_file_size: usize) -> Self {
42 Self {
43 path: path.as_ref().to_owned(),
44 limit_file_size,
45 }
46 }
47
48 /// Sets the log file storage path.
49 ///
50 /// # Arguments
51 ///
52 /// - `P: AsRef<str>` - The new path for storing log files, which will be converted to string slice.
53 ///
54 /// # Returns
55 ///
56 /// - `&mut Self` - Mutable reference to self for method chaining.
57 #[inline(always)]
58 pub fn path<P: AsRef<str>>(&mut self, path: P) -> &mut Self {
59 self.path = path.as_ref().to_owned();
60 self
61 }
62
63 /// Sets the maximum size limit for log files.
64 ///
65 /// # Arguments
66 ///
67 /// - `usize` - The new maximum file size limit in bytes.
68 ///
69 /// # Returns
70 ///
71 /// - `&mut Self` - Mutable reference to self for method chaining.
72 #[inline(always)]
73 pub fn limit_file_size(&mut self, limit_file_size: usize) -> &mut Self {
74 self.limit_file_size = limit_file_size;
75 self
76 }
77
78 /// Checks if logging is enabled.
79 ///
80 /// # Returns
81 ///
82 /// - `bool` - True if logging is enabled.
83 #[inline(always)]
84 pub fn is_enable(&self) -> bool {
85 self.limit_file_size != DISABLE_LOG_FILE_SIZE
86 }
87
88 /// Checks if logging is disabled.
89 ///
90 /// # Returns
91 ///
92 /// - `bool` - True if logging is disabled.
93 #[inline(always)]
94 pub fn is_disable(&self) -> bool {
95 !self.is_enable()
96 }
97
98 /// Writes log data synchronously to specified directory.
99 ///
100 /// # Arguments
101 ///
102 /// - `AsRef<str>` - The data to be logged, which will be converted to string slice.
103 /// - `L: LogFuncTrait<T>` - The log formatting function.
104 /// - `&str` - The subdirectory for log file.
105 ///
106 /// # Returns
107 ///
108 /// - `&Self` - Reference to self for method chaining.
109 fn write_sync<T, L>(&self, data: T, func: L, dir: &str) -> &Self
110 where
111 T: AsRef<str>,
112 L: LogFuncTrait<T>,
113 {
114 if self.is_disable() {
115 return self;
116 }
117 let out: String = func(data);
118 let path: String = get_log_path(dir, &self.path, &self.limit_file_size);
119 let _ = append_to_file(&path, out.as_bytes());
120 self
121 }
122
123 /// Writes log data asynchronously to specified directory.
124 ///
125 /// # Arguments
126 ///
127 /// - `AsRef<str>` - The data to be logged, which will be converted to string slice.
128 /// - `L: LogFuncTrait<T>` - The log formatting function.
129 /// - `&str` - The subdirectory for log file.
130 ///
131 /// # Returns
132 ///
133 /// - `&Self` - Reference to self for method chaining.
134 async fn write_async<T, L>(&self, data: T, func: L, dir: &str) -> &Self
135 where
136 T: AsRef<str>,
137 L: LogFuncTrait<T>,
138 {
139 if self.is_disable() {
140 return self;
141 }
142 let out: String = func(data);
143 let path: String = get_log_path(dir, &self.path, &self.limit_file_size);
144 let _ = async_append_to_file(&path, out.as_bytes()).await;
145 self
146 }
147
148 /// Logs error message synchronously.
149 ///
150 /// # Arguments
151 ///
152 /// - `AsRef<str>` - Error data to be logged, which will be converted to string slice.
153 /// - `L: LogFuncTrait<T>` - Log formatting function.
154 ///
155 /// # Returns
156 ///
157 /// - `&Self` - Reference to self.
158 pub fn error<T, L>(&self, data: T, func: L) -> &Self
159 where
160 T: AsRef<str>,
161 L: LogFuncTrait<T>,
162 {
163 self.write_sync(data, func, ERROR_DIR)
164 }
165
166 /// Logs error message asynchronously.
167 ///
168 /// # Arguments
169 ///
170 /// - `AsRef<str>` - Error data to be logged, which will be converted to string slice.
171 /// - `L: LogFuncTrait<T>` - Log formatting function.
172 ///
173 /// # Returns
174 ///
175 /// - `&Self` - Reference to self.
176 pub async fn async_error<T, L>(&self, data: T, func: L) -> &Self
177 where
178 T: AsRef<str>,
179 L: LogFuncTrait<T>,
180 {
181 self.write_async(data, func, ERROR_DIR).await
182 }
183
184 /// Logs info message synchronously.
185 ///
186 /// # Arguments
187 ///
188 /// - `AsRef<str>` - Info data to be logged, which will be converted to string slice.
189 /// - `L: LogFuncTrait<T>` - Log formatting function.
190 ///
191 /// # Returns
192 ///
193 /// - `&Self` - Reference to self.
194 pub fn info<T, L>(&self, data: T, func: L) -> &Self
195 where
196 T: AsRef<str>,
197 L: LogFuncTrait<T>,
198 {
199 self.write_sync(data, func, INFO_DIR)
200 }
201
202 /// Logs info message asynchronously.
203 ///
204 /// # Arguments
205 ///
206 /// - `AsRef<str>` - Info data to be logged, which will be converted to string slice.
207 /// - `L: LogFuncTrait<T>` - Log formatting function.
208 ///
209 /// # Returns
210 ///
211 /// - `&Self` - Reference to self.
212 pub async fn async_info<T, L>(&self, data: T, func: L) -> &Self
213 where
214 T: AsRef<str>,
215 L: LogFuncTrait<T>,
216 {
217 self.write_async(data, func, INFO_DIR).await
218 }
219
220 /// Logs debug message synchronously.
221 ///
222 /// # Arguments
223 ///
224 /// - `AsRef<str>` - Debug data to be logged, which will be converted to string slice.
225 /// - `L: LogFuncTrait<T>` - Log formatting function.
226 ///
227 /// # Returns
228 ///
229 /// - `&Self` - Reference to self.
230 pub fn debug<T, L>(&self, data: T, func: L) -> &Self
231 where
232 T: AsRef<str>,
233 L: LogFuncTrait<T>,
234 {
235 self.write_sync(data, func, DEBUG_DIR)
236 }
237
238 /// Logs debug message asynchronously.
239 ///
240 /// # Arguments
241 ///
242 /// - `AsRef<str>` - Debug data to be logged, which will be converted to string slice.
243 /// - `L: LogFuncTrait<T>` - Log formatting function.
244 ///
245 /// # Returns
246 ///
247 /// - `&Self` - Reference to self.
248 pub async fn async_debug<T, L>(&self, data: T, func: L) -> &Self
249 where
250 T: AsRef<str>,
251 L: LogFuncTrait<T>,
252 {
253 self.write_async(data, func, DEBUG_DIR).await
254 }
255}