M
martyjc2000
Hi
I have a logscanner script (don't we all!).
However I have recently discovered that if the log that it is reading
has, say half a line written, then a pause and then the rest of the
line is written, $line = <FH> will read in the first half of the line
first and then read in the second half of the line separately. Here is
a simple e.g. script:
#!/usr/bin/perl
# logscanner.pl
$|=1;
open (MYLOG, "/tmp/mylog");
while ( 1 )
{
while ( ($line = <MYLOG> ) )
{
print ("GOT THIS: ",$line);
}
seek (MYLOG,0, 1);
sleep (1);
}
To recreate problem, touch /tmp/mylog then run the above script.
Then in a separate terminal run this script which will let you write to
the log:
#!/opt/DKBperl/bin/perl
# writer.pl
$|=1;
open (LOG, ">>/tmp/mylog");
select LOG;
$|=1;
$stuff="";
while ( $stuff ne "exit" )
{
$stuff = <STDIN>;
if ( $stuff !~ /n$/ )
{
chomp ($stuff);
}
print LOG $stuff;
}
close (LOG);
The writer.pl script will write whatever you type into the log, without
buffering.
To make a newline enter n at the end of your line.
here is an e.g. run:
../writer.pl
line1n
line2n
line3 beginning
line3 endn
The final /tmp/mylog looks like this:
line1n
line2n
line3 beginningline3 endn
output from logscanner.pl is:
GOT THIS: line1n
GOT THIS: line2n
GOT THIS: line3 beginningGOT THIS: line3 endn
e.g. even though line3beginning did not have a new line, $line =
<MYLOG> returned that partial line. I thought it would wait until a
whole line terminated by a new line character is present.
Any ideas how I can change the logscanner script to only return the
whole line? This has to work in real time (e.g. in a similar way to
tail -f).
big thanks
Martin
I have a logscanner script (don't we all!).
However I have recently discovered that if the log that it is reading
has, say half a line written, then a pause and then the rest of the
line is written, $line = <FH> will read in the first half of the line
first and then read in the second half of the line separately. Here is
a simple e.g. script:
#!/usr/bin/perl
# logscanner.pl
$|=1;
open (MYLOG, "/tmp/mylog");
while ( 1 )
{
while ( ($line = <MYLOG> ) )
{
print ("GOT THIS: ",$line);
}
seek (MYLOG,0, 1);
sleep (1);
}
To recreate problem, touch /tmp/mylog then run the above script.
Then in a separate terminal run this script which will let you write to
the log:
#!/opt/DKBperl/bin/perl
# writer.pl
$|=1;
open (LOG, ">>/tmp/mylog");
select LOG;
$|=1;
$stuff="";
while ( $stuff ne "exit" )
{
$stuff = <STDIN>;
if ( $stuff !~ /n$/ )
{
chomp ($stuff);
}
print LOG $stuff;
}
close (LOG);
The writer.pl script will write whatever you type into the log, without
buffering.
To make a newline enter n at the end of your line.
here is an e.g. run:
../writer.pl
line1n
line2n
line3 beginning
line3 endn
The final /tmp/mylog looks like this:
line1n
line2n
line3 beginningline3 endn
output from logscanner.pl is:
GOT THIS: line1n
GOT THIS: line2n
GOT THIS: line3 beginningGOT THIS: line3 endn
e.g. even though line3beginning did not have a new line, $line =
<MYLOG> returned that partial line. I thought it would wait until a
whole line terminated by a new line character is present.
Any ideas how I can change the logscanner script to only return the
whole line? This has to work in real time (e.g. in a similar way to
tail -f).
big thanks
Martin