Skip to content

Commit 3fbc0c3

Browse files
committed
Remove log_impl::Output and replace it with Box<dyn Log>
1 parent fab151f commit 3fbc0c3

File tree

2 files changed

+56
-119
lines changed

2 files changed

+56
-119
lines changed

src/builders.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -414,79 +414,79 @@ impl Dispatch {
414414

415415
let output = children
416416
.into_iter()
417-
.flat_map(|child| match child {
417+
.filter_map(|child| match child {
418418
OutputInner::Stdout { stream, line_sep } => {
419419
max_child_level = log::LevelFilter::Trace;
420-
Some(log_impl::Output::Stdout(log_impl::Stdout {
420+
Some(Box::new(log_impl::Stdout {
421421
stream,
422422
line_sep,
423-
}))
423+
}) as Box<dyn Log>)
424424
}
425425
OutputInner::Stderr { stream, line_sep } => {
426426
max_child_level = log::LevelFilter::Trace;
427-
Some(log_impl::Output::Stderr(log_impl::Stderr {
427+
Some(Box::new(log_impl::Stderr {
428428
stream,
429429
line_sep,
430430
}))
431431
}
432432
OutputInner::File { stream, line_sep } => {
433433
max_child_level = log::LevelFilter::Trace;
434-
Some(log_impl::Output::File(log_impl::File {
434+
Some(Box::new(log_impl::File {
435435
stream: Mutex::new(io::BufWriter::new(stream)),
436436
line_sep,
437437
}))
438438
}
439439
OutputInner::Writer { stream, line_sep } => {
440440
max_child_level = log::LevelFilter::Trace;
441-
Some(log_impl::Output::Writer(log_impl::Writer {
441+
Some(Box::new(log_impl::Writer {
442442
stream: Mutex::new(stream),
443443
line_sep,
444444
}))
445445
}
446446
#[cfg(all(not(windows), feature = "reopen-03"))]
447447
OutputInner::Reopen { stream, line_sep } => {
448448
max_child_level = log::LevelFilter::Trace;
449-
Some(log_impl::Output::Reopen(log_impl::Reopen {
449+
Some(Box::new(log_impl::Reopen {
450450
stream: Mutex::new(stream),
451451
line_sep,
452452
}))
453453
}
454454
OutputInner::Sender { stream, line_sep } => {
455455
max_child_level = log::LevelFilter::Trace;
456-
Some(log_impl::Output::Sender(log_impl::Sender {
456+
Some(Box::new(log_impl::Sender {
457457
stream: Mutex::new(stream),
458458
line_sep,
459459
}))
460460
}
461461
#[cfg(all(not(windows), feature = "syslog-3"))]
462462
OutputInner::Syslog3(log) => {
463463
max_child_level = log::LevelFilter::Trace;
464-
Some(log_impl::Output::Syslog3(log_impl::Syslog3 { inner: log }))
464+
Some(Box::new(log_impl::Syslog3 { inner: log }))
465465
}
466466
#[cfg(all(not(windows), feature = "syslog-4"))]
467467
OutputInner::Syslog4Rfc3164(logger) => {
468468
max_child_level = log::LevelFilter::Trace;
469-
Some(log_impl::Output::Syslog4Rfc3164(log_impl::Syslog4Rfc3164 {
469+
Some(Box::new(log_impl::Syslog4Rfc3164 {
470470
inner: Mutex::new(logger),
471471
}))
472472
}
473473
#[cfg(all(not(windows), feature = "syslog-4"))]
474474
OutputInner::Syslog4Rfc5424 { logger, transform } => {
475475
max_child_level = log::LevelFilter::Trace;
476-
Some(log_impl::Output::Syslog4Rfc5424(log_impl::Syslog4Rfc5424 {
476+
Some(Box::new(log_impl::Syslog4Rfc5424 {
477477
inner: Mutex::new(logger),
478478
transform,
479479
}))
480480
}
481481
OutputInner::Panic => {
482482
max_child_level = log::LevelFilter::Trace;
483-
Some(log_impl::Output::Panic(log_impl::Panic))
483+
Some(Box::new(log_impl::Panic))
484484
}
485485
OutputInner::Dispatch(child_dispatch) => {
486486
let (child_level, child) = child_dispatch.into_dispatch();
487487
if child_level > log::LevelFilter::Off {
488488
max_child_level = cmp::max(max_child_level, child_level);
489-
Some(log_impl::Output::Dispatch(child))
489+
Some(Box::new(child))
490490
} else {
491491
None
492492
}
@@ -499,18 +499,18 @@ impl Dispatch {
499499

500500
if child_level > log::LevelFilter::Off {
501501
max_child_level = cmp::max(max_child_level, child_level);
502-
Some(log_impl::Output::SharedDispatch(child))
502+
Some(Box::new(log_impl::LogWrapper::<_, log_impl::Dispatch>(child, Default::default())))
503503
} else {
504504
None
505505
}
506506
}
507507
OutputInner::OtherBoxed(child_log) => {
508508
max_child_level = log::LevelFilter::Trace;
509-
Some(log_impl::Output::OtherBoxed(child_log))
509+
Some(Box::new(child_log))
510510
}
511511
OutputInner::OtherStatic(child_log) => {
512512
max_child_level = log::LevelFilter::Trace;
513-
Some(log_impl::Output::OtherStatic(child_log))
513+
Some(Box::new(log_impl::LogRef(child_log)))
514514
}
515515
#[cfg(feature = "date-based")]
516516
OutputInner::DateBased { config } => {
@@ -532,7 +532,7 @@ impl Dispatch {
532532
// ignore errors - we'll just retry later.
533533
let initial_file = config.open_current_log_file(&computed_suffix).ok();
534534

535-
Some(log_impl::Output::DateBased(log_impl::DateBased {
535+
Some(Box::new(log_impl::DateBased {
536536
config,
537537
state: Mutex::new(DateBasedState::new(computed_suffix, initial_file)),
538538
}))

src/log_impl.rs

Lines changed: 39 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use std::borrow::Borrow;
12
use std::{
23
borrow::Cow,
34
collections::HashMap,
45
fmt, fs,
56
io::{self, BufWriter, Write},
6-
sync::{mpsc, Arc, Mutex},
7+
sync::{mpsc, Mutex},
78
};
89

910
#[cfg(feature = "date-based")]
@@ -29,7 +30,7 @@ pub enum LevelConfiguration {
2930
}
3031

3132
pub struct Dispatch {
32-
pub output: Vec<Output>,
33+
pub output: Vec<Box<dyn Log>>,
3334
pub default_level: log::LevelFilter,
3435
pub levels: LevelConfiguration,
3536
pub format: Option<Box<Formatter>>,
@@ -57,29 +58,6 @@ pub struct FormatCallback<'a>(InnerFormatCallback<'a>);
5758

5859
struct InnerFormatCallback<'a>(&'a mut bool, &'a Dispatch, &'a log::Record<'a>);
5960

60-
pub enum Output {
61-
Stdout(Stdout),
62-
Stderr(Stderr),
63-
File(File),
64-
Sender(Sender),
65-
#[cfg(all(not(windows), feature = "syslog-3"))]
66-
Syslog3(Syslog3),
67-
#[cfg(all(not(windows), feature = "syslog-4"))]
68-
Syslog4Rfc3164(Syslog4Rfc3164),
69-
#[cfg(all(not(windows), feature = "syslog-4"))]
70-
Syslog4Rfc5424(Syslog4Rfc5424),
71-
Dispatch(Dispatch),
72-
SharedDispatch(Arc<Dispatch>),
73-
OtherBoxed(Box<dyn Log>),
74-
OtherStatic(&'static dyn Log),
75-
Panic(Panic),
76-
Writer(Writer),
77-
#[cfg(feature = "date-based")]
78-
DateBased(DateBased),
79-
#[cfg(all(not(windows), feature = "reopen-03"))]
80-
Reopen(Reopen),
81-
}
82-
8361
pub struct Stdout {
8462
pub stream: io::Stdout,
8563
pub line_sep: Cow<'static, str>,
@@ -131,6 +109,42 @@ pub struct Syslog4Rfc5424 {
131109
>,
132110
}
133111

112+
// TODO this causes an unnecessary double indirect call.
113+
// See https://github.com/rust-lang/log/issues/458
114+
pub struct LogRef(pub &'static dyn Log);
115+
116+
impl Log for LogRef {
117+
fn enabled(&self, meta: &log::Metadata<'_>) -> bool {
118+
self.0.enabled(meta)
119+
}
120+
fn log(&self, record: &log::Record<'_>) {
121+
self.0.log(record)
122+
}
123+
fn flush(&self) {
124+
self.0.flush()
125+
}
126+
}
127+
128+
// TODO this causes an unnecessary double indirect call.
129+
// See https://github.com/rust-lang/log/issues/458
130+
pub struct LogWrapper<T: Borrow<R>, R: Log>(pub T, pub std::marker::PhantomData<R>);
131+
132+
impl<R, T> Log for LogWrapper<T, R>
133+
where
134+
T: Borrow<R> + Send + Sync,
135+
R: Log,
136+
{
137+
fn enabled(&self, meta: &log::Metadata<'_>) -> bool {
138+
self.0.borrow().enabled(meta)
139+
}
140+
fn log(&self, record: &log::Record<'_>) {
141+
self.0.borrow().log(record)
142+
}
143+
fn flush(&self) {
144+
self.0.borrow().flush()
145+
}
146+
}
147+
134148
pub struct Panic;
135149

136150
pub struct Null;
@@ -293,83 +307,6 @@ impl LevelConfiguration {
293307
}
294308
}
295309

296-
impl Log for Output {
297-
fn enabled(&self, metadata: &log::Metadata) -> bool {
298-
match *self {
299-
Output::Stdout(ref s) => s.enabled(metadata),
300-
Output::Stderr(ref s) => s.enabled(metadata),
301-
Output::File(ref s) => s.enabled(metadata),
302-
Output::Sender(ref s) => s.enabled(metadata),
303-
Output::Dispatch(ref s) => s.enabled(metadata),
304-
Output::SharedDispatch(ref s) => s.enabled(metadata),
305-
Output::OtherBoxed(ref s) => s.enabled(metadata),
306-
Output::OtherStatic(ref s) => s.enabled(metadata),
307-
#[cfg(all(not(windows), feature = "syslog-3"))]
308-
Output::Syslog3(ref s) => s.enabled(metadata),
309-
#[cfg(all(not(windows), feature = "syslog-4"))]
310-
Output::Syslog4Rfc3164(ref s) => s.enabled(metadata),
311-
#[cfg(all(not(windows), feature = "syslog-4"))]
312-
Output::Syslog4Rfc5424(ref s) => s.enabled(metadata),
313-
Output::Panic(ref s) => s.enabled(metadata),
314-
Output::Writer(ref s) => s.enabled(metadata),
315-
#[cfg(feature = "date-based")]
316-
Output::DateBased(ref s) => s.enabled(metadata),
317-
#[cfg(all(not(windows), feature = "reopen-03"))]
318-
Output::Reopen(ref s) => s.enabled(metadata),
319-
}
320-
}
321-
322-
fn log(&self, record: &log::Record) {
323-
match *self {
324-
Output::Stdout(ref s) => s.log(record),
325-
Output::Stderr(ref s) => s.log(record),
326-
Output::File(ref s) => s.log(record),
327-
Output::Sender(ref s) => s.log(record),
328-
Output::Dispatch(ref s) => s.log(record),
329-
Output::SharedDispatch(ref s) => s.log(record),
330-
Output::OtherBoxed(ref s) => s.log(record),
331-
Output::OtherStatic(ref s) => s.log(record),
332-
#[cfg(all(not(windows), feature = "syslog-3"))]
333-
Output::Syslog3(ref s) => s.log(record),
334-
#[cfg(all(not(windows), feature = "syslog-4"))]
335-
Output::Syslog4Rfc3164(ref s) => s.log(record),
336-
#[cfg(all(not(windows), feature = "syslog-4"))]
337-
Output::Syslog4Rfc5424(ref s) => s.log(record),
338-
Output::Panic(ref s) => s.log(record),
339-
Output::Writer(ref s) => s.log(record),
340-
#[cfg(feature = "date-based")]
341-
Output::DateBased(ref s) => s.log(record),
342-
#[cfg(all(not(windows), feature = "reopen-03"))]
343-
Output::Reopen(ref s) => s.log(record),
344-
}
345-
}
346-
347-
fn flush(&self) {
348-
match *self {
349-
Output::Stdout(ref s) => s.flush(),
350-
Output::Stderr(ref s) => s.flush(),
351-
Output::File(ref s) => s.flush(),
352-
Output::Sender(ref s) => s.flush(),
353-
Output::Dispatch(ref s) => s.flush(),
354-
Output::SharedDispatch(ref s) => s.flush(),
355-
Output::OtherBoxed(ref s) => s.flush(),
356-
Output::OtherStatic(ref s) => s.flush(),
357-
#[cfg(all(not(windows), feature = "syslog-3"))]
358-
Output::Syslog3(ref s) => s.flush(),
359-
#[cfg(all(not(windows), feature = "syslog-4"))]
360-
Output::Syslog4Rfc3164(ref s) => s.flush(),
361-
#[cfg(all(not(windows), feature = "syslog-4"))]
362-
Output::Syslog4Rfc5424(ref s) => s.flush(),
363-
Output::Panic(ref s) => s.flush(),
364-
Output::Writer(ref s) => s.flush(),
365-
#[cfg(feature = "date-based")]
366-
Output::DateBased(ref s) => s.flush(),
367-
#[cfg(all(not(windows), feature = "reopen-03"))]
368-
Output::Reopen(ref s) => s.flush(),
369-
}
370-
}
371-
}
372-
373310
impl Log for Null {
374311
fn enabled(&self, _: &log::Metadata) -> bool {
375312
false

0 commit comments

Comments
 (0)