package Apache2::Tail; our $VERSION = 0.02; use strict; use Apache2::RequestIO (); use Apache2::RequestRec (); use Apache2::ServerUtil (); use Apache2::ServerRec (); use File::Tail (); use CGI; use Apache2::Const -compile => qw(OK); use constant TAIL_CNT => 25; sub handler : method { my $class = shift; my $r = shift; my $s = $r->server; my $name = $s->server_hostname; my $error_log = $class->error_log($r); my $q = new CGI($r); my $tail_cnt = $q->param('n') || $class->tail_cnt($r); $r->content_type('text/html'); my $tail = File::Tail->new( name => $error_log, tail => $tail_cnt, nowait => 1, ); $class->print_header($r); while (my $line = $tail->read) { my ($date, $level, $client, $msg); if ($line =~ m{\[(.*?)\]\s*\[(.*?)\]\s*(\[client\s*(.*?)\]\s*)?(.*)}) { $level = $2; $date = $1; $client = $4; $msg = $5; $msg =~ s/\\t/    /g; } next unless $date; $r->print(<<"EOF"); $date$name$level$client$msg EOF last if --$tail_cnt <= 0; } $class->print_footer($r); return Apache2::Const::OK; } sub style { my ($class, $r) = @_; if (my $user_style = $r->dir_config($class . '::CSS')) { return qq(); } else { return <<'EOF'; EOF } } sub print_footer { my ($class, $r) = @_; $r->print(<<'EOF'); EOF } sub print_header { my ($class, $r) = @_; my $style = $class->style($r); $r->print(<<"EOF"); Apache2::Tail $VERSION $style EOF } sub tail_cnt { my ($class, $r) = @_; return $r->dir_config($class . '::Count') || TAIL_CNT(); } sub error_log { my ($class, $r) = @_; my $s = $r->server; return $r->dir_config($class . '::ErrorLog') || Apache2::ServerUtil::server_root_relative($r->pool, $s->error_fname); } 42; __END__ =head1 NAME Apache2::Tail - mod_perl handler to display the error_log =head1 SYNOPSIS PerlModule Apache2::Tail SetHandler modperl PerlHandler Apache2::Tail [PerlSetVar Apache2::Tail::ErrorLog /some/other/log/file] [PerlSetVar Apache2::Tail::CSS /css/mystyle.css] [PerlSetVar Apache2::Tail::Count 100] Order deny,allow allow from 127.0.0.0/8 deny from all =head1 DESCRIPTION Simple mod_perl handler that displays a pretty html version of the error_log. =head2 OPTIONS These options can be configured with PerlSetVar =over 2 =item * Apache2::Tail::ErrorLog The file to display, defaults to the current VirtualHost's I =item * Apache2::Tail::Count The default maximum number of lines to display. Defaults to I<50>, but can also be overriden at request time with the ?n= query parameter =item * Apache2::Tail::CSS The URL to an alternate sylesheet, defaults to some built-in defaults =back =head1 INTERNAL DOCUMENTATION Quick summary of the internal APIs for anybody interested in subclassing Apache2::Tail. =head2 handler($class, $r) The main handler, responsible for processing the request =head2 error_log($class, $r) Must return the full path to the file that needs tailing =head2 print_header($class, $r) prints the HTML header up to and including the EbodyE tag =head2 print_footer($class, $r) prints the HTML footer closing the EbodyE tag =head2 tail_cnt($class, $r) returns the maximum number of lines to tail for this request. =head2 style($class, $r) returns the CSS content for the page =head1 AUTHOR Philippe M. Chiasson Egozer@cpan.orgE =head1 ARTWORK Tara Gibbs Etarag@activestate.comE =head1 REPOSITORY http://svn.ectoplasm.org/projects/perl/Apache2-Tail/trunk/ =head1 COPYRIGHT Copyright 2007 by Philippe M. Chiasson Egozer@cpan.orgE. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See F =cut