color_log/impl.rs
1use super::*;
2
3/// Blanket implementation for any function matching FileLoggerFuncTrait signature.
4///
5/// This allows any compatible closure or function to be used as a log formatter.
6impl<F, T> FileLoggerFuncTrait<T> for F
7where
8 F: Fn(T) -> String + Send + Sync,
9 T: AsRef<str>,
10{
11}
12
13/// Default implementation for FileLogger configuration.
14impl Default for FileLogger {
15 /// Creates default FileLogger configuration.
16 ///
17 /// # Returns
18 ///
19 /// - `Self` - Default FileLogger 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 trace_dir: TRACE_DIR.to_owned(),
26 debug_dir: DEBUG_DIR.to_owned(),
27 info_dir: INFO_DIR.to_owned(),
28 warn_dir: WARN_DIR.to_owned(),
29 error_dir: ERROR_DIR.to_owned(),
30 }
31 }
32}
33
34impl FileLogger {
35 /// Creates new FileLogger configuration with specified parameters.
36 ///
37 /// # Arguments
38 ///
39 /// - `P` - The path for storing log files, which will be converted to string slice.
40 /// - `usize` - The maximum file size limit in bytes.
41 ///
42 /// # Returns
43 ///
44 /// - `Self` - A new FileLogger instance with specified configuration.
45 #[inline(always)]
46 pub fn new<P: AsRef<str>>(path: P, limit_file_size: usize) -> Self {
47 Self {
48 path: path.as_ref().to_owned(),
49 limit_file_size,
50 trace_dir: TRACE_DIR.to_owned(),
51 debug_dir: DEBUG_DIR.to_owned(),
52 info_dir: INFO_DIR.to_owned(),
53 warn_dir: WARN_DIR.to_owned(),
54 error_dir: ERROR_DIR.to_owned(),
55 }
56 }
57
58 /// Checks if logging is enabled.
59 ///
60 /// # Returns
61 ///
62 /// - `bool` - True if logging is enabled.
63 #[inline(always)]
64 pub fn is_enable(&self) -> bool {
65 *self.get_limit_file_size() != DISABLE_LOG_FILE_SIZE
66 }
67
68 /// Checks if logging is disabled.
69 ///
70 /// # Returns
71 ///
72 /// - `bool` - True if logging is disabled.
73 #[inline(always)]
74 pub fn is_disable(&self) -> bool {
75 !self.is_enable()
76 }
77
78 /// Writes log data synchronously to specified directory.
79 ///
80 /// # Arguments
81 ///
82 /// - `T` - The data to be logged, which will be converted to string slice.
83 /// - `L` - The log formatting function.
84 /// - `&str` - The subdirectory for log file.
85 ///
86 /// # Returns
87 ///
88 /// - `&Self` - Reference to self for method chaining.
89 fn write_sync<T, L>(&self, data: T, func: L, dir: &str) -> &Self
90 where
91 T: AsRef<str>,
92 L: FileLoggerFuncTrait<T>,
93 {
94 if self.is_disable() {
95 return self;
96 }
97 let out: String = func(data);
98 let path: String = get_log_path(dir, self.get_path(), self.get_limit_file_size());
99 let _: Result<(), Error> = append_to_file(&path, out.as_bytes());
100 self
101 }
102
103 /// Writes log data asynchronously to specified directory.
104 ///
105 /// # Arguments
106 ///
107 /// - `T` - The data to be logged, which will be converted to string slice.
108 /// - `L` - The log formatting function.
109 /// - `&str` - The subdirectory for log file.
110 ///
111 /// # Returns
112 ///
113 /// - `&Self` - Reference to self for method chaining.
114 async fn write_async<T, L>(&self, data: T, func: L, dir: &str) -> &Self
115 where
116 T: AsRef<str>,
117 L: FileLoggerFuncTrait<T>,
118 {
119 if self.is_disable() {
120 return self;
121 }
122 let out: String = func(data);
123 let path: String = get_log_path(dir, self.get_path(), self.get_limit_file_size());
124 let _: Result<(), Error> = async_append_to_file(&path, out.as_bytes()).await;
125 self
126 }
127
128 /// Logs trace message synchronously.
129 ///
130 /// # Arguments
131 ///
132 /// - `T` - Trace data to be logged, which will be converted to string slice.
133 /// - `L` - FileLogger formatting function.
134 ///
135 /// # Returns
136 ///
137 /// - `&Self` - Reference to self.
138 pub fn trace<T, L>(&self, data: T, func: L) -> &Self
139 where
140 T: AsRef<str>,
141 L: FileLoggerFuncTrait<T>,
142 {
143 self.write_sync(data, func, self.get_trace_dir())
144 }
145
146 /// Logs trace message asynchronously.
147 ///
148 /// # Arguments
149 ///
150 /// - `T` - Trace data to be logged, which will be converted to string slice.
151 /// - `L` - FileLogger formatting function.
152 ///
153 /// # Returns
154 ///
155 /// - `&Self` - Reference to self.
156 pub async fn async_trace<T, L>(&self, data: T, func: L) -> &Self
157 where
158 T: AsRef<str>,
159 L: FileLoggerFuncTrait<T>,
160 {
161 self.write_async(data, func, self.get_trace_dir()).await
162 }
163
164 /// Logs debug message synchronously.
165 ///
166 /// # Arguments
167 ///
168 /// - `T` - Debug data to be logged, which will be converted to string slice.
169 /// - `L` - FileLogger formatting function.
170 ///
171 /// # Returns
172 ///
173 /// - `&Self` - Reference to self.
174 pub fn debug<T, L>(&self, data: T, func: L) -> &Self
175 where
176 T: AsRef<str>,
177 L: FileLoggerFuncTrait<T>,
178 {
179 self.write_sync(data, func, self.get_debug_dir())
180 }
181
182 /// Logs debug message asynchronously.
183 ///
184 /// # Arguments
185 ///
186 /// - `T` - Debug data to be logged, which will be converted to string slice.
187 /// - `L` - FileLogger formatting function.
188 ///
189 /// # Returns
190 ///
191 /// - `&Self` - Reference to self.
192 pub async fn async_debug<T, L>(&self, data: T, func: L) -> &Self
193 where
194 T: AsRef<str>,
195 L: FileLoggerFuncTrait<T>,
196 {
197 self.write_async(data, func, self.get_debug_dir()).await
198 }
199
200 /// Logs info message synchronously.
201 ///
202 /// # Arguments
203 ///
204 /// - `T` - Info data to be logged, which will be converted to string slice.
205 /// - `L` - FileLogger formatting function.
206 ///
207 /// # Returns
208 ///
209 /// - `&Self` - Reference to self.
210 pub fn info<T, L>(&self, data: T, func: L) -> &Self
211 where
212 T: AsRef<str>,
213 L: FileLoggerFuncTrait<T>,
214 {
215 self.write_sync(data, func, self.get_info_dir())
216 }
217
218 /// Logs info message asynchronously.
219 ///
220 /// # Arguments
221 ///
222 /// - `T` - Info data to be logged, which will be converted to string slice.
223 /// - `L` - FileLogger formatting function.
224 ///
225 /// # Returns
226 ///
227 /// - `&Self` - Reference to self.
228 pub async fn async_info<T, L>(&self, data: T, func: L) -> &Self
229 where
230 T: AsRef<str>,
231 L: FileLoggerFuncTrait<T>,
232 {
233 self.write_async(data, func, self.get_info_dir()).await
234 }
235
236 /// Logs warn message synchronously.
237 ///
238 /// # Arguments
239 ///
240 /// - `T` - Warn data to be logged, which will be converted to string slice.
241 /// - `L` - FileLogger formatting function.
242 ///
243 /// # Returns
244 ///
245 /// - `&Self` - Reference to self.
246 pub fn warn<T, L>(&self, data: T, func: L) -> &Self
247 where
248 T: AsRef<str>,
249 L: FileLoggerFuncTrait<T>,
250 {
251 self.write_sync(data, func, self.get_warn_dir())
252 }
253
254 /// Logs warn message asynchronously.
255 ///
256 /// # Arguments
257 ///
258 /// - `T` - Warn data to be logged, which will be converted to string slice.
259 /// - `L` - FileLogger formatting function.
260 ///
261 /// # Returns
262 ///
263 /// - `&Self` - Reference to self.
264 pub async fn async_warn<T, L>(&self, data: T, func: L) -> &Self
265 where
266 T: AsRef<str>,
267 L: FileLoggerFuncTrait<T>,
268 {
269 self.write_async(data, func, self.get_warn_dir()).await
270 }
271
272 /// Logs error message synchronously.
273 ///
274 /// # Arguments
275 ///
276 /// - `T` - Error data to be logged, which will be converted to string slice.
277 /// - `L` - FileLogger formatting function.
278 ///
279 /// # Returns
280 ///
281 /// - `&Self` - Reference to self.
282 pub fn error<T, L>(&self, data: T, func: L) -> &Self
283 where
284 T: AsRef<str>,
285 L: FileLoggerFuncTrait<T>,
286 {
287 self.write_sync(data, func, self.get_error_dir())
288 }
289
290 /// Logs error message asynchronously.
291 ///
292 /// # Arguments
293 ///
294 /// - `T` - Error data to be logged, which will be converted to string slice.
295 /// - `L` - FileLogger formatting function.
296 ///
297 /// # Returns
298 ///
299 /// - `&Self` - Reference to self.
300 pub async fn async_error<T, L>(&self, data: T, func: L) -> &Self
301 where
302 T: AsRef<str>,
303 L: FileLoggerFuncTrait<T>,
304 {
305 self.write_async(data, func, self.get_error_dir()).await
306 }
307}