hyperlane_log/log/impl.rs
1use crate::*;
2
3/// Blanket implementation for any function matching ServerLogFuncTrait signature.
4///
5/// This allows any compatible closure or function to be used as a log formatter.
6impl<F, T> ServerLogFuncTrait<T> for F
7where
8 F: Fn(T) -> String + Send + Sync,
9 T: AsRef<str>,
10{
11}
12
13/// Default implementation for ServerLog configuration.
14impl Default for ServerLog {
15 /// Creates default ServerLog configuration.
16 ///
17 /// # Returns
18 ///
19 /// - `Self` - Default ServerLog 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 ServerLog {
30 /// Creates new ServerLog 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 ServerLog 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: ServerLogFuncTrait<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: ServerLogFuncTrait<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: ServerLogFuncTrait<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: ServerLogFuncTrait<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 trace message synchronously.
149 ///
150 /// # Arguments
151 ///
152 /// - `AsRef<str>` - Trace data to be logged, which will be converted to string slice.
153 /// - `L: ServerLogFuncTrait<T>` - ServerLog formatting function.
154 ///
155 /// # Returns
156 ///
157 /// - `&Self` - Reference to self.
158 pub fn trace<T, L>(&self, data: T, func: L) -> &Self
159 where
160 T: AsRef<str>,
161 L: ServerLogFuncTrait<T>,
162 {
163 self.write_sync(data, func, TRACE_DIR)
164 }
165
166 /// Logs trace message asynchronously.
167 ///
168 /// # Arguments
169 ///
170 /// - `AsRef<str>` - Trace data to be logged, which will be converted to string slice.
171 /// - `L: ServerLogFuncTrait<T>` - ServerLog formatting function.
172 ///
173 /// # Returns
174 ///
175 /// - `&Self` - Reference to self.
176 pub async fn async_trace<T, L>(&self, data: T, func: L) -> &Self
177 where
178 T: AsRef<str>,
179 L: ServerLogFuncTrait<T>,
180 {
181 self.write_async(data, func, TRACE_DIR).await
182 }
183
184 /// Logs debug message synchronously.
185 ///
186 /// # Arguments
187 ///
188 /// - `AsRef<str>` - Debug data to be logged, which will be converted to string slice.
189 /// - `L: ServerLogFuncTrait<T>` - ServerLog formatting function.
190 ///
191 /// # Returns
192 ///
193 /// - `&Self` - Reference to self.
194 pub fn debug<T, L>(&self, data: T, func: L) -> &Self
195 where
196 T: AsRef<str>,
197 L: ServerLogFuncTrait<T>,
198 {
199 self.write_sync(data, func, DEBUG_DIR)
200 }
201
202 /// Logs debug message asynchronously.
203 ///
204 /// # Arguments
205 ///
206 /// - `AsRef<str>` - Debug data to be logged, which will be converted to string slice.
207 /// - `L: ServerLogFuncTrait<T>` - ServerLog formatting function.
208 ///
209 /// # Returns
210 ///
211 /// - `&Self` - Reference to self.
212 pub async fn async_debug<T, L>(&self, data: T, func: L) -> &Self
213 where
214 T: AsRef<str>,
215 L: ServerLogFuncTrait<T>,
216 {
217 self.write_async(data, func, DEBUG_DIR).await
218 }
219
220 /// Logs info message synchronously.
221 ///
222 /// # Arguments
223 ///
224 /// - `AsRef<str>` - Info data to be logged, which will be converted to string slice.
225 /// - `L: ServerLogFuncTrait<T>` - ServerLog formatting function.
226 ///
227 /// # Returns
228 ///
229 /// - `&Self` - Reference to self.
230 pub fn info<T, L>(&self, data: T, func: L) -> &Self
231 where
232 T: AsRef<str>,
233 L: ServerLogFuncTrait<T>,
234 {
235 self.write_sync(data, func, INFO_DIR)
236 }
237
238 /// Logs info message asynchronously.
239 ///
240 /// # Arguments
241 ///
242 /// - `AsRef<str>` - Info data to be logged, which will be converted to string slice.
243 /// - `L: ServerLogFuncTrait<T>` - ServerLog formatting function.
244 ///
245 /// # Returns
246 ///
247 /// - `&Self` - Reference to self.
248 pub async fn async_info<T, L>(&self, data: T, func: L) -> &Self
249 where
250 T: AsRef<str>,
251 L: ServerLogFuncTrait<T>,
252 {
253 self.write_async(data, func, INFO_DIR).await
254 }
255
256 /// Logs warn message synchronously.
257 ///
258 /// # Arguments
259 ///
260 /// - `AsRef<str>` - Warn data to be logged, which will be converted to string slice.
261 /// - `L: ServerLogFuncTrait<T>` - ServerLog formatting function.
262 ///
263 /// # Returns
264 ///
265 /// - `&Self` - Reference to self.
266 pub fn warn<T, L>(&self, data: T, func: L) -> &Self
267 where
268 T: AsRef<str>,
269 L: ServerLogFuncTrait<T>,
270 {
271 self.write_sync(data, func, WARN_DIR)
272 }
273
274 /// Logs warn message asynchronously.
275 ///
276 /// # Arguments
277 ///
278 /// - `AsRef<str>` - Warn data to be logged, which will be converted to string slice.
279 /// - `L: ServerLogFuncTrait<T>` - ServerLog formatting function.
280 ///
281 /// # Returns
282 ///
283 /// - `&Self` - Reference to self.
284 pub async fn async_warn<T, L>(&self, data: T, func: L) -> &Self
285 where
286 T: AsRef<str>,
287 L: ServerLogFuncTrait<T>,
288 {
289 self.write_async(data, func, WARN_DIR).await
290 }
291
292 /// Logs error message synchronously.
293 ///
294 /// # Arguments
295 ///
296 /// - `AsRef<str>` - Error data to be logged, which will be converted to string slice.
297 /// - `L: ServerLogFuncTrait<T>` - ServerLog formatting function.
298 ///
299 /// # Returns
300 ///
301 /// - `&Self` - Reference to self.
302 pub fn error<T, L>(&self, data: T, func: L) -> &Self
303 where
304 T: AsRef<str>,
305 L: ServerLogFuncTrait<T>,
306 {
307 self.write_sync(data, func, ERROR_DIR)
308 }
309
310 /// Logs error message asynchronously.
311 ///
312 /// # Arguments
313 ///
314 /// - `AsRef<str>` - Error data to be logged, which will be converted to string slice.
315 /// - `L: ServerLogFuncTrait<T>` - ServerLog formatting function.
316 ///
317 /// # Returns
318 ///
319 /// - `&Self` - Reference to self.
320 pub async fn async_error<T, L>(&self, data: T, func: L) -> &Self
321 where
322 T: AsRef<str>,
323 L: ServerLogFuncTrait<T>,
324 {
325 self.write_async(data, func, ERROR_DIR).await
326 }
327}