hyperlane_log/log/
impl.rs

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