@@ -81,15 +81,23 @@ class LogReader
81
81
/**
82
82
* Get a single log line.
83
83
*
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
86
87
*
87
88
* @return null|string
88
89
* @throws \Exception
89
90
*/
90
- public function getLine (int $ timeoutSeconds = 3 , int $ timeoutMicroseconds = 0 ): ?string
91
- {
92
- $ line = $ this ->getSource ()->getLine ($ timeoutSeconds , $ timeoutMicroseconds );
91
+ public function getLine (
92
+ int $ timeoutSeconds = 3 ,
93
+ int $ timeoutMicroseconds = 0 ,
94
+ bool $ throwOnTimeout = false
95
+ ): ?string {
96
+ $ line = $ this ->getSource ()->getLine (
97
+ $ timeoutSeconds ,
98
+ $ timeoutMicroseconds ,
99
+ $ throwOnTimeout
100
+ );
93
101
$ this ->trace (is_null ($ line ) ? "LINE - null " : "LINE: $ line " );
94
102
95
103
return $ line ;
@@ -196,17 +204,27 @@ class LogReader
196
204
}
197
205
}
198
206
207
+ class LogTimoutException extends \Exception
208
+ {
209
+ }
210
+
199
211
abstract class LogSource
200
212
{
201
213
/**
202
214
* Get single line from the source.
203
215
*
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
206
219
*
207
220
* @return string|null
221
+ * @throws LogTimoutException
208
222
*/
209
- public abstract function getLine (int $ timeoutSeconds , int $ timeoutMicroseconds ): ?string ;
223
+ public abstract function getLine (
224
+ int $ timeoutSeconds ,
225
+ int $ timeoutMicroseconds ,
226
+ bool $ throwOnTimeout = false
227
+ ): ?string ;
210
228
211
229
/**
212
230
* Get all lines that has been returned by getLine() method.
@@ -236,13 +254,18 @@ class LogStreamSource extends LogSource
236
254
/**
237
255
* Get single line from the stream.
238
256
*
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
241
260
*
242
261
* @return string|null
262
+ * @throws LogTimoutException
243
263
*/
244
- public function getLine (int $ timeoutSeconds , int $ timeoutMicroseconds ): ?string
245
- {
264
+ public function getLine (
265
+ int $ timeoutSeconds ,
266
+ int $ timeoutMicroseconds ,
267
+ bool $ throwOnTimeout = false
268
+ ): ?string {
246
269
if (feof ($ this ->stream )) {
247
270
return null ;
248
271
}
@@ -255,6 +278,10 @@ class LogStreamSource extends LogSource
255
278
256
279
return $ line ;
257
280
} else {
281
+ if ($ throwOnTimeout ) {
282
+ throw new LogTimoutException ('Timout exceeded when reading line ' );
283
+ }
284
+
258
285
return null ;
259
286
}
260
287
}
@@ -296,13 +323,18 @@ class LogFileSource extends LogSource
296
323
/**
297
324
* Get single line from the file.
298
325
*
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
301
329
*
302
330
* @return string|null
331
+ * @throws LogTimoutException
303
332
*/
304
- public function getLine (int $ timeoutSeconds , int $ timeoutMicroseconds ): ?string
305
- {
333
+ public function getLine (
334
+ int $ timeoutSeconds ,
335
+ int $ timeoutMicroseconds ,
336
+ bool $ throwOnTimeout = false
337
+ ): ?string {
306
338
$ endTime = microtime (true ) + $ timeoutSeconds + ($ timeoutMicroseconds / 1_000_000 );
307
339
while ($ this ->position >= count ($ this ->lines )) {
308
340
if (is_file ($ this ->filePath )) {
@@ -317,6 +349,10 @@ class LogFileSource extends LogSource
317
349
}
318
350
usleep (50_000 );
319
351
if (microtime (true ) > $ endTime ) {
352
+ if ($ throwOnTimeout ) {
353
+ throw new LogTimoutException ('Timout exceeded when reading line ' );
354
+ }
355
+
320
356
return null ;
321
357
}
322
358
}
0 commit comments