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