1+ use std:: borrow:: Borrow ;
12use 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
3132pub 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
5859struct 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-
8361pub 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+
134148pub struct Panic ;
135149
136150pub 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-
373310impl Log for Null {
374311 fn enabled ( & self , _: & log:: Metadata ) -> bool {
375312 false
0 commit comments