####################################################### # log_window('/apps/foobar/var/logs/MainLogFile.20030921','Main Log File'); # # Create window for viewing/tailing log file # Recognise file as compressed if it ends in .Z sub log_window { my ($file,$title) = @_; my $compressed = $file =~ /.Z$/; my $freeze = 0; # Create new window my $newwin = $mainw->Toplevel( -title => $title ); # Frame for buttons my $buttbox = $newwin->Frame->pack( -side => 'top'); # Read only textbox for log file text my $txt = $newwin->Scrolled('ROText', -scrollbars => 'oe', -height => 20, -setgrid => 'true', ) ->pack( -expand => 1, -fill => 'both'); # Set up closure to handle tailing my $logger = make_logger($file, $txt, \$freeze); # closure # Buttons... $buttbox->Button( -text => 'Close', -command => [$newwin, 'destroy']) ->pack( -side => 'left'); $buttbox->Button( -text => 'Top', -command => [$txt, 'see', '0.0']) ->pack( -side => 'right'); $buttbox->Button( -text => 'Bottom', -command => [$txt, 'see', 'end']) ->pack( -side => 'right'); if ($compressed) { # Log is compressed, so expand it. No need for tail as it's not growing $txt->insert('end' => `zcat $file`); $txt->see('end'); } else { # Log is not compressed. add checkbox for freeze frame and set up tailing $buttbox->Checkbutton( -text => 'Freeze frame', -variable => \$freeze, -indicatoron => 0, -selectcolor => 'white', ) ->pack( -side => 'left', -ipady => 4); $newwin->repeat( $myconf->param('logfile_poll'), $logger); &$logger; } } ########################################## # make_logger('/apps/foobar/var/logs/MainLogFile.20030921',$txt,\$freeze) # # closure factory for loggers. Generates private copy of $size to keep track of end of file. sub make_logger { my ($log,$box,$reffrz) = @_; my $size = 0; sub { log_to_window($log,$box,$reffrz,\$size); } } ########################################## # log_to_window('/app/foobar/var/logs/MainLogFile.20030921',$txt,\$freeze,\$size) # # This is the code which is called back (as a closure) on a timer to tail log files. # It is also called to initially populate the window. sub log_to_window { my ($log,$box,$reffrz,$refsize) = @_; print "log_to_window called: $log\n" if $debug; return if $$reffrz; # Freeze frame: do nothing return if $$refsize == (-s $log); # Log size has not changed: do nothing my $logMax = $myconf->param('logfile_max_lines'); open LOG,$log or return; # Wot no log file? seek LOG,$$refsize,0 if $$refsize; # Position at last EOF my @logtext; while () { # slurp from log file push @logtext,$_; shift @logtext if @logtext >= $logMax; #Drop lines after line count reaches logMax } $box->insert(end => join('',@logtext)); $box->delete('1.0', "end - $logMax lines"); $box->see('end'); # Position window to end as new text has appeared $$refsize = tell LOG; close LOG; }