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