use crate::service::{StandardWriteMessageFormatter, WriteMessageFormatter};
use crate::{Concurrency, DirectLogger, Logger, QueuedLogger, service};
use std::fs::File;
use std::io::BufWriter;
pub struct IoWrite {
max_retries: usize,
worker_count: usize,
}
pub struct TypedIoWrite<W>
where
W: std::io::Write + Send + Sync + 'static,
{
writer: W,
max_retries: usize,
worker_count: usize,
}
pub type FileIoFactory = TypedIoWrite<File>;
pub type BufferedFileIoFactory = TypedIoWrite<BufWriter<File>>;
pub type BoxedIoFactory = TypedIoWrite<Box<dyn std::io::Write + Send + Sync>>;
impl IoWrite {
pub fn file(self, file: File) -> FileIoFactory {
FileIoFactory {
writer: file,
max_retries: self.max_retries,
worker_count: self.worker_count,
}
}
pub fn buffered_file(self, file: BufWriter<File>) -> BufferedFileIoFactory {
BufferedFileIoFactory {
writer: file,
max_retries: self.max_retries,
worker_count: self.worker_count,
}
}
pub fn boxed(self, writer: Box<dyn std::io::Write + Send + Sync>) -> BoxedIoFactory {
BoxedIoFactory {
writer,
max_retries: self.max_retries,
worker_count: self.worker_count,
}
}
pub fn writer<W>(self, writer: W) -> TypedIoWrite<W>
where
W: std::io::Write + Send + Sync + 'static,
{
TypedIoWrite {
writer,
max_retries: self.max_retries,
worker_count: self.worker_count,
}
}
pub fn max_retries(self, max_retries: usize) -> Self {
Self {
max_retries,
..self
}
}
pub fn worker_count(self, worker_count: usize) -> Self {
Self {
worker_count,
..self
}
}
pub fn build<W>(self, concurrency: Concurrency, writer: W) -> Logger
where
W: std::io::Write + Send + Sync + 'static,
{
match concurrency {
Concurrency::Sync => Logger::new(self.build_impl_direct(writer)),
Concurrency::Async => Logger::new(self.build_impl_queued(writer)),
}
}
pub fn build_impl_direct<W>(self, writer: W) -> Box<DirectLogger>
where
W: std::io::Write + Send + Sync + 'static,
{
let max_retries = self.max_retries;
DirectLogger::new(self.build_service(writer), max_retries)
}
pub fn build_impl_queued<W>(self, writer: W) -> Box<QueuedLogger>
where
W: std::io::Write + Send + Sync + 'static,
{
let max_retries = self.max_retries;
let worker_count = self.worker_count;
QueuedLogger::new(self.build_service(writer), max_retries, worker_count)
}
pub fn build_service<W>(self, writer: W) -> Box<service::IoWrite<W, StandardWriteMessageFormatter>>
where
W: std::io::Write + Send + Sync + 'static,
{
service::IoWrite::new(writer)
}
pub fn build_with_formatter<W, MF>(
self,
concurrency: Concurrency,
writer: W,
formatter: MF,
) -> Logger
where
MF: WriteMessageFormatter + 'static,
W: std::io::Write + Send + Sync + 'static,
{
match concurrency {
Concurrency::Sync => {
Logger::new(self.build_impl_direct_with_formatter(writer, formatter))
}
Concurrency::Async => {
Logger::new(self.build_impl_queued_with_formatter(writer, formatter))
}
}
}
pub fn build_impl_direct_with_formatter<W, MF>(
self,
writer: W,
formatter: MF,
) -> Box<DirectLogger>
where
MF: WriteMessageFormatter + 'static,
W: std::io::Write + Send + Sync + 'static,
{
let max_retries = self.max_retries;
DirectLogger::new(
self.build_service_with_formatter(writer, formatter),
max_retries,
)
}
pub fn build_impl_queued_with_formatter<W, MF>(
self,
writer: W,
formatter: MF,
) -> Box<QueuedLogger>
where
MF: WriteMessageFormatter + 'static,
W: std::io::Write + Send + Sync + 'static,
{
let max_retries = self.max_retries;
let worker_count = self.worker_count;
QueuedLogger::new(
self.build_service_with_formatter(writer, formatter),
max_retries,
worker_count,
)
}
pub fn build_service_with_formatter<W, MF>(self, writer: W, formatter: MF) -> Box<service::IoWrite<W, MF>>
where
MF: WriteMessageFormatter + 'static,
W: std::io::Write + Send + Sync + 'static,
{
service::IoWrite::with_formatter(writer, formatter)
}
}
impl Default for IoWrite {
fn default() -> Self {
Self {
max_retries: 3,
worker_count: 1,
}
}
}
impl<W> TypedIoWrite<W>
where
W: std::io::Write + Send + Sync + 'static,
{
pub fn new(writer: W) -> Self {
Self {
writer,
max_retries: 3,
worker_count: 1,
}
}
pub fn get_writer(&self) -> &W {
&self.writer
}
pub fn get_max_retries(&self) -> usize {
self.max_retries
}
pub fn get_worker_count(&self) -> usize {
self.worker_count
}
pub fn writer(self, writer: W) -> Self {
Self { writer, ..self }
}
pub fn max_retries(self, max_retries: usize) -> Self {
Self {
max_retries,
..self
}
}
pub fn worker_count(self, worker_count: usize) -> Self {
Self {
worker_count,
..self
}
}
pub fn build(self, concurrency: Concurrency) -> Logger {
match concurrency {
Concurrency::Sync => Logger::new(self.build_impl_direct()),
Concurrency::Async => Logger::new(self.build_impl_queued()),
}
}
pub fn build_impl_direct(self) -> Box<DirectLogger> {
let max_retries = self.max_retries;
DirectLogger::new(self.build_service(), max_retries)
}
pub fn build_impl_queued(self) -> Box<QueuedLogger> {
let max_retries = self.max_retries;
let worker_count = self.worker_count;
QueuedLogger::new(self.build_service(), max_retries, worker_count)
}
pub fn build_service(self) -> Box<service::IoWrite<W, StandardWriteMessageFormatter>> {
service::IoWrite::new(self.writer)
}
pub fn build_with_formatter<MF>(self, concurrency: Concurrency, formatter: MF) -> Logger
where
MF: WriteMessageFormatter + 'static,
{
match concurrency {
Concurrency::Sync => Logger::new(self.build_impl_direct_with_formatter(formatter)),
Concurrency::Async => Logger::new(self.build_impl_queued_with_formatter(formatter)),
}
}
pub fn build_impl_direct_with_formatter<MF>(self, formatter: MF) -> Box<DirectLogger>
where
MF: WriteMessageFormatter + 'static,
{
let max_retries = self.max_retries;
DirectLogger::new(self.build_service_with_formatter(formatter), max_retries)
}
pub fn build_impl_queued_with_formatter<MF>(self, formatter: MF) -> Box<QueuedLogger>
where
MF: WriteMessageFormatter + 'static,
{
let max_retries = self.max_retries;
let worker_count = self.worker_count;
QueuedLogger::new(
self.build_service_with_formatter(formatter),
max_retries,
worker_count,
)
}
pub fn build_service_with_formatter<MF>(self, formatter: MF) -> Box<service::IoWrite<W, MF>>
where
MF: WriteMessageFormatter + 'static,
{
service::IoWrite::with_formatter(self.writer, formatter)
}
}