From 0d12ba460bd40d560802866bf6123908822377fa Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 27 Jun 2010 12:33:37 +0100 Subject: [PATCH] Ditch our tailer and parser and use Parse::Syslog and File::Tail --- newstailer | 109 +++++++++++++++-------------------------------------- 1 file changed, 30 insertions(+), 79 deletions(-) mode change 100644 => 100755 newstailer diff --git a/newstailer b/newstailer old mode 100644 new mode 100755 index 7d22520..3ff6b72 --- a/newstailer +++ b/newstailer @@ -3,92 +3,43 @@ use strict qw(refs vars); use POSIX; -our $polltime= 60; -our $logfile= "/var/log/news.notice"; +use IO::Handle; +use IO::File; +use File::Tail; +use Parse::Syslog; -our ($loghandle, $logfile_devino); +die unless @ARGV; +die if $ARGV[0] =~ m/^\-/; -sub current_devino () { return join '.', (stat _)[0..1]; } +my $totail= pop @ARGV; -our %months; -$months{ (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$_] }= $_ - foreach qw(0..11); - -sub processline_core ($) { - local ($_) = @_; - my ($mon,$dom,$h,$m,$s) = - s/^ ([A-Z][a-z]{2}) [ ] - ([ 013]\d) [ ] - ([ 0-2]\d) : (\d\d) : (\d\d) [ ] - [-0-9a-z]+ /x; - return "date re not matched" unless defined $mon; - $mon= $months{$mon}; - return "unknown month $mon" unless defined $mon; - - my $isdst= 0; - $ts= mktime($s,$m,$h, $dom,$current_year, 0,0, $isdst); - $last_ts= +sub run ($) { + my ($object) = @_; + my $parser= new Parse::Syslog $object, repeat=>0, arrayref=>1; + while (my $sl= $parser->next) { + print join("|", @$sl), "\n"; + } } -sub backlog () { - my $fh= new IO::File '<', "$logfile.0"; - if (!$fh) { - die "$logfile.0 $!" unless $!==&ENOENT; - return; - } - while (defined(my $l= <$fh>)) { - $l =~ s/\n$// or last; - processline($l); +foreach my $staticpath (@ARGV) { + if ($staticpath =~ m/\.gz$/) { + my $fh= new IO::Handle; + open $fh, '-|', 'gunzip', '--', $staticpath or die $!; + run($fh); + !$fh->error or die "$staticpath $!"; + $!=0;$?=0; close $fh or die "$staticpath $! $?"; + } else { + my $fh= new IO::File '<', $staticpath or die $!; + run($staticpath); + !$fh->error or die "$staticpath $!"; + close $fh or die "$staticpath $!"; } - close $fh; } - -sub logpoll () { - my $lbuf= ''; - my $waited_eol= 0; - - for (;;) { - sleep($polltime); - if (!$loghandle) { - $loghandle= new IO::File '<', $logfile; - if (!$loghandle) { - die "$logfile $!" unless $!==&ENOENT; - next; - } - stat $loghandle or die $!; - $logfile_devino= join '.', (stat _)[0..1]; - } +my $tailer= new File::Tail name=>$totail, + interval=>60, adjustafter=>2, ignore_nonexistant=>1, tail=>-1 + or die "$totail $!"; - for (;;) { - if ($lbuf =~ s/\n$//) { - processline($l); - $lbuf= ''; - } - my $xtra= <$loghandle>; - if (!defined $xtra) { - die "$logfile $!" if $loghandle->error; - last; - } - $lbuf .= $xtra; - } - - if (stat $logfile) { - next if $logfile_devino eq current_devino; - } else { - die "$logfile $!" unless $!==&ENOENT; - } - - # current file is no longer right - if (length $lbuf) { - # wait for the rest of the line ? - next unless $waited_eol++ > 3; - } - $lbuf= ''; - close $loghandle; - $loghandle= undef; - } -} +run($tailer); -backlog(); -logpoll(); +die "huh?"; -- 2.30.2