1use crate::service::{StandardWriteMessageFormatter, WriteMessageFormatter};
5use crate::{Concurrency, DirectLogger, Logger, QueuedLogger, service};
6use std::fs::File;
7use std::io::BufWriter;
8
9pub struct IoWrite {
19 max_retries: usize,
20 worker_count: usize,
21}
22
23pub struct TypedIoWrite<W>
28where
29 W: std::io::Write + Send + Sync + 'static,
30{
31 writer: W,
32 max_retries: usize,
33 worker_count: usize,
34}
35
36pub type FileIoFactory = TypedIoWrite<File>;
39
40pub type BufferedFileIoFactory = TypedIoWrite<BufWriter<File>>;
46
47pub type BoxedIoFactory = TypedIoWrite<Box<dyn std::io::Write + Send + Sync>>;
50
51impl IoWrite {
52 pub fn file(self, file: File) -> FileIoFactory {
57 FileIoFactory {
58 writer: file,
59 max_retries: self.max_retries,
60 worker_count: self.worker_count,
61 }
62 }
63
64 pub fn buffered_file(self, file: BufWriter<File>) -> BufferedFileIoFactory {
70 BufferedFileIoFactory {
71 writer: file,
72 max_retries: self.max_retries,
73 worker_count: self.worker_count,
74 }
75 }
76
77 pub fn boxed(self, writer: Box<dyn std::io::Write + Send + Sync>) -> BoxedIoFactory {
81 BoxedIoFactory {
82 writer,
83 max_retries: self.max_retries,
84 worker_count: self.worker_count,
85 }
86 }
87
88 pub fn writer<W>(self, writer: W) -> TypedIoWrite<W>
90 where
91 W: std::io::Write + Send + Sync + 'static,
92 {
93 TypedIoWrite {
94 writer,
95 max_retries: self.max_retries,
96 worker_count: self.worker_count,
97 }
98 }
99
100 pub fn max_retries(self, max_retries: usize) -> Self {
102 Self {
103 max_retries,
104 ..self
105 }
106 }
107
108 pub fn worker_count(self, worker_count: usize) -> Self {
110 Self {
111 worker_count,
112 ..self
113 }
114 }
115
116 pub fn build<W>(self, concurrency: Concurrency, writer: W) -> Logger
119 where
120 W: std::io::Write + Send + Sync + 'static,
121 {
122 match concurrency {
123 Concurrency::Sync => Logger::new(self.build_impl_direct(writer)),
124 Concurrency::Async => Logger::new(self.build_impl_queued(writer)),
125 }
126 }
127
128 pub fn build_impl_direct<W>(self, writer: W) -> Box<DirectLogger>
130 where
131 W: std::io::Write + Send + Sync + 'static,
132 {
133 let max_retries = self.max_retries;
134 DirectLogger::new(self.build_service(writer), max_retries)
135 }
136
137 pub fn build_impl_queued<W>(self, writer: W) -> Box<QueuedLogger>
139 where
140 W: std::io::Write + Send + Sync + 'static,
141 {
142 let max_retries = self.max_retries;
143 let worker_count = self.worker_count;
144 QueuedLogger::new(self.build_service(writer), max_retries, worker_count)
145 }
146
147 pub fn build_service<W>(self, writer: W) -> Box<service::IoWrite<W, StandardWriteMessageFormatter>>
149 where
150 W: std::io::Write + Send + Sync + 'static,
151 {
152 service::IoWrite::new(writer)
153 }
154
155 pub fn build_with_formatter<W, MF>(
157 self,
158 concurrency: Concurrency,
159 writer: W,
160 formatter: MF,
161 ) -> Logger
162 where
163 MF: WriteMessageFormatter + 'static,
164 W: std::io::Write + Send + Sync + 'static,
165 {
166 match concurrency {
167 Concurrency::Sync => {
168 Logger::new(self.build_impl_direct_with_formatter(writer, formatter))
169 }
170 Concurrency::Async => {
171 Logger::new(self.build_impl_queued_with_formatter(writer, formatter))
172 }
173 }
174 }
175
176 pub fn build_impl_direct_with_formatter<W, MF>(
178 self,
179 writer: W,
180 formatter: MF,
181 ) -> Box<DirectLogger>
182 where
183 MF: WriteMessageFormatter + 'static,
184 W: std::io::Write + Send + Sync + 'static,
185 {
186 let max_retries = self.max_retries;
187 DirectLogger::new(
188 self.build_service_with_formatter(writer, formatter),
189 max_retries,
190 )
191 }
192
193 pub fn build_impl_queued_with_formatter<W, MF>(
195 self,
196 writer: W,
197 formatter: MF,
198 ) -> Box<QueuedLogger>
199 where
200 MF: WriteMessageFormatter + 'static,
201 W: std::io::Write + Send + Sync + 'static,
202 {
203 let max_retries = self.max_retries;
204 let worker_count = self.worker_count;
205 QueuedLogger::new(
206 self.build_service_with_formatter(writer, formatter),
207 max_retries,
208 worker_count,
209 )
210 }
211
212 pub fn build_service_with_formatter<W, MF>(self, writer: W, formatter: MF) -> Box<service::IoWrite<W, MF>>
214 where
215 MF: WriteMessageFormatter + 'static,
216 W: std::io::Write + Send + Sync + 'static,
217 {
218 service::IoWrite::with_formatter(writer, formatter)
219 }
220}
221
222impl Default for IoWrite {
223 fn default() -> Self {
228 Self {
229 max_retries: 3,
230 worker_count: 1,
231 }
232 }
233}
234
235impl<W> TypedIoWrite<W>
236where
237 W: std::io::Write + Send + Sync + 'static,
238{
239 pub fn new(writer: W) -> Self {
243 Self {
244 writer,
245 max_retries: 3,
246 worker_count: 1,
247 }
248 }
249
250 pub fn get_writer(&self) -> &W {
252 &self.writer
253 }
254
255 pub fn get_max_retries(&self) -> usize {
257 self.max_retries
258 }
259
260 pub fn get_worker_count(&self) -> usize {
262 self.worker_count
263 }
264
265 pub fn writer(self, writer: W) -> Self {
267 Self { writer, ..self }
268 }
269
270 pub fn max_retries(self, max_retries: usize) -> Self {
272 Self {
273 max_retries,
274 ..self
275 }
276 }
277
278 pub fn worker_count(self, worker_count: usize) -> Self {
280 Self {
281 worker_count,
282 ..self
283 }
284 }
285
286 pub fn build(self, concurrency: Concurrency) -> Logger {
290 match concurrency {
291 Concurrency::Sync => Logger::new(self.build_impl_direct()),
292 Concurrency::Async => Logger::new(self.build_impl_queued()),
293 }
294 }
295
296 pub fn build_impl_direct(self) -> Box<DirectLogger> {
301 let max_retries = self.max_retries;
302 DirectLogger::new(self.build_service(), max_retries)
303 }
304
305 pub fn build_impl_queued(self) -> Box<QueuedLogger> {
310 let max_retries = self.max_retries;
311 let worker_count = self.worker_count;
312 QueuedLogger::new(self.build_service(), max_retries, worker_count)
313 }
314
315 pub fn build_service(self) -> Box<service::IoWrite<W, StandardWriteMessageFormatter>> {
318 service::IoWrite::new(self.writer)
319 }
320
321 pub fn build_with_formatter<MF>(self, concurrency: Concurrency, formatter: MF) -> Logger
326 where
327 MF: WriteMessageFormatter + 'static,
328 {
329 match concurrency {
330 Concurrency::Sync => Logger::new(self.build_impl_direct_with_formatter(formatter)),
331 Concurrency::Async => Logger::new(self.build_impl_queued_with_formatter(formatter)),
332 }
333 }
334
335 pub fn build_impl_direct_with_formatter<MF>(self, formatter: MF) -> Box<DirectLogger>
337 where
338 MF: WriteMessageFormatter + 'static,
339 {
340 let max_retries = self.max_retries;
341 DirectLogger::new(self.build_service_with_formatter(formatter), max_retries)
342 }
343
344 pub fn build_impl_queued_with_formatter<MF>(self, formatter: MF) -> Box<QueuedLogger>
346 where
347 MF: WriteMessageFormatter + 'static,
348 {
349 let max_retries = self.max_retries;
350 let worker_count = self.worker_count;
351 QueuedLogger::new(
352 self.build_service_with_formatter(formatter),
353 max_retries,
354 worker_count,
355 )
356 }
357
358 pub fn build_service_with_formatter<MF>(self, formatter: MF) -> Box<service::IoWrite<W, MF>>
361 where
362 MF: WriteMessageFormatter + 'static,
363 {
364 service::IoWrite::with_formatter(self.writer, formatter)
365 }
366}