Skip to content

Commit

Permalink
OutputInner: remove struct duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed Jul 9, 2021
1 parent 3fbc0c3 commit 3516e59
Showing 1 changed file with 64 additions and 97 deletions.
161 changes: 64 additions & 97 deletions src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,48 +415,30 @@ impl Dispatch {
let output = children
.into_iter()
.filter_map(|child| match child {
OutputInner::Stdout { stream, line_sep } => {
OutputInner::Stdout(child) => {
max_child_level = log::LevelFilter::Trace;
Some(Box::new(log_impl::Stdout {
stream,
line_sep,
}) as Box<dyn Log>)
Some(Box::new(child) as Box<dyn Log>)
}
OutputInner::Stderr { stream, line_sep } => {
OutputInner::Stderr(child) => {
max_child_level = log::LevelFilter::Trace;
Some(Box::new(log_impl::Stderr {
stream,
line_sep,
}))
Some(Box::new(child))
}
OutputInner::File { stream, line_sep } => {
OutputInner::File(child) => {
max_child_level = log::LevelFilter::Trace;
Some(Box::new(log_impl::File {
stream: Mutex::new(io::BufWriter::new(stream)),
line_sep,
}))
Some(Box::new(child))
}
OutputInner::Writer { stream, line_sep } => {
OutputInner::Writer(child) => {
max_child_level = log::LevelFilter::Trace;
Some(Box::new(log_impl::Writer {
stream: Mutex::new(stream),
line_sep,
}))
Some(Box::new(child))
}
#[cfg(all(not(windows), feature = "reopen-03"))]
OutputInner::Reopen { stream, line_sep } => {
OutputInner::Reopen(child) => {
max_child_level = log::LevelFilter::Trace;
Some(Box::new(log_impl::Reopen {
stream: Mutex::new(stream),
line_sep,
}))
Some(Box::new(child))
}
OutputInner::Sender { stream, line_sep } => {
OutputInner::Sender(child) => {
max_child_level = log::LevelFilter::Trace;
Some(Box::new(log_impl::Sender {
stream: Mutex::new(stream),
line_sep,
}))
Some(Box::new(child))
}
#[cfg(all(not(windows), feature = "syslog-3"))]
OutputInner::Syslog3(log) => {
Expand Down Expand Up @@ -499,7 +481,10 @@ impl Dispatch {

if child_level > log::LevelFilter::Off {
max_child_level = cmp::max(max_child_level, child_level);
Some(Box::new(log_impl::LogWrapper::<_, log_impl::Dispatch>(child, Default::default())))
Some(Box::new(log_impl::LogWrapper::<_, log_impl::Dispatch>(
child,
Default::default(),
)))
} else {
None
}
Expand Down Expand Up @@ -614,37 +599,19 @@ impl Dispatch {
/// This enum contains various outputs that you can send messages to.
enum OutputInner {
/// Prints all messages to stdout with `line_sep` separator.
Stdout {
stream: io::Stdout,
line_sep: Cow<'static, str>,
},
Stdout(log_impl::Stdout),
/// Prints all messages to stderr with `line_sep` separator.
Stderr {
stream: io::Stderr,
line_sep: Cow<'static, str>,
},
Stderr(log_impl::Stderr),
/// Writes all messages to file with `line_sep` separator.
File {
stream: fs::File,
line_sep: Cow<'static, str>,
},
File(log_impl::File),
/// Writes all messages to the writer with `line_sep` separator.
Writer {
stream: Box<dyn Write + Send>,
line_sep: Cow<'static, str>,
},
Writer(log_impl::Writer),
/// Writes all messages to the reopen::Reopen file with `line_sep`
/// separator.
#[cfg(all(not(windows), feature = "reopen-03"))]
Reopen {
stream: reopen::Reopen<fs::File>,
line_sep: Cow<'static, str>,
},
Reopen(log_impl::Reopen),
/// Writes all messages to mpst::Sender with `line_sep` separator.
Sender {
stream: Sender<String>,
line_sep: Cow<'static, str>,
},
Sender(log_impl::Sender),
/// Passes all messages to other dispatch.
Dispatch(Dispatch),
/// Passes all messages to other dispatch that's shared.
Expand Down Expand Up @@ -756,10 +723,10 @@ impl From<fs::File> for Output {
///
/// File writes are buffered and flushed once per log record.
fn from(file: fs::File) -> Self {
Output(OutputInner::File {
stream: file,
Output(OutputInner::File(log_impl::File {
stream: Mutex::new(io::BufWriter::new(file)),
line_sep: "\n".into(),
})
}))
}
}

Expand All @@ -771,10 +738,10 @@ impl From<Box<dyn Write + Send>> for Output {
/// needed (eg. wrap it in `BufWriter`). However, flush is called after
/// each log record.
fn from(writer: Box<dyn Write + Send>) -> Self {
Output(OutputInner::Writer {
stream: writer,
Output(OutputInner::Writer(log_impl::Writer {
stream: Mutex::new(writer),
line_sep: "\n".into(),
})
}))
}
}

Expand All @@ -783,32 +750,32 @@ impl From<reopen::Reopen<fs::File>> for Output {
/// Creates an output logger which writes all messages to the file contained
/// in the Reopen struct, using `\n` as the separator.
fn from(reopen: reopen::Reopen<fs::File>) -> Self {
Output(OutputInner::Reopen {
stream: reopen,
Output(OutputInner::Reopen(log_impl::Reopen {
stream: Mutex::new(reopen),
line_sep: "\n".into(),
})
}))
}
}

impl From<io::Stdout> for Output {
/// Creates an output logger which writes all messages to stdout with the
/// given handle and `\n` as the separator.
fn from(stream: io::Stdout) -> Self {
Output(OutputInner::Stdout {
Output(OutputInner::Stdout(log_impl::Stdout {
stream,
line_sep: "\n".into(),
})
}))
}
}

impl From<io::Stderr> for Output {
/// Creates an output logger which writes all messages to stderr with the
/// given handle and `\n` as the separator.
fn from(stream: io::Stderr) -> Self {
Output(OutputInner::Stderr {
Output(OutputInner::Stderr(log_impl::Stderr {
stream,
line_sep: "\n".into(),
})
}))
}
}

Expand All @@ -818,10 +785,10 @@ impl From<Sender<String>> for Output {
///
/// All messages sent to the mpsc channel are suffixed with '\n'.
fn from(stream: Sender<String>) -> Self {
Output(OutputInner::Sender {
stream,
Output(OutputInner::Sender(log_impl::Sender {
stream: Mutex::new(stream),
line_sep: "\n".into(),
})
}))
}
}

Expand Down Expand Up @@ -927,10 +894,10 @@ impl Output {
/// [`Dispatch::chain`]: struct.Dispatch.html#method.chain
/// [`fern::log_file`]: fn.log_file.html
pub fn file<T: Into<Cow<'static, str>>>(file: fs::File, line_sep: T) -> Self {
Output(OutputInner::File {
stream: file,
Output(OutputInner::File(log_impl::File {
stream: Mutex::new(io::BufWriter::new(file)),
line_sep: line_sep.into(),
})
}))
}

/// Returns a logger using arbitrary write object and custom separator.
Expand Down Expand Up @@ -969,10 +936,10 @@ impl Output {
///
/// [`Dispatch::chain`]: struct.Dispatch.html#method.chain
pub fn writer<T: Into<Cow<'static, str>>>(writer: Box<dyn Write + Send>, line_sep: T) -> Self {
Output(OutputInner::Writer {
stream: writer,
Output(OutputInner::Writer(log_impl::Writer {
stream: Mutex::new(writer),
line_sep: line_sep.into(),
})
}))
}

/// Returns a reopenable logger, i.e., handling SIGHUP.
Expand Down Expand Up @@ -1008,10 +975,10 @@ impl Output {
reopen: reopen::Reopen<fs::File>,
line_sep: T,
) -> Self {
Output(OutputInner::Reopen {
stream: reopen,
Output(OutputInner::Reopen(log_impl::Reopen {
stream: Mutex::new(reopen),
line_sep: line_sep.into(),
})
}))
}

/// Returns an stdout logger using a custom separator.
Expand All @@ -1034,10 +1001,10 @@ impl Output {
/// # .into_log();
/// ```
pub fn stdout<T: Into<Cow<'static, str>>>(line_sep: T) -> Self {
Output(OutputInner::Stdout {
Output(OutputInner::Stdout(log_impl::Stdout {
stream: io::stdout(),
line_sep: line_sep.into(),
})
}))
}

/// Returns an stderr logger using a custom separator.
Expand All @@ -1057,10 +1024,10 @@ impl Output {
/// # .into_log();
/// ```
pub fn stderr<T: Into<Cow<'static, str>>>(line_sep: T) -> Self {
Output(OutputInner::Stderr {
Output(OutputInner::Stderr(log_impl::Stderr {
stream: io::stderr(),
line_sep: line_sep.into(),
})
}))
}

/// Returns a mpsc::Sender logger using a custom separator.
Expand All @@ -1080,10 +1047,10 @@ impl Output {
/// # .into_log();
/// ```
pub fn sender<T: Into<Cow<'static, str>>>(sender: Sender<String>, line_sep: T) -> Self {
Output(OutputInner::Sender {
stream: sender,
Output(OutputInner::Sender(log_impl::Sender {
stream: Mutex::new(sender),
line_sep: line_sep.into(),
})
}))
}

/// Returns a logger which logs into an RFC5424 syslog.
Expand Down Expand Up @@ -1195,45 +1162,45 @@ impl fmt::Debug for Dispatch {
impl fmt::Debug for OutputInner {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
OutputInner::Stdout {
OutputInner::Stdout(log_impl::Stdout {
ref stream,
ref line_sep,
} => f
}) => f
.debug_struct("Output::Stdout")
.field("stream", stream)
.field("line_sep", line_sep)
.finish(),
OutputInner::Stderr {
OutputInner::Stderr(log_impl::Stderr {
ref stream,
ref line_sep,
} => f
}) => f
.debug_struct("Output::Stderr")
.field("stream", stream)
.field("line_sep", line_sep)
.finish(),
OutputInner::File {
OutputInner::File(log_impl::File {
ref stream,
ref line_sep,
} => f
}) => f
.debug_struct("Output::File")
.field("stream", stream)
.field("line_sep", line_sep)
.finish(),
OutputInner::Writer { ref line_sep, .. } => f
OutputInner::Writer(log_impl::Writer { ref line_sep, .. }) => f
.debug_struct("Output::Writer")
.field("stream", &"<unknown writer>")
.field("line_sep", line_sep)
.finish(),
#[cfg(all(not(windows), feature = "reopen-03"))]
OutputInner::Reopen { ref line_sep, .. } => f
OutputInner::Reopen(log_impl::Reopen { ref line_sep, .. }) => f
.debug_struct("Output::Reopen")
.field("stream", &"<unknown reopen file>")
.field("line_sep", line_sep)
.finish(),
OutputInner::Sender {
OutputInner::Sender(log_impl::Sender {
ref stream,
ref line_sep,
} => f
}) => f
.debug_struct("Output::Sender")
.field("stream", stream)
.field("line_sep", line_sep)
Expand Down

0 comments on commit 3516e59

Please sign in to comment.