use crate::service::{StandardWriteMessageFormatter, WriteMessageFormatter};
use crate::{service, Concurrency, DirectLogger, Logger, QueuedLogger};
pub struct FmtWrite {
max_retries: usize,
worker_count: usize,
}
pub struct TypedFmtWrite<W>
where
W: std::fmt::Write + Send + Sync + 'static,
{
writer: W,
max_retries: usize,
worker_count: usize,
}
pub struct BoxedFmtWrite {
writer: Box<dyn std::fmt::Write + Send + Sync>,
max_retries: usize,
worker_count: usize,
}
pub type StringFmt = TypedFmtWrite<String>;
impl FmtWrite {
pub fn string(self) -> StringFmt {
StringFmt {
writer: String::with_capacity(1024),
max_retries: self.max_retries,
worker_count: self.worker_count,
}
}
pub fn string_with_capacity(self, capacity: usize) -> StringFmt {
StringFmt {
writer: String::with_capacity(capacity),
max_retries: self.max_retries,
worker_count: self.worker_count,
}
}
pub fn string_sullied(self, writer: String) -> StringFmt {
StringFmt {
writer,
max_retries: self.max_retries,
worker_count: self.worker_count,
}
}
pub fn writer<W>(self, writer: W) -> TypedFmtWrite<W>
where
W: std::fmt::Write + Send + Sync + 'static,
{
TypedFmtWrite {
writer,
max_retries: self.max_retries,
worker_count: self.worker_count,
}
}
pub fn boxed(self, writer: Box<dyn std::fmt::Write + Send + Sync>) -> BoxedFmtWrite {
BoxedFmtWrite {
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::fmt::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::fmt::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::fmt::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::FmtWrite<W, StandardWriteMessageFormatter>>
where
W: std::fmt::Write + Send + Sync + 'static,
{
service::FmtWrite::new(writer)
}
pub fn build_with_formatter<W, MF>(
self,
concurrency: Concurrency,
writer: W,
formatter: MF,
) -> Logger
where
MF: WriteMessageFormatter + 'static,
W: std::fmt::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::fmt::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::fmt::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::FmtWrite<W, MF>>
where
MF: WriteMessageFormatter + 'static,
W: std::fmt::Write + Send + Sync + 'static,
{
service::FmtWrite::with_formatter(writer, formatter)
}
}
impl Default for FmtWrite {
fn default() -> Self {
Self {
max_retries: 3,
worker_count: 1,
}
}
}
impl<W> TypedFmtWrite<W>
where
W: std::fmt::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::FmtWrite<W, StandardWriteMessageFormatter>> {
service::FmtWrite::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::FmtWrite<W, MF>>
where
MF: WriteMessageFormatter + 'static,
{
service::FmtWrite::with_formatter(self.writer, formatter)
}
}
impl BoxedFmtWrite {
pub fn new(writer: Box<dyn std::fmt::Write + Send + Sync>) -> Self {
Self {
writer,
max_retries: 3,
worker_count: 1,
}
}
pub fn get_writer(&self) -> &(dyn std::fmt::Write + Send + Sync) {
self.writer.as_ref()
}
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: Box<dyn std::fmt::Write + Send + Sync>) -> 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::BoxedFmtWrite<StandardWriteMessageFormatter>> {
service::BoxedFmtWrite::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::BoxedFmtWrite<MF>>
where
MF: WriteMessageFormatter + 'static,
{
service::BoxedFmtWrite::with_formatter(self.writer, formatter)
}
}