@@ -269,6 +269,9 @@ impl std::io::Write for ControlEnd<DestinationStderr> {
269269 . insert_content ( buf)
270270 . map_err ( |_e| std:: io:: Error :: other ( "Error inserting content" ) ) ?;
271271
272+ // By default stderr is unbuffered (the content is flushed immediately)
273+ self . flush ( ) ?;
274+
272275 Ok ( buf. len ( ) )
273276 }
274277
@@ -283,21 +286,57 @@ impl std::io::Write for ControlEnd<DestinationStderr> {
283286
284287impl std:: io:: Write for ControlEnd < DestinationStdout > {
285288 fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
286- if self . current_event . is_none ( ) {
287- self . current_event
288- . replace ( Event :: LegacyPassThrough ( LegacyPassThroughOutput :: Stdout (
289- Default :: default ( ) ,
290- ) ) ) ;
289+ // By default stdout is line buffered, so we'll delimit the incoming buffer with new line
290+ // and flush accordingly.
291+ let mut start = 0_usize ;
292+ let mut end = 0_usize ;
293+ while end < buf. len ( ) {
294+ let Some ( byte) = buf. get ( end) else {
295+ break ;
296+ } ;
297+
298+ if byte == & 10 || byte == & 13 {
299+ if self . current_event . is_none ( ) {
300+ self . current_event
301+ . replace ( Event :: LegacyPassThrough ( LegacyPassThroughOutput :: Stderr (
302+ Default :: default ( ) ,
303+ ) ) ) ;
304+ }
305+
306+ let current_event = self
307+ . current_event
308+ . as_mut ( )
309+ . ok_or ( std:: io:: Error :: other ( "No event set" ) ) ?;
310+
311+ current_event
312+ . insert_content ( & buf[ start..=end] )
313+ . map_err ( std:: io:: Error :: other) ?;
314+
315+ self . flush ( ) ?;
316+
317+ start = end + 1 ;
318+ }
319+
320+ end += 1 ;
291321 }
292322
293- let current_event = self
294- . current_event
295- . as_mut ( )
296- . ok_or ( std:: io:: Error :: other ( "No event set" ) ) ?;
323+ if start < end {
324+ if self . current_event . is_none ( ) {
325+ self . current_event
326+ . replace ( Event :: LegacyPassThrough ( LegacyPassThroughOutput :: Stderr (
327+ Default :: default ( ) ,
328+ ) ) ) ;
329+ }
297330
298- current_event
299- . insert_content ( buf)
300- . map_err ( |_e| std:: io:: Error :: other ( "Error inserting content" ) ) ?;
331+ let current_event = self
332+ . current_event
333+ . as_mut ( )
334+ . ok_or ( std:: io:: Error :: other ( "No event set" ) ) ?;
335+
336+ current_event
337+ . insert_content ( & buf[ start..end] )
338+ . map_err ( std:: io:: Error :: other) ?;
339+ }
301340
302341 Ok ( buf. len ( ) )
303342 }
0 commit comments