B
bernd wegener
Hello netties,
I coded the following in perl:
open(LOGFILE,">&STDOUT") ;
if ( $ARGV[0] ) {
close(LOGFILE) ;
open(LOGFILE,">> $ARGV[0]") or die "Cannot open file $ARGV[0]\n" ;
select(LOGFILE) ; # If commented out, nothing
$| = 1 ; # is printed to $ARGV[0] later on
}
while ( <STDIN> ) {
( $sec, $min, $hour, $day, $month, $year ) = localtime() ;
$month += 1 ;
$year += 1900 ;
printf LOGFILE ("%02d.%02d.%04d %02d:%02d:%02d: %s", $day, $month,
$year,
$hour, $min, $sec, $_) ;
}
The program is intended to receive input line by line from STDIN
(continuously, e.g. from UNIX-commands like ping or tail -f) and to
print the input line prepended with a timestamp either to STDOUT or to
a file if a file name is given on the command line.
If I pipe an "ordinary" file to this program:
$ cat test_inp.txt | tstamp.pl
I get all lines of the file printed to the terminal or to a file:
$ cat test_inp.txt | tstamp.pl test_out.txt
each line prepended with the time stamp.
If I try the same with the select-function and the setting of
unbuffered output commented out, i.e.,
close(LOGFILE) ;
open(LOGFILE,">> $ARGV[0]") or die "Cannot open file $ARGV[0]\n" ;
# select(LOGFILE) ; # If commented out, nothing
# $| = 1 ; # is printed to $ARGV[0] later on
and redirect from a continuously writing program:
ping <host> | tstamp.pl
I get proper output on the terminal, but a file as the output target
remains empty. If instead unbuffered output is used ($| = 1), then
output to the file works fine as well.
Does somebody know the reason for this ?
Any help is appreciated.
Cheers
Bernd
I coded the following in perl:
open(LOGFILE,">&STDOUT") ;
if ( $ARGV[0] ) {
close(LOGFILE) ;
open(LOGFILE,">> $ARGV[0]") or die "Cannot open file $ARGV[0]\n" ;
select(LOGFILE) ; # If commented out, nothing
$| = 1 ; # is printed to $ARGV[0] later on
}
while ( <STDIN> ) {
( $sec, $min, $hour, $day, $month, $year ) = localtime() ;
$month += 1 ;
$year += 1900 ;
printf LOGFILE ("%02d.%02d.%04d %02d:%02d:%02d: %s", $day, $month,
$year,
$hour, $min, $sec, $_) ;
}
The program is intended to receive input line by line from STDIN
(continuously, e.g. from UNIX-commands like ping or tail -f) and to
print the input line prepended with a timestamp either to STDOUT or to
a file if a file name is given on the command line.
If I pipe an "ordinary" file to this program:
$ cat test_inp.txt | tstamp.pl
I get all lines of the file printed to the terminal or to a file:
$ cat test_inp.txt | tstamp.pl test_out.txt
each line prepended with the time stamp.
If I try the same with the select-function and the setting of
unbuffered output commented out, i.e.,
close(LOGFILE) ;
open(LOGFILE,">> $ARGV[0]") or die "Cannot open file $ARGV[0]\n" ;
# select(LOGFILE) ; # If commented out, nothing
# $| = 1 ; # is printed to $ARGV[0] later on
and redirect from a continuously writing program:
ping <host> | tstamp.pl
I get proper output on the terminal, but a file as the output target
remains empty. If instead unbuffered output is used ($| = 1), then
output to the file works fine as well.
Does somebody know the reason for this ?
Any help is appreciated.
Cheers
Bernd