Skip to content

Commit

Permalink
refactor: better non-blocking builder method
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Nov 12, 2024
1 parent 7ab3a8c commit badb946
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/rolling_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() {
.build("logs")
.unwrap();

let (non_blocking, _guard) = rolling_file::non_blocking_builder().finish(rolling_writer);
let (non_blocking, _guard) = rolling_file::non_blocking(rolling_writer).finish();

logforth::builder()
.dispatch(|d| {
Expand Down
2 changes: 1 addition & 1 deletion examples/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use logforth::append::syslog::SyslogWriter;

fn main() {
let syslog_writer = SyslogWriter::tcp_well_known().unwrap();
let (non_blocking, _guard) = syslog::non_blocking_builder().finish(syslog_writer);
let (non_blocking, _guard) = syslog::non_blocking(syslog_writer).finish();

logforth::builder()
.dispatch(|d| {
Expand Down
6 changes: 3 additions & 3 deletions src/append/rolling_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! .build("logs")
//! .unwrap();
//!
//! let (non_blocking, _guard) = rolling_file::non_blocking_builder().finish(rolling_writer);
//! let (non_blocking, _guard) = rolling_file::non_blocking(rolling_writer).finish();
//!
//! logforth::builder()
//! .dispatch(|d| {
Expand All @@ -54,6 +54,6 @@ mod rolling;
mod rotation;

/// Create a non-blocking builder for rolling file writers.
pub fn non_blocking_builder() -> NonBlockingBuilder<RollingFileWriter> {
NonBlockingBuilder::new("logforth-rolling-file")
pub fn non_blocking(writer: RollingFileWriter) -> NonBlockingBuilder<RollingFileWriter> {
NonBlockingBuilder::new("logforth-rolling-file", writer)
}
6 changes: 3 additions & 3 deletions src/append/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! use logforth::append::syslog::SyslogWriter;
//!
//! let syslog_writer = SyslogWriter::tcp_well_known().unwrap();
//! let (non_blocking, _guard) = syslog::non_blocking_builder().finish(syslog_writer);
//! let (non_blocking, _guard) = syslog::non_blocking(syslog_writer).finish();
//!
//! logforth::builder()
//! .dispatch(|d| {
Expand Down Expand Up @@ -168,8 +168,8 @@ impl Append for Syslog {
}

/// Create a non-blocking builder for syslog writers.
pub fn non_blocking_builder() -> NonBlockingBuilder<SyslogWriter> {
NonBlockingBuilder::new("logforth-syslog")
pub fn non_blocking(writer: SyslogWriter) -> NonBlockingBuilder<SyslogWriter> {
NonBlockingBuilder::new("logforth-syslog", writer)
}

/// A writer that writes formatted log records to syslog.
Expand Down
10 changes: 5 additions & 5 deletions src/non_blocking/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@ pub struct NonBlockingBuilder<T: Writer + Send + 'static> {
thread_name: String,
buffered_lines_limit: Option<usize>,
shutdown_timeout: Option<Duration>,
marker: std::marker::PhantomData<T>,
writer: T,
}

impl<T: Writer + Send + 'static> NonBlockingBuilder<T> {
pub(crate) fn new(thread_name: impl Into<String>) -> Self {
pub(crate) fn new(thread_name: impl Into<String>, writer: T) -> Self {
Self {
thread_name: thread_name.into(),
buffered_lines_limit: None,
shutdown_timeout: None,
marker: std::marker::PhantomData,
writer,
}
}

Expand All @@ -163,9 +163,9 @@ impl<T: Writer + Send + 'static> NonBlockingBuilder<T> {
}

/// Completes the builder, returning the configured `NonBlocking`.
pub fn finish(self, writer: T) -> (NonBlocking<T>, WorkerGuard) {
pub fn finish(self) -> (NonBlocking<T>, WorkerGuard) {
NonBlocking::create(
writer,
self.writer,
self.thread_name,
self.buffered_lines_limit,
self.shutdown_timeout,
Expand Down
2 changes: 1 addition & 1 deletion tests/recursive_logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn test_meta_logging_in_format_works() {
.max_file_size(1024 * 1024)
.build("logs")
.unwrap();
let (writer, _guard) = rolling_file::non_blocking_builder().finish(rolling);
let (writer, _guard) = rolling_file::non_blocking(rolling).finish();

let layout = |src: &'static str| {
layout::CustomLayout::new(move |record| {
Expand Down

0 comments on commit badb946

Please sign in to comment.