如何逐行读取文件10秒?(How can I read a file line by line for 10 seconds?)
我正在开展一个项目,我需要阅读日志并检查错误。 我正在使用while循环:
while($count < 10) { $count++; # Stop waiting if an exception is thrown $sdata = file($logfile); $lastline = $sdata[count($sdata)-1]; if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; } sleep(1); }这是每秒检查一次日志,但是,我需要在它进入时读取每一行。如何在保持while循环的超时值的同时读取日志的最后一行?
I'm working on a project where I need to read a log and check for errors. I'm using a while loop like so:
while($count < 10) { $count++; # Stop waiting if an exception is thrown $sdata = file($logfile); $lastline = $sdata[count($sdata)-1]; if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; } sleep(1); }This is checking the log every second, however, I need to read EVERY line as it comes in. How do I read the last line of the log as it is written while maintaining a timeout value for the while loop?
最满意答案
这样做你需要的吗?
$time = time(); $stop = $time+10; while($time < $stop) { $time = time(); # Stop waiting if an exception is thrown $sdata = file($logfile); $lastline = end($sdata); if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; } }Does this do what you need?
$time = time(); $stop = $time+10; while($time < $stop) { $time = time(); # Stop waiting if an exception is thrown $sdata = file($logfile); $lastline = end($sdata); if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; } }更多推荐
发布评论