#!/usr/bin/perl use strict; use Tk; use File::Tail; my $tail = File::Tail->new(name => "./log2.txt", tail => 150); my $main = MainWindow->new(-title => "Hacker Alert"); my $t = $main->Text(-wrap => 'none')->pack(-expand => 1); my $b = $main->Button( -text => "Quit", -width => 10, -command => \&bye, ); $b->pack(qw/-side right -expand yes -pady 2/); my $b2 = $main->Button( -text => "Hide", -width => 10, -command => sub { $main->iconify }, ); $b2->pack(qw/-side left -expand yes -pady 2/); # check every 5000ms for new events... $main->repeat(5000, [\&collect_lines,$t]); $main->iconify; MainLoop; sub bye { $main->destroy; Tk::exit; } # reap lines arrived during last $main->repeat(...) sub collect_lines { my ($w) = @_; my $timeout = 0.01; my @lines; while (1) { my ($nfound, $timeleft, @pending) = File::Tail::select(undef,undef,undef,$timeout,($tail)); last unless $nfound; # timeout - nothing more to read push @lines, $pending[0]->read; } fill($w,@lines) if @lines; } # update the text widget { my $time = "n/a"; # last timestamp seen (assumes correct log syntax) sub fill { my ($w, @lines) = @_; return unless @lines; # nothing to do foreach (@lines) { $time=$1, next if /^Time:\s*(\S+)/; if ( /(\s+)(\d{1,4})(\.\d{1,4}){3}(\s+)/ ) { $w->insert('end',"[$time] $_"); $w->yview('end'); $time = "n/a"; } } # do annoing stuff to get attention $main->Popup; $main->bell; # $main->focus(); # bah! } }