@@ -66,8 +66,7 @@ impl AsyncWrite for MultiWriter {
6666
6767 // First writer determines how many bytes are accepted.
6868 if this. write_current == 0 {
69- // SAFETY: writers are heap-allocated (Box) and won't move.
70- match unsafe { Pin :: new_unchecked ( & mut * this. writers [ 0 ] ) } . poll_write ( cx, buf) {
69+ match pin_writer ( & mut * this. writers [ 0 ] ) . poll_write ( cx, buf) {
7170 Poll :: Ready ( Ok ( n) ) => {
7271 this. write_n = n;
7372 this. write_current = 1 ;
@@ -85,8 +84,7 @@ impl AsyncWrite for MultiWriter {
8584 while this. write_current < this. writers . len ( ) {
8685 let idx = this. write_current ;
8786 let n = this. write_n ;
88- // SAFETY: writers are heap-allocated (Box) and won't move.
89- match unsafe { Pin :: new_unchecked ( & mut * this. writers [ idx] ) } . poll_write ( cx, & buf[ ..n] ) {
87+ match pin_writer ( & mut * this. writers [ idx] ) . poll_write ( cx, & buf[ ..n] ) {
9088 Poll :: Ready ( Ok ( _) ) => this. write_current += 1 ,
9189 Poll :: Ready ( Err ( e) ) => {
9290 tracing:: warn!( cause=?e, "MultiWriter: appender {idx} write error" ) ;
@@ -104,19 +102,13 @@ impl AsyncWrite for MultiWriter {
104102 fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , std:: io:: Error > > {
105103 let this = self . get_mut ( ) ;
106104
107- if this. flush_time . is_none ( ) {
108- let time = Utc :: now ( ) ;
109- this. flush_time = Some ( time) ;
110- }
105+ let time = * this. flush_time . get_or_insert_with ( Utc :: now) ;
106+
111107 while this. flush_current < this. writers . len ( ) {
112108 let idx = this. flush_current ;
113- // SAFETY: writers are heap-allocated (Box) and won't move.
114-
115- if let Some ( time) = this. flush_time . clone ( ) {
116- unsafe { Pin :: new_unchecked ( & mut * this. writers [ idx] ) } . flush_event_datetime ( time) ;
117- }
109+ pin_writer ( & mut * this. writers [ idx] ) . flush_event_datetime ( time) ;
118110
119- match unsafe { Pin :: new_unchecked ( & mut * this. writers [ idx] ) } . poll_flush ( cx) {
111+ match pin_writer ( & mut * this. writers [ idx] ) . poll_flush ( cx) {
120112 Poll :: Ready ( _) => this. flush_current += 1 ,
121113 Poll :: Pending => return Poll :: Pending ,
122114 }
@@ -135,8 +127,7 @@ impl AsyncWrite for MultiWriter {
135127
136128 while this. shutdown_current < this. writers . len ( ) {
137129 let idx = this. shutdown_current ;
138- // SAFETY: writers are heap-allocated (Box) and won't move.
139- match unsafe { Pin :: new_unchecked ( & mut * this. writers [ idx] ) } . poll_shutdown ( cx) {
130+ match pin_writer ( & mut * this. writers [ idx] ) . poll_shutdown ( cx) {
140131 Poll :: Ready ( _) => this. shutdown_current += 1 ,
141132 Poll :: Pending => return Poll :: Pending ,
142133 }
@@ -147,6 +138,11 @@ impl AsyncWrite for MultiWriter {
147138 }
148139}
149140
141+ fn pin_writer ( w : & mut dyn Appender ) -> Pin < & mut dyn Appender > {
142+ // SAFETY: Box contents are heap-allocated and will not move.
143+ unsafe { Pin :: new_unchecked ( w) }
144+ }
145+
150146impl IsTerminal for Logger {
151147 fn is_terminal ( & self ) -> bool {
152148 false
@@ -165,13 +161,6 @@ impl StdoutStream for Logger {
165161}
166162
167163impl Logger {
168- pub fn new ( ) -> Self {
169- Self {
170- properties : Default :: default ( ) ,
171- appenders : vec ! [ ] ,
172- }
173- }
174-
175164 /// Builder-style method to attach an additional appender.
176165 pub fn with_appender < S : AppenderBuilder + Sync + Send + ' static > ( mut self , sink : S ) -> Self {
177166 self . appenders . push ( Arc :: new ( sink) ) ;
@@ -186,12 +175,13 @@ impl Logger {
186175 pub async fn write_msg ( & self , msg : String ) {
187176 let bytes = msg. as_bytes ( ) ;
188177 for appender in & self . appenders {
189- if let Err ( error) = Box :: into_pin ( appender. build ( self . properties . clone ( ) ) )
190- . write_all ( bytes)
191- . await
192- {
178+ let mut pin = Box :: into_pin ( appender. build ( self . properties . clone ( ) ) ) ;
179+ if let Err ( error) = pin. write_all ( bytes) . await {
193180 tracing:: warn!( cause=?error, "write_msg" ) ;
194181 }
182+ if let Err ( error) = pin. flush ( ) . await {
183+ tracing:: warn!( cause=?error, "write_msg flush" ) ;
184+ }
195185 }
196186 }
197187}
0 commit comments