@@ -81,15 +81,23 @@ class LogReader
8181    /** 
8282     * Get a single log line. 
8383     * 
84-      * @param int $timeoutSeconds 
85-      * @param int $timeoutMicroseconds 
84+      * @param int  $timeoutSeconds      Read timeout in seconds 
85+      * @param int  $timeoutMicroseconds Read timeout in microseconds 
86+      * @param bool $throwOnTimeout      Whether to throw an exception on timeout 
8687     * 
8788     * @return null|string 
8889     * @throws \Exception 
8990     */ 
90-     public  function  getLine (int  $ timeoutSeconds3 , int  $ timeoutMicroseconds0 ): ?string 
91-     {
92-         $ line$ this getSource ()->getLine ($ timeoutSeconds$ timeoutMicroseconds
91+     public  function  getLine (
92+         int  $ timeoutSeconds3 ,
93+         int  $ timeoutMicroseconds0 ,
94+         bool  $ throwOnTimeoutfalse 
95+     ): ?string  {
96+         $ line$ this getSource ()->getLine (
97+             $ timeoutSeconds
98+             $ timeoutMicroseconds
99+             $ throwOnTimeout
100+         );
93101        $ this trace (is_null ($ line"LINE - null "  : "LINE:  $ line );
94102
95103        return  $ line
@@ -196,17 +204,27 @@ class LogReader
196204    }
197205}
198206
207+ class  LogTimoutException extends  \Exception
208+ {
209+ }
210+ 
199211abstract  class  LogSource
200212{
201213    /** 
202214     * Get single line from the source. 
203215     * 
204-      * @param int $timeoutSeconds      Read timeout in seconds 
205-      * @param int $timeoutMicroseconds Read timeout in microseconds 
216+      * @param int  $timeoutSeconds      Read timeout in seconds 
217+      * @param int  $timeoutMicroseconds Read timeout in microseconds 
218+      * @param bool $throwOnTimeout      Whether to throw an exception on timeout 
206219     * 
207220     * @return string|null 
221+      * @throws LogTimoutException 
208222     */ 
209-     public  abstract  function  getLine (int  $ timeoutSecondsint  $ timeoutMicrosecondsstring ;
223+     public  abstract  function  getLine (
224+         int  $ timeoutSeconds
225+         int  $ timeoutMicroseconds
226+         bool  $ throwOnTimeoutfalse 
227+     ): ?string ;
210228
211229    /** 
212230     * Get all lines that has been returned by getLine() method. 
@@ -236,13 +254,18 @@ class LogStreamSource extends LogSource
236254    /** 
237255     * Get single line from the stream. 
238256     * 
239-      * @param int $timeoutSeconds      Read timeout in seconds 
240-      * @param int $timeoutMicroseconds Read timeout in microseconds 
257+      * @param int  $timeoutSeconds      Read timeout in seconds 
258+      * @param int  $timeoutMicroseconds Read timeout in microseconds 
259+      * @param bool $throwOnTimeout      Whether to throw an exception on timeout 
241260     * 
242261     * @return string|null 
262+      * @throws LogTimoutException 
243263     */ 
244-     public  function  getLine (int  $ timeoutSecondsint  $ timeoutMicrosecondsstring 
245-     {
264+     public  function  getLine (
265+         int  $ timeoutSeconds
266+         int  $ timeoutMicroseconds
267+         bool  $ throwOnTimeoutfalse 
268+     ): ?string  {
246269        if  (feof ($ this stream )) {
247270            return  null ;
248271        }
@@ -255,6 +278,10 @@ class LogStreamSource extends LogSource
255278
256279            return  $ line
257280        } else  {
281+             if  ($ throwOnTimeout
282+                 throw  new  LogTimoutException ('Timout exceeded when reading line ' );
283+             }
284+ 
258285            return  null ;
259286        }
260287    }
@@ -296,13 +323,18 @@ class LogFileSource extends LogSource
296323    /** 
297324     * Get single line from the file. 
298325     * 
299-      * @param int $timeoutSeconds      Read timeout in seconds 
300-      * @param int $timeoutMicroseconds Read timeout in microseconds 
326+      * @param int  $timeoutSeconds      Read timeout in seconds 
327+      * @param int  $timeoutMicroseconds Read timeout in microseconds 
328+      * @param bool $throwOnTimeout      Whether to throw an exception on timeout 
301329     * 
302330     * @return string|null 
331+      * @throws LogTimoutException 
303332     */ 
304-     public  function  getLine (int  $ timeoutSecondsint  $ timeoutMicrosecondsstring 
305-     {
333+     public  function  getLine (
334+         int  $ timeoutSeconds
335+         int  $ timeoutMicroseconds
336+         bool  $ throwOnTimeoutfalse 
337+     ): ?string  {
306338        $ endTimemicrotime (true ) + $ timeoutSeconds$ timeoutMicroseconds1_000_000 );
307339        while  ($ this position  >= count ($ this lines )) {
308340            if  (is_file ($ this filePath )) {
@@ -317,6 +349,10 @@ class LogFileSource extends LogSource
317349            }
318350            usleep (50_000 );
319351            if  (microtime (true ) > $ endTime
352+                 if  ($ throwOnTimeout
353+                     throw  new  LogTimoutException ('Timout exceeded when reading line ' );
354+                 }
355+ 
320356                return  null ;
321357            }
322358        }
0 commit comments