#!/usr/bin/perl
#
#  Action module for Ant monitoring system
#
#  Writes logs into plain file
#
#  Argument - path to log file
#
#

use Fcntl;
use RRDs;

$SIG{QUIT}=sub {close FILE; exit 0;};
$SIG{TERM}=sub {close FILE; exit 0;};

die "Usage: $0 logname\n" if @ARGV<1;

my $file=shift @ARGV;
unless(open(FILE,">>$file")){
  die "Cannot write to $file\n";
}

fcntl(STDIN,F_SETFL,fcntl(STDIN,F_GETFL,0)|O_NONBLOCK);

select STDERR;
$|=1;
select FILE;
$|=1;

my $line;

for(;;){
  $line='';
INNER_LOOP:
  for(;;){
    while(read(STDIN,$p,1)>0){
      $line.=$p;
      print STDERR "ZERO!!!\n" if $p eq "\0";
      last INNER_LOOP if $p eq "\n";
    }
    select(undef,undef,undef,0.1);
  }
  print FILE $line;
}
