@@ -6,16 +6,14 @@ use std::collections::HashMap;
66use std:: time:: { Instant , Duration } ;
77use std:: fmt:: { self , Debug } ;
88
9- pub struct Registry < Id > {
10- /// A worker-specific identifier.
11- id : Id ,
9+ pub struct Registry {
1210 /// A map from names to typed loggers.
1311 map : HashMap < String , ( Box < dyn Any > , Box < dyn Flush > ) > ,
1412 /// An instant common to all logging statements.
1513 time : Instant ,
1614}
1715
18- impl < Id : Clone + ' static > Registry < Id > {
16+ impl Registry {
1917 /// Binds a log name to an action on log event batches.
2018 ///
2119 /// This method also returns any pre-installed action, rather than overwriting it
@@ -27,20 +25,20 @@ impl<Id: Clone+'static> Registry<Id> {
2725 /// seen (likely greater or equal to the timestamp of the last event). The end of a
2826 /// logging stream is indicated only by dropping the associated action, which can be
2927 /// accomplished with `remove` (or a call to insert, though this is not recommended).
30- pub fn insert < T : ' static , F : FnMut ( & Duration , & mut Vec < ( Duration , Id , T ) > ) +' static > (
28+ pub fn insert < T : ' static , F : FnMut ( & Duration , & mut Vec < ( Duration , T ) > ) +' static > (
3129 & mut self ,
3230 name : & str ,
3331 action : F ) -> Option < Box < dyn Any > >
3432 {
35- let logger = Logger :: < T , Id > :: new ( self . time , Duration :: default ( ) , self . id . clone ( ) , action) ;
33+ let logger = Logger :: < T > :: new ( self . time , Duration :: default ( ) , action) ;
3634 self . insert_logger ( name, logger)
3735 }
3836
3937 /// Binds a log name to a logger.
4038 pub fn insert_logger < T : ' static > (
4139 & mut self ,
4240 name : & str ,
43- logger : Logger < T , Id > ) -> Option < Box < dyn Any > >
41+ logger : Logger < T > ) -> Option < Box < dyn Any > >
4442 {
4543 self . map . insert ( name. to_owned ( ) , ( Box :: new ( logger. clone ( ) ) , Box :: new ( logger) ) ) . map ( |x| x. 0 )
4644 }
@@ -56,17 +54,16 @@ impl<Id: Clone+'static> Registry<Id> {
5654 }
5755
5856 /// Retrieves a shared logger, if one has been inserted.
59- pub fn get < T : ' static > ( & self , name : & str ) -> Option < Logger < T , Id > > {
57+ pub fn get < T : ' static > ( & self , name : & str ) -> Option < Logger < T > > {
6058 self . map
6159 . get ( name)
62- . and_then ( |entry| entry. 0 . downcast_ref :: < Logger < T , Id > > ( ) )
60+ . and_then ( |entry| entry. 0 . downcast_ref :: < Logger < T > > ( ) )
6361 . map ( |x| ( * x) . clone ( ) )
6462 }
6563
6664 /// Creates a new logger registry.
67- pub fn new ( time : Instant , id : Id ) -> Self {
65+ pub fn new ( time : Instant ) -> Self {
6866 Registry {
69- id,
7067 time,
7168 map : HashMap :: new ( ) ,
7269 }
@@ -78,7 +75,7 @@ impl<Id: Clone+'static> Registry<Id> {
7875 }
7976}
8077
81- impl < Id > Flush for Registry < Id > {
78+ impl Flush for Registry {
8279 fn flush ( & mut self ) {
8380 for value in self . map . values_mut ( ) {
8481 value. 1 . flush ( ) ;
@@ -88,42 +85,40 @@ impl<Id> Flush for Registry<Id> {
8885
8986/// A buffering logger.
9087#[ derive( Debug ) ]
91- pub struct Logger < T , E > {
92- inner : Rc < RefCell < LoggerInner < T , E , dyn FnMut ( & Duration , & mut Vec < ( Duration , E , T ) > ) > > > ,
88+ pub struct Logger < T > {
89+ inner : Rc < RefCell < LoggerInner < T , dyn FnMut ( & Duration , & mut Vec < ( Duration , T ) > ) > > > ,
9390}
9491
95- impl < T , E : Clone > Clone for Logger < T , E > {
92+ impl < T > Clone for Logger < T > {
9693 fn clone ( & self ) -> Self {
9794 Self {
9895 inner : self . inner . clone ( )
9996 }
10097 }
10198}
10299
103- struct LoggerInner < T , E , A : ?Sized + FnMut ( & Duration , & mut Vec < ( Duration , E , T ) > ) > {
104- id : E ,
100+ struct LoggerInner < T , A : ?Sized + FnMut ( & Duration , & mut Vec < ( Duration , T ) > ) > {
105101 /// common instant used for all loggers.
106102 time : Instant ,
107103 /// offset to allow re-calibration.
108104 offset : Duration ,
109105 /// shared buffer of accumulated log events
110- buffer : Vec < ( Duration , E , T ) > ,
106+ buffer : Vec < ( Duration , T ) > ,
111107 /// action to take on full log buffers.
112108 action : A ,
113109}
114110
115- impl < T , E : Clone > Logger < T , E > {
111+ impl < T > Logger < T > {
116112 /// Allocates a new shareable logger bound to a write destination.
117- pub fn new < F > ( time : Instant , offset : Duration , id : E , action : F ) -> Self
113+ pub fn new < F > ( time : Instant , offset : Duration , action : F ) -> Self
118114 where
119- F : FnMut ( & Duration , & mut Vec < ( Duration , E , T ) > ) +' static
115+ F : FnMut ( & Duration , & mut Vec < ( Duration , T ) > ) +' static
120116 {
121117 let inner = LoggerInner {
122- id,
123118 time,
124119 offset,
125120 action,
126- buffer : Vec :: with_capacity ( LoggerInner :: < T , E , F > :: buffer_capacity ( ) ) ,
121+ buffer : Vec :: with_capacity ( LoggerInner :: < T , F > :: buffer_capacity ( ) ) ,
127122 } ;
128123 let inner = Rc :: new ( RefCell :: new ( inner) ) ;
129124 Logger { inner }
@@ -167,7 +162,7 @@ impl<T, E: Clone> Logger<T, E> {
167162 }
168163}
169164
170- impl < T , E : Clone , A : ?Sized + FnMut ( & Duration , & mut Vec < ( Duration , E , T ) > ) > LoggerInner < T , E , A > {
165+ impl < T , A : ?Sized + FnMut ( & Duration , & mut Vec < ( Duration , T ) > ) > LoggerInner < T , A > {
171166
172167 /// The upper limit for buffers to allocate, size in bytes. [Self::buffer_capacity] converts
173168 /// this to size in elements.
@@ -177,7 +172,7 @@ impl<T, E: Clone, A: ?Sized + FnMut(&Duration, &mut Vec<(Duration, E, T)>)> Logg
177172 /// and 1, inclusively.
178173 // TODO: This fn is not const because it cannot depend on non-Sized generic parameters
179174 fn buffer_capacity ( ) -> usize {
180- let size = :: std:: mem:: size_of :: < ( Duration , E , T ) > ( ) ;
175+ let size = :: std:: mem:: size_of :: < ( Duration , T ) > ( ) ;
181176 if size == 0 {
182177 Self :: BUFFER_SIZE_BYTES
183178 } else if size <= Self :: BUFFER_SIZE_BYTES {
@@ -192,7 +187,7 @@ impl<T, E: Clone, A: ?Sized + FnMut(&Duration, &mut Vec<(Duration, E, T)>)> Logg
192187 {
193188 let elapsed = self . time . elapsed ( ) + self . offset ;
194189 for event in events {
195- self . buffer . push ( ( elapsed, self . id . clone ( ) , event. into ( ) ) ) ;
190+ self . buffer . push ( ( elapsed, event. into ( ) ) ) ;
196191 if self . buffer . len ( ) == self . buffer . capacity ( ) {
197192 // Would call `self.flush()`, but for `RefCell` panic.
198193 ( self . action ) ( & elapsed, & mut self . buffer ) ;
@@ -209,7 +204,7 @@ impl<T, E: Clone, A: ?Sized + FnMut(&Duration, &mut Vec<(Duration, E, T)>)> Logg
209204}
210205
211206/// Flush on the *last* drop of a logger.
212- impl < T , E , A : ?Sized + FnMut ( & Duration , & mut Vec < ( Duration , E , T ) > ) > Drop for LoggerInner < T , E , A > {
207+ impl < T , A : ?Sized + FnMut ( & Duration , & mut Vec < ( Duration , T ) > ) > Drop for LoggerInner < T , A > {
213208 fn drop ( & mut self ) {
214209 // Avoid sending out empty buffers just because of drops.
215210 if !self . buffer . is_empty ( ) {
@@ -218,14 +213,12 @@ impl<T, E, A: ?Sized + FnMut(&Duration, &mut Vec<(Duration, E, T)>)> Drop for Lo
218213 }
219214}
220215
221- impl < T , E , A : ?Sized + FnMut ( & Duration , & mut Vec < ( Duration , E , T ) > ) > Debug for LoggerInner < T , E , A >
216+ impl < T , A : ?Sized + FnMut ( & Duration , & mut Vec < ( Duration , T ) > ) > Debug for LoggerInner < T , A >
222217where
223- E : Debug ,
224218 T : Debug ,
225219{
226220 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
227221 f. debug_struct ( "LoggerInner" )
228- . field ( "id" , & self . id )
229222 . field ( "time" , & self . time )
230223 . field ( "offset" , & self . offset )
231224 . field ( "action" , & "FnMut" )
@@ -240,13 +233,13 @@ trait Flush {
240233 fn flush ( & mut self ) ;
241234}
242235
243- impl < T , E > Flush for Logger < T , E > {
236+ impl < T > Flush for Logger < T > {
244237 fn flush ( & mut self ) {
245238 self . inner . borrow_mut ( ) . flush ( )
246239 }
247240}
248241
249- impl < T , E , A : ?Sized + FnMut ( & Duration , & mut Vec < ( Duration , E , T ) > ) > Flush for LoggerInner < T , E , A > {
242+ impl < T , A : ?Sized + FnMut ( & Duration , & mut Vec < ( Duration , T ) > ) > Flush for LoggerInner < T , A > {
250243 fn flush ( & mut self ) {
251244 let elapsed = self . time . elapsed ( ) + self . offset ;
252245 if !self . buffer . is_empty ( ) {
0 commit comments