From e36b5da695d7b3858c19c76adb93c7d87d489738 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 26 Jun 2010 20:47:40 +0100 Subject: [PATCH] wip newstailer; need to handle date conversion urgh --- newstailer | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 newstailer diff --git a/newstailer b/newstailer new file mode 100644 index 0000000..7d22520 --- /dev/null +++ b/newstailer @@ -0,0 +1,94 @@ +#!/usr/bin/perl -w + +use strict qw(refs vars); +use POSIX; + +our $polltime= 60; +our $logfile= "/var/log/news.notice"; + +our ($loghandle, $logfile_devino); + +sub current_devino () { return join '.', (stat _)[0..1]; } + +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 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); + } + 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]; + } + + 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; + } +} + +backlog(); +logpoll(); -- 2.30.2