read "tail -f file"

This is a discussion on read "tail -f file" within the alt.comp.lang.php forums, part of the PHP Programming Forums category; hallo, In a file are added logs from routers. I'd like to read these log. I think about reading ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-07-2006
_mario.lat
 
Posts: n/a
Default read "tail -f file"

hallo,
In a file are added logs from routers.
I'd like to read these log.
I think about reading data from command "tail -f file" but how can I do
that?
Thank you in advance,
Mario.
Reply With Quote
  #2 (permalink)  
Old 12-08-2006
petersprc
 
Posts: n/a
Default Re: read "tail -f file"

Hi,

You can display a running tail of a log file with this function:

<?

//
// tailFile
//
// Monitor a file and print new data to the browser as it
// becomes available.
//

function tailFile($path)
{
echo <<<end
<script type="text/javascript">
function scrollDown()
{
sh = document.body.scrollHeight
ch = document.body.clientHeight
if (sh > ch){
window.scrollTo(0, sh - ch)
}
}
</script>
<pre>
end;

$tail = popen('tail -50f ' . escapeshellarg($path) . ' 2>&1', 'r');
if (!$tail) {
trigger_error('tail failed.', E_USER_ERROR);
} elseif (stream_set_blocking($tail, 0) === false) {
trigger_error('stream_set_blocking', E_USER_ERROR);
} else {
$buf = '';
$bytes = 0;
$updateTime = 0;

for (;;) {
if (stream_select($r = array($tail), $w = null, $x = null, 9, 0)
=== false) {
trigger_error('stream_select', E_USER_ERROR);
break;
}

$buf .= fread($tail, 8192);
$len = strlen($buf);
$part = false;

if (($nl = strrpos($buf, "\n")) !== false) {
$part = substr($buf, 0, $nl + 1);
$buf = substr($buf, $nl + 1);
} elseif ($len > 65536 || time() - $updateTime > 5) {
$part = $buf;
$buf = '';
}

if ($part !== false) {
$updateTime = time();
$part = htmlentities($part, ENT_QUOTES);
$bytes += strlen($part);
echo '<script type="text/javascript">scrollDown()</script>';
echo $part;
echo '<script type="text/javascript">scrollDown()</script>';
if ($bytes > 262144) {
echo '<script type="text/javascript">window.location = \'' .
htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES) .
'\'</script>';
}
flush();
}

sleep(1);
}

pclose($tail) or trigger_error('pclose', E_USER_ERROR);
}

echo '</pre>';
}

tailFile('/tmp/mylog');

?>

To just run tail once, you can use:

<?

exec('tail -20 /my/file 2>&1', $lines, $exitCode);
if ($exitCode != 0) {
trigger_error('tail failed.', E_USER_ERROR);
} else {
echo '<pre>' . htmlentities(join("\n", $lines), ENT_QUOTES) .
'</pre>';
}

?>

_mario.lat wrote:
> hallo,
> In a file are added logs from routers.
> I'd like to read these log.
> I think about reading data from command "tail -f file" but how can I do
> that?
> Thank you in advance,
> Mario.


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:35 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0